cosine83

WinSCP Synchronize

Apr 20th, 2015
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #WinSCP Synchronize
  2.  
  3. $Path = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WinSCP"
  4.  
  5. If (Test-Path $Path) {
  6. Write-Host "WinSCP PoSh module installed, loading it"
  7. Import-Module WinSCP
  8. Write-Host "WinSCP Module Loaded"
  9. }
  10. Else {
  11. Write-Host "WinSCP PoSh module not installed, installing it"
  12. & ((Split-Path $MyInvocation.InvocationName) + "\Install WinSCP Powershell Module.ps1")
  13. Import-Module WinSCP
  14. Write-Host "WinSCP Module Loaded"
  15. }
  16.  
  17. $SMTP = "mail.domain.com"
  18. $Date = Get-Date -format "MM.dd.yyy HH:mm"
  19.  
  20. $LocalFolder = "\\UNPath\c$\FolderToSync"
  21. $RemoteFolder = "./"
  22. $sessionOptions = @{
  23. HostName = "ServerNameOrIP"
  24. UserName = "UserName"
  25. Password = "Password"
  26. SshHostKeyFingerprint = "SSH-RSA Key"
  27. }
  28.  
  29. Try {
  30. Write-Host "Opening WinSCP session"
  31. $session = Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions @sessionOptions)
  32. Write-Host "Successfully connected, "
  33. Sync-WinSCPDirectory -WinSCPSession $session -RemotePath $RemoteFolder -LocalPath $LocalFolder -Mode Remote | Out-Null
  34. Send-MailMessage -SmtpServer $SMTP -From $From -To $To -Cc $CC -Subject "Results Sync Scheduled Task" -Body "Successfully synchronized results folder on $Date!"
  35. }
  36. Catch {
  37. Send-MailMessage -SmtpServer $SMTP -From $From -To $To -Cc $CC -Subject "Results Sync Scheduled Task" -Body "Failed to synchronize results folder on $Date!"
  38. }
Advertisement
Add Comment
Please, Sign In to add comment