Differences

This shows you the differences between the selected revisions of the page.

2015-07-10 2015-08-24
Update examples to refactor of module version 5.7.4.1 (dotps1) powershell try can have both catch and finally (martin) (hidden)
Line 131: Line 131:
    $session = New-Object WinSCP.Session     $session = New-Object WinSCP.Session
-    try +    # Connect 
-    { + ···$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+
    {     {
-        # Disconnect, clean up +        Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
- ·······$session.Dispose()+
    }     }
- 
    exit 0     exit 0
} }
Line 163: Line 154:
    Write-Host $_.Exception.Message     Write-Host $_.Exception.Message
    exit 1     exit 1
 +}
 +finally
 +{
 +    if ($session -ne $Null)
 +    {
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
} }
</code> </code>

Last modified: by martin