Differences
This shows you the differences between the selected revisions of the page.
| 2013-04-30 | 2013-04-30 | ||
| also lcd command (martin) | example (martin) | ||
| Line 43: | Line 43: | ||
| $session.GetFiles("/home/martinp/test.txt", "d:\") | $session.GetFiles("/home/martinp/test.txt", "d:\") | ||
| </code> | </code> | ||
| + | |||
| + | ===== Example ===== | ||
| + | Following script: | ||
| + | |||
| + | <code winscp> | ||
| + | # Automatically abort script on errors | ||
| + | option batch abort | ||
| + | # Disable overwrite confirmations that conflict with the previous | ||
| + | option confirm off | ||
| + | # Connect | ||
| + | open sftp://user:password@example.com -hostkey="ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
| + | # Change remote directory | ||
| + | cd /home/user | ||
| + | # Force binary mode transfer | ||
| + | option transfer binary | ||
| + | # Download file to the local directory d:\ | ||
| + | get examplefile.txt d:\ | ||
| + | # Disconnect | ||
| + | close | ||
| + | # Exit WinSCP | ||
| + | exit | ||
| + | </code> | ||
| + | |||
| + | maps to following PowerShell code: | ||
| + | |||
| + | <code powershell> | ||
| + | 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 = "password" | ||
| + | $sessionOptions.SshHostKeyFingerprint = "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) | ||
| + | |||
| + | # Force binary mode transfer | ||
| + | $transferOptions = New-Object WinSCP.TransferOptions | ||
| + | $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary | ||
| + | |||
| + | # Download file to the local directory d:\ | ||
| + | # Note use of absolute path | ||
| + | $transferResult = $session.GetFiles("/home/user/examplefile.txt", "d:\", $False, $transferOptions) | ||
| + | |||
| + | # Throw on any error to emulate "option batch abort" | ||
| + | $transferResult.Check() | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | # Disconnect, clean up | ||
| + | $session.Dispose() | ||
| + | } | ||
| + | |||
| + | exit 0 | ||
| + | } | ||
| + | catch [Exception] | ||
| + | { | ||
| + | Write-Host $_.Exception.Message | ||
| + | exit 1 | ||
| + | } | ||
| + | </code> | ||
| + | |||