internal/functions/Get-XpDirTreeRestoreFile.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function Get-XpDirTreeRestoreFile {
    <#
    .SYNOPSIS
        Internal Function to get SQL Server backfiles from a specified folder using xp_dirtree
 
    .DESCRIPTION
        Takes path, checks for validity. Scans for usual backup file
 
    .PARAMETER Path
        The path to retrieve the restore for.
 
    .PARAMETER SqlInstance
        The SQL Server that you're connecting to.
 
    .PARAMETER SqlCredential
        Credential object used to connect to the SQL Server as a different user
 
    .PARAMETER EnableException
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
    .EXAMPLE
        PS C:\> Get-XpDirTreeRestoreFile -Path '\\foo\bar\' -SqlInstance $SqlInstance
 
        Tests whether the instance $SqlInstance has access to the path \\foo\bar\
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory, ValueFromPipeline)]
        [string]$Path,
        [parameter(Mandatory)]
        [DbaInstanceParameter]$SqlInstance,
        [System.Management.Automation.PSCredential]$SqlCredential,
        [switch]$EnableException,
        [switch]$NoRecurse
    )

    Write-Message -Level InternalComment -Message "Starting"

    $server = Connect-SqlInstance -SqlInstance $SqlInstance -SqlCredential $SqlCredential
    $pathSep = Get-DbaPathSep -Server $server
    if (($path -like '*.bak') -or ($path -like '*.trn')) {
        # For a future person who knows what's up, please replace this comment with the reason this is empty
    } elseif ($Path[-1] -ne $pathSep) {
        $Path = $Path + $pathSep
    }

    if (!(Test-DbaPath -SqlInstance $server -path $path)) {
        Stop-Function -Message "SqlInstance $SqlInstance cannot access $path"
    }
    if (!(Test-DbaPath -SqlInstance $server -path $Path)) {
        Stop-Function -Message "SqlInstance $SqlInstance cannot access $Path"
    }
    if ($server.VersionMajor -ge 14) {
        # this is all kinds of cool, api could be expanded sooo much here
        $sql = "SELECT file_or_directory_name AS subdirectory, ~CONVERT(BIT, is_directory) as [file], 1 as depth
        FROM sys.dm_os_enumerate_filesystem('$Path', '*')
        WHERE [level] = 0"

    } elseif ($server.VersionMajor -lt 9) {
        $sql = "EXEC master..xp_dirtree '$Path',1,1;"
    } else {
        $sql = "EXEC master.sys.xp_dirtree '$Path',1,1;"
    }
    #$queryResult = Invoke-DbaQuery -SqlInstance $SqlInstance -Credential $SqlCredential -Database tempdb -Query $query
    $queryResult = $server.Query($sql)
    Write-Message -Level Debug -Message $sql
    $dirs = $queryResult | Where-Object file -eq 0
    $Results = @()
    $Results += $queryResult | Where-Object file -eq 1 | Select-Object @{ Name = "FullName"; Expression = { $path + $_.subdirectory } }

    if ($True -ne $NoRecurse) {
        foreach ($d in $dirs) {
            $fullpath = $path + $d.subdirectory
            Write-Message -Level Verbose -Message "Enumerating subdirectory '$fullpath'"
            $Results += Get-XpDirTreeRestoreFile -Path $fullpath -SqlInstance $server
        }
    }
    return $Results
}