Differences

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

2012-03-27 2012-04-03
examples (martin) powershell example (martin)
Line 117: Line 117:
</code> </code>
 +==== [[powershell]] PowerShell Example ====
 +<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 = "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)
 +
 +        $directory = $session.ListDirectory("/home/martin/public_html")
 +
 +        foreach ($fileInfo in $directory.Files)
 +        {
 +            Write-Host ("{0} with size {1}, permissions {2} and last modification at {3}" -f `
 +                $fileInfo.Name, $fileInfo.Length, $fileInfo.FilePermissions, $fileInfo.LastWriteTime)
 +        }
 +    }
 +    finally
 +    {
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
 +
 +    exit 0
 +}
 +catch [Exception]
 +{
 +    Write-Host $_.Exception.Message
 +    exit 1
 +}
 +</code>

Last modified: by martin