forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-DirectoryRestoreFile.ps1
42 lines (40 loc) · 1.45 KB
/
Get-DirectoryRestoreFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function Get-DirectoryRestoreFile {
<#
.SYNOPSIS
Internal Function to get SQL Server backfiles from a specified folder
.DESCRIPTION
Takes path, checks for validity. Scans for usual backup file
#>
[CmdletBinding()]
Param (
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$Path,
[switch]$Recurse,
[switch][Alias('Silent')]$EnableException
)
Write-Message -Level Verbose -Message "Starting"
Write-Message -Level Verbose -Message "Checking Path"
if ((Test-Path $Path) -ne $true) {
Stop-Function -Message "$Path is not reachable"
return
}
#Path needs to end \* to use includes, which is faster than Where-Object
$PathCheckArray = $path.ToCharArray()
if ($PathCheckArray[-2] -eq '\' -and $PathCheckArray[-1] -eq '*') {
#We're good
}
elseif ($PathCheckArray[-2] -ne '\' -and $PathCheckArray[-1] -eq '*') {
$Path = ($PathCheckArray[0..(($PathCheckArray.length) - 2)] -join ('')) + "\*"
}
elseif ($PathCheckArray[-2] -eq '\' -and $PathCheckArray[-1] -ne '*') {
#Append a * to the end
$Path = "$Path*"
}
elseif ($PathCheckArray[-2] -ne '\' -and $PathCheckArray[-1] -ne '*') {
#Append a \* to the end
$Path = "$Path\*"
}
Write-Message -Level Verbose -Message "Scanning $path"
$Results = Get-ChildItem -path $Path -Recurse:$Recurse | Where-Object {$_.PsIsContainer -eq $false}
return $Results
}