Differences

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

2012-03-26 2012-03-27
vb.net signature (martin) powershell example (martin)
Line 135: Line 135:
        }         }
    }     }
 +}
 +</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)
 +
 +        $stamp = $(Get-Date -f yyyyMMdd)
 +        $fileName = "export_$stamp.txt"
 +        $remotePath = "/home/user/sysbatch/$fileName"
 +        $localPath = "d:\backup\$fileName"
 +
 +        # Manual "remote to local" synchronization.
 +
 +        # You can achieve the same using:
 +        # $transferOptions = New-Object WinSCP.TransferOptions
 +        # $transferOptions.IncludeMask = $fileName
 +        # $session.SynchronizeDirectories(
 +        #    [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath,
 +        #    $FALSE, $FALSE, [WinSCP.SynchronizationCriteria]::Time,
 +        #    $transferOptions).Check()
 +        if ($session.FileExists($remotePath))
 +        {
 +            if (!(Test-Path $localPath))
 +            {
 +                Write-Host
 +                    ("File {0} exists, local backup {1} does not" `
 +                    -f $remotePath, $localPath)
 +                $download = $TRUE
 +            }
 +            else
 +            {
 +                $remoteWriteTime = $session.GetFileInfo($remotePath).LastWriteTime
 +                $localWriteTime = (Get-Item $localPath).LastWriteTime
 +
 +                if ($remoteWriteTime -gt $localWriteTime)
 +                {
 +                    Write-Host (
 +                        ("File {0} as well as local backup {1} exist, " + `
 +                        "but remote file is newer ({2}) than local backup ({3})") `
 +                        -f $remotePath, $localPath, $remoteWriteTime, $localWriteTime)
 +                    $download = $TRUE
 +                }
 +                else
 +                {
 +                    Write-Host (
 +                        ("File {0} as well as local backup {1} exist, " + `
 +                        "but remote file is not newer ({2}) than local backup ({3})") `
 +                        -f $remotePath, $localPath, $remoteWriteTime, $localWriteTime)
 +                    $download = $FALSE
 +                }
 +            }
 +
 +            if ($download)
 +            {
 +                # Download the file and throw on any error
 +                $session.GetFiles($remotePath, $localPath).Check()
 +
 +                Write-Host "Download to backup done."
 +            }
 +        }
 +        else
 +        {
 +            Write-Host ("File {0} does not exist yet" -f $remotePath)
 +        }
 +    }
 +    finally
 +    {
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
 +
 +
 +    exit 0
 +}
 +catch [System.Exception]
 +{
 +    Write-Host $_.Exception.Message
 +    exit 1
} }
</code> </code>

Last modified: by martin