Differences
This shows you the differences between the selected revisions of the page.
2015-08-24 | 2015-08-24 | ||
powershell try can have both catch and finally (martin) (hidden) | undoing, we actually wanted the powershell code to be as close to c# other examples as possible (martin) (hidden) | ||
Line 131: | Line 131: | ||
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
- | # Connect | + | try |
- | ···$session.Open($sessionOptions) | + | { |
+ | ········# Connect | ||
+ | ·······$session.Open($sessionOptions) | ||
- | ····# Upload files | + | ········# Upload files |
- | ···$transferOptions = New-Object WinSCP.TransferOptions | + | ·······$transferOptions = New-Object WinSCP.TransferOptions |
- | ···$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary | + | ·······$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary |
- | ····$transferResult = $session.PutFiles("b:\toupload\*", "/home/user/", $False, $transferOptions) | + | ········$transferResult = $session.PutFiles("b:\toupload\*", "/home/user/", $False, $transferOptions) |
- | ····# Throw on any error | + | ········# Throw on any error |
- | ···$transferResult.Check() | + | ·······$transferResult.Check() |
- | ····# Print results | + | ········# Print results |
- | ···foreach ($transfer in $transferResult.Transfers) | + | ·······foreach ($transfer in $transferResult.Transfers) |
+ | { | ||
+ | Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) | ||
+ | } | ||
+ | } | ||
+ | finally | ||
{ | { | ||
- | Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) | + | # Disconnect, clean up |
+ | ·······$session.Dispose() | ||
} | } | ||
+ | |||
exit 0 | exit 0 | ||
} | } | ||
Line 154: | Line 163: | ||
Write-Host $_.Exception.Message | Write-Host $_.Exception.Message | ||
exit 1 | exit 1 | ||
- | } | ||
- | finally | ||
- | { | ||
- | if ($session -ne $Null) | ||
- | { | ||
- | # Disconnect, clean up | ||
- | $session.Dispose() | ||
- | } | ||
} | } | ||
</code> | </code> |