Set-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
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
126
Function Set-FTPConnection
{
    <#
    .SYNOPSIS
        Set config to ftp Connection.
 
    .DESCRIPTION
        The Set-FTPConnection cmdlet creates a Windows PowerShell configuration to ftp server. When you create a ftp connection, you may run multiple commands that use this config.
         
    .PARAMETER Credential
        Specifies a user account that has permission to access to ftp location.
             
    .PARAMETER Server
        Specifies the ftp server you want to connect.
             
    .PARAMETER EnableSsl
        Specifies that an SSL connection should be used.
             
    .PARAMETER ignoreCert
        If you use SSL connection you may ignore certificate error.
             
    .PARAMETER KeepAlive
        Specifies whether the control connection to the ftp server is closed after the request completes.
             
    .PARAMETER UseBinary
        Specifies the data type for file transfers.
             
    .PARAMETER UsePassive
        Behavior of a client application's data transfer process.
 
    .PARAMETER Session
        Specifies a friendly name for the ftp session. Default session name is 'DefaultFTPSession'.
     
    .EXAMPLE
 
        Set-FTPConnection -Credentials userName -Server myftpserver.com
         
    .EXAMPLE
 
        $Credentials = Get-Credential
        Set-FTPConnection -Credentials $Credentials -Server ftp://myftpserver.com -EnableSsl -ignoreCert -UsePassive
 
    .NOTES
        Author: Michal Gajda
        Blog : http://commandlinegeeks.com/
 
    .LINK
        Get-FTPChildItem
    #>
    

    [CmdletBinding(
        SupportsShouldProcess=$True,
        ConfirmImpact="Low"
    )]
    Param(
        [parameter(Mandatory=$true)]
        [Alias("Credential")]
        $Credentials, 
        [parameter(Mandatory=$true)]
        [String]$Server,
        [Switch]$EnableSsl = $False,
        [Switch]$ignoreCert = $False,
        [Switch]$KeepAlive = $False,
        [Switch]$UseBinary = $False,
        [Switch]$UsePassive = $False,
        [String]$Session = "DefaultFTPSession"
    )
    
    Begin
    {
        if($Credentials -isnot [System.Management.Automation.PSCredential])
        {
            $Credentials = Get-Credential $Credentials
        }
    }
    
    Process
    {
        if ($pscmdlet.ShouldProcess($Server,"Connect to FTP Server")) 
        {    
            if(!($Server -match "ftp://"))
            {
                $Server = "ftp://"+$Server    
                Write-Debug "Add ftp:// at start: $Server"                
            }
            
            Write-Verbose "Create FtpWebRequest object."
            [System.Net.FtpWebRequest]$Request = [System.Net.WebRequest]::Create($Server)
            $Request.Credentials = $Credentials
            $Request.EnableSsl = $EnableSsl
            $Request.KeepAlive = $KeepAlive
            $Request.UseBinary = $UseBinary
            $Request.UsePassive = $UsePassive
            $Request | Add-Member -MemberType NoteProperty -Name ignoreCert -Value $ignoreCert
            $Request | Add-Member -MemberType NoteProperty -Name Session -Value $Session
            $Request | Add-Member -MemberType NoteProperty -Name StartPath -Value ""

            $Request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
            Try
            {
                [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$ignoreCert}
                $Response = $Request.GetResponse()
                $Response.Close()
                
                if((Get-Variable -Scope Global -Name $Session -ErrorAction SilentlyContinue) -eq $null)
                {
                    Write-Verbose "Create global variable: $Session"
                    New-Variable -Scope Global -Name $Session -Value $Request
                }
                else
                {
                    Write-Verbose "Set global variable: $Session"
                    Set-Variable -Scope Global -Name $Session -Value $Request
                }
                
                Return $Response
            }
            Catch
            {
                Write-Error $_.Exception.Message -ErrorAction Stop 
            }
        }
    }
    
    End{}                
}