Topic "Connect to multiple servers in one powershell script"

Author Message
GRR3
[View user's profile]

Joined: 2017-05-31
Posts: 2
I'm learning powershell and also am new to OOP.

If I wanted to download from one SFTP server and upload to a second server in the same script, would this be the general idea (below)?
Is there a better way to do this?

Thank you.

Code:

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $session = New-Object WinSCP.Session
 
     $sessionOptions_second_server = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example2.com"
        UserName = "user2"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    }
 
    $session_second_server = New-Object WinSCP.Session
   
    try
    {
      # Connect to download from server 1
        $session.Open($sessionOptions)
 
        # Download files
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
        $transferResult = $session.GetFiles("/home/user/*", "d:\download\", $False, $transferOptions)
 
        # Throw on any error
        $transferResult.Check()
 
        # Print results
      $files_downloaded = 0
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Download of {0} succeeded" -f $transfer.FileName)
         $files_downloaded++
        }
   
      if ($files_downloaded)
      {   
         # Connect to upload to server 2
         $session_second_server.Open($sessionOptions_second_server)
   
         # Upload files
         $transferOptions_second_server = New-Object WinSCP.TransferOptions
         $transferOptions_second_server.TransferMode = [WinSCP.TransferMode]::Binary
   
         $transferResult_second_server = $session_second_server.PutFiles("d:\download\*", "/home/user/", $False, $transferOptions_second_server)
   
         # Throw on any error
         $transferResult_second_server.Check()
   
         # Print results
         foreach ($transfer in $transferResult.Transfers)
         {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
         }
      }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
      $session_second_server.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26462
Location: Prague, Czechia
Looks good.

Just that you should close the first session - $session.Dispose(), as soon as you do not need it - just after the download.
Advertisements

You can post new topics in this forum






Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License