Advertisement
Guest User

WinSCPUpload.ps1

a guest
Aug 3rd, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. try
  2. {
  3.     # Load WinSCP .NET assembly
  4.     Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
  5.  
  6.     # Set up session options
  7.     $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  8.         Protocol = [WinSCP.Protocol]::Sftp
  9.         HostName = "ftp hostname here"
  10.         UserName = "ftp un here"
  11.         Password = "ftp pw here"
  12.         SshHostKeyFingerprint = "ssh fingerprint here"
  13.     }
  14.  
  15.     $sessionOptions.AddRawSettings("ProxyPort", "0")
  16.     $session = New-Object WinSCP.Session
  17.  
  18.      #Begin file upload to SFTP destination
  19.     try
  20.     {
  21.         # Connect
  22.         $session.Open($sessionOptions)
  23.  
  24.         # Transfer files
  25.         $session.PutFiles($File, "/Export/*").Check()
  26.         #move successfully transfered files to a Completed folder
  27.         Move-Item $File -Destination $Destination -Force    
  28.     }
  29.     finally #end Try
  30.     {
  31.         $session.Dispose()
  32.     }
  33.     exit 0
  34. }
  35. catch #end Try
  36. {
  37.     Write-Host "Error: $($_.Exception.Message)"
  38.     exit 1
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement