Get-FTPConnection.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
Function Get-FTPConnection
{
    <#
    .SYNOPSIS
        Get config to ftp Connection.
 
    .DESCRIPTION
        The Get-FTPConnection cmdlet create a list of registered PSFTP sessions.
         
    .PARAMETER Session
        Specifies a friendly name for the ftp session.
     
    .EXAMPLE
 
        Get-FTPConnection
         
    .EXAMPLE
 
        Get-FTPConnection -Session DefaultFTPS*
 
    .NOTES
        Author: Michal Gajda
        Blog : http://commandlinegeeks.com/
 
    .LINK
        Set-FTPConnection
    #>
    

    [OutputType('PSFTP.Session')]
    [CmdletBinding(
        SupportsShouldProcess=$True,
        ConfirmImpact="Low"
    )]
    Param(
        [String]$Session
    )
    
    Begin{}
    
    Process
    {
        if($Session)
        {
            $Variables = Get-Variable -Scope Global | 
            Where-Object {$_.value -is [System.Net.FtpWebRequest] -and $_.Name -like $Session}
        }
        else
        {
            $Variables = Get-Variable -Scope Global | Where-Object {$_.value -is [System.Net.FtpWebRequest]}
        }
        
        $Sessions = @()
        $Variables | ForEach{
            $CurrentSession = Get-Variable -Scope Global -Name $_.Name -ErrorAction SilentlyContinue -ValueOnly
        
            if($Sessions -notcontains $CurrentSession)
            {
                $Sessions += $_.Value
            }
        }

        $Sessions.PSTypeNames.Clear()
        $Sessions.PSTypeNames.Add('PSFTP.Session')
        
        Return $Sessions
    }
    
    End{}                
}