Public/Get-WinSCPChildItem.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function Get-WinSCPChildItem {

    [CmdletBinding(
        HelpUri = "https://github.com/dotps1/WinSCP/wiki/Get-WinSCPChildItem",
        PositionalBinding = $false
    )]
    [OutputType(
        [Array]
    )]

    param (
        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateScript({ 
            if ($_.Opened) { 
                return $true 
            } else { 
                throw "The WinSCP Session is not in an Open state."
            }
        })]
        [WinSCP.Session]
        $WinSCPSession,

        [Parameter(
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [String[]]
        $Path = $WinSCPSession.HomePath,

        [Parameter()]
        [String]
        $Filter = $null,

        [Parameter()]
        [Switch]
        $Recurse,

        [Parameter()]
        [Int]
        $Depth,

        [Parameter()]
        [Switch]
        $Name,

        [Parameter()]
        [Switch]
        $Directory,

        [Parameter()]
        [Switch]
        $File
    )

    process {
        foreach ($pathValue in ( Format-WinSCPPathString -Path $Path )) {
            $depthParameterUsed = $PSBoundParameters.ContainsKey(
                "Depth"
            )

            if ($depthParameterUsed -and -not $Recurse.IsPresent) {
                $Recurse = $true
            }

            if ($Recurse.IsPresent) {
                $enumerationOptions = [WinSCP.EnumerationOptions]::AllDirectories -bor [WinSCP.EnumerationOptions]::MatchDirectories
            } else {
                $enumerationOptions = [WinSCP.EnumerationOptions]::None -bor [WinSCP.EnumerationOptions]::MatchDirectories
            }

            try {
                $items = $WinSCPSession.EnumerateRemoteFiles(
                    $pathValue, $Filter, $enumerationOptions
                )

                $items = $items |
                    Sort-Object -Property IsDirectory -Descending:$false | 
                        Sort-Object -Property @{ Expression = { Split-Path -Path $_.FullName } }, Name

                if ($depthParameterUsed) {
                    $items = $items.Where({
                        ($_.FullName.SubString(
                            0, $_.FullName.LastIndexOf(
                                [System.IO.Path]::AltDirectorySeparatorChar
                            )
                        ).Split(
                            [System.IO.Path]::AltDirectorySeparatorChar
                        ).Count - 1) -le $Depth
                    })
                }

                if ($Directory.IsPresent -and $File.IsPresent) {
                    # If both -Directory and -File switches are used, exit the loop and return nothing.
                    # This mimics the functionality of Get-ChildItem.
                    continue
                } elseif ($Directory.IsPresent -and -not $File.IsPresent ) {
                    $items = $items.Where({
                        $_.IsDirectory -eq $true
                    })
                } elseif ($File.IsPresent -and -not $Directory.IsPresent) {
                        $items = $items.Where({
                        $_.IsDirectory -eq $false
                    }) 
                }

                if ($Name.IsPresent) {
                    $items = $items | 
                        Select-Object -ExpandProperty Name
                }

                Write-Output -InputObject $items
            } catch {
                $PSCmdlet.WriteError(
                    $_
                )
                continue
            }
        }
    }
}