Differences
This shows you the differences between the selected revisions of the page.
2012-03-21 | 2012-03-23 | ||
space after catch (martin) | powershell example (martin) | ||
Line 153: | Line 153: | ||
End Class | End Class | ||
+ | </code> | ||
+ | |||
+ | ==== [[powershell]] PowerShell Example ==== | ||
+ | <code> | ||
+ | try | ||
+ | { | ||
+ | # Load WinSCP .NET assembly | ||
+ | [Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null | ||
+ | |||
+ | # Setup session options | ||
+ | $sessionOptions = New-Object WinSCP.SessionOptions | ||
+ | $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | ||
+ | $sessionOptions.HostName = "example.com" | ||
+ | $sessionOptions.UserName = "user" | ||
+ | $sessionOptions.Password = "mypassword" | ||
+ | $sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
+ | |||
+ | $session = New-Object WinSCP.Session | ||
+ | |||
+ | try | ||
+ | { | ||
+ | # Connect | ||
+ | $session.Open($sessionOptions) | ||
+ | |||
+ | # Upload files | ||
+ | $transferOptions = New-Object WinSCP.TransferOptions | ||
+ | $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary; | ||
+ | |||
+ | $transferResult = $session.PutFiles("b:\toupload\*", "./", $FALSE, $transferOptions) | ||
+ | |||
+ | # Throw on any error | ||
+ | $transferResult.Check() | ||
+ | |||
+ | # Print results | ||
+ | foreach ($transfer in $transferResult.Transfers) | ||
+ | { | ||
+ | Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) | ||
+ | } | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | # Disconnect, clean up | ||
+ | $session.Dispose() | ||
+ | } | ||
+ | |||
+ | exit 0 | ||
+ | } | ||
+ | catch [System.Exception] | ||
+ | { | ||
+ | Write-Host $_.Exception.Message | ||
+ | exit 1 | ||
+ | } | ||
</code> | </code> | ||