Public/New-WinSCPSessionOption.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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
function New-WinSCPSessionOption { [CmdletBinding( ConfirmImpact = "Low", HelpUri = "https://github.com/dotps1/WinSCP/wiki/New-WinSCPSessionOption", SupportsShouldProcess = $true )] [OutputType( [WinSCP.SessionOptions] )] param ( [Parameter()] [ValidateNotNull()] [PSCredential] $Credential = [PSCredential]::new( "anonymous", [SecureString]::new() ), [Parameter()] [WinSCP.FtpMode] $FtpMode = (New-Object -TypeName WinSCP.FtpMode), [Parameter()] [WinSCP.FtpSecure] $FtpSecure = (New-Object -TypeName WinSCP.FtpSecure), [Parameter()] [Switch] $GiveUpSecurityAndAcceptAnySshHostKey, [Parameter()] [Switch] $GiveUpSecurityAndAcceptAnyTlsHostCertificate, [Parameter( Mandatory = $true )] [Alias( "ComputerName" )] [String] $HostName, [Parameter()] [Int] $PortNumber = 0, [Parameter()] [SecureString] $PrivateKeyPassphrase, [Parameter()] [WinSCP.Protocol] $Protocol = ( New-Object -TypeName WinSCP.Protocol ), [Parameter( ValueFromPipelineByPropertyName = $true )] [String[]] $SshHostKeyFingerprint, [Parameter()] [ValidateScript({ if (Test-Path -Path $_) { if ((Get-Item -Path $_).PSIsContainer) { throw "Target is not a file: '$_'." } else { return $true } } else { throw "Unable to find part of Path: '$_'." } })] [String] $SshPrivateKeyPath, [Parameter()] [ValidateScript({ if (Test-Path -Path $_) { if ((Get-Item -Path $_).PSIsContainer) { throw "Target is not a file: '$_'." } else { return $true } } else { throw "Unable to find part of Path: '$_'." } })] [String] $TlsClientCertificatePath, [Parameter()] [String] $TlsHostCertificateFingerprint, [Parameter()] [TimeSpan] $Timeout = (New-TimeSpan -Seconds 15), [Parameter()] [Switch] $WebdavSecure, [Parameter()] [String] $WebdavRoot, [Parameter()] [HashTable] $RawSetting ) $sessionOptions = New-Object -TypeName WinSCP.SessionOptions $shouldProcess = $PSCmdlet.ShouldProcess( $sessionOptions ) if ($shouldProcess) { # Convert PSCredential Object to match names of the WinSCP.SessionOptions Object. # Remove the parameter "Credential", that is not a valid property of the WinSCP.SessionObject. $PSBoundParameters.Add( "UserName", $Credential.UserName ) $PSBoundParameters.Add( "SecurePassword", $Credential.Password ) # Resolve Full Path, WinSCP.exe does not like dot sourced path for the Certificate. $sshPrivateKeyPathUsed = $PSBoundParameters.ContainsKey( "SshPrivateKeyPath" ) if ($sshPrivateKeyPathUsed) { $PSBoundParameters.SshPrivateKeyPath = Resolve-Path -Path $SshPrivateKeyPath | Select-Object -ExpandProperty Path } $tlsClientCertificatePathUsed = $PSBoundParameters.ContainsKey( "TlsClientCertificatePath" ) if ($tlsClientCertificatePathUsed) { $PSBoundParameters.TlsClientCertificatePath = Resolve-Path -Path $TlsClientCertificatePath | Select-Object -ExpandProperty Path } # Enumerate each parameter. try { $sessionOptionObjectProperties = $sessionOptions | Get-Member -MemberType Property | Select-Object -ExpandProperty Name $keys = ($PSBoundParameters.Keys).Where({ $_ -in $sessionOptionObjectProperties }) foreach ($key in $keys) { $sessionOptions.$key = $PSBoundParameters.$key } } catch { $PSCmdlet.ThrowTerminatingError( $_ ) } # Enumerate raw settings and add the options to the WinSCP.SessionOptions object. foreach ($key in $RawSetting.Keys) { $sessionOptions.AddRawSettings( $key, $RawSetting[$key] ) } Write-Output -InputObject $sessionOptions } } |