using_ps_code.ps1 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Set configuration variables
  2. $remote_directory = "/data2/backups/bethany/{0}/Profiles/" -f ( $env:COMPUTERNAME )
  3. $local_directory = "c:\Users"
  4. $excludes = @(
  5. "*/Users/Default*/",
  6. "*/Users/All Users/",
  7. "*/Users/desktop.ini",
  8. "*/AppData/"
  9. )
  10. $transfermask = "| " + ($excludes -join ";")
  11. # Load WinSCP .NET assembly
  12. Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
  13. # Set up session options
  14. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  15. Protocol = [WinSCP.Protocol]::Sftp
  16. HostName = "gandalf"
  17. UserName = "bethany"
  18. Password = "haze7pie"
  19. SshHostKeyFingerprint = "ssh-ed25519 256 55:2d:b5:25:58:19:5d:cd:14:39:36:ec:c9:7c:ee:08"
  20. }
  21. $session = New-Object WinSCP.Session
  22. try
  23. {
  24. # Connect
  25. $session.Open($sessionOptions)
  26. # Your code
  27. $transferOptions = New-Object WinSCP.TransferOptions
  28. $transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
  29. $transferOptions.FileMask = $transfermask
  30. $transferOptions.FilePermissions = [WinSCP.FilePermissions](432)
  31. # Synchronize files
  32. $synchronizationResult = $session.SynchronizeDirectories(
  33. [WinSCP.SynchronizationMode]::Remote,
  34. $local_directory,
  35. $remote_directory,
  36. $True, #removeFiles
  37. $True, #mirror
  38. $transferOptions)
  39. # Throw on any error
  40. $synchronizationResult.Check()
  41. }
  42. finally
  43. {
  44. $session.Dispose()
  45. }