Connect to multiple servers in one powershell script

Advertisement

GRR3
Joined:
Posts:
10

Connect to multiple servers in one powershell script

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.

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
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: Connect to multiple servers in one powershell script

Looks good.

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

Reply with quote

Advertisement

You can post new topics in this forum