Advertisement
Guest User

scripting

a guest
Nov 10th, 2016
8,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. function SFTP-Download {
  2.  
  3. [cmdletbinding()]
  4. param(
  5. [Parameter(Mandatory=$true)]
  6. [string]$sitehost,
  7. [Parameter(Mandatory=$true)]
  8. [string]$port,
  9. [Parameter(Mandatory=$true)]
  10. [string]$user,
  11. [Parameter(Mandatory=$true)]
  12. [string]$pass,
  13. [Parameter(Mandatory=$true)]
  14. [string]$local,
  15. [Parameter(Mandatory=$true)]
  16. [string]$remote,
  17. [Parameter(Mandatory=$true)]
  18. $winscpdll,
  19. [Parameter(Mandatory=$true)]
  20. [string]$sshkey
  21. )
  22.  
  23. process{
  24.  
  25. # Load WinSCP .NET assembly
  26. Add-Type -Path $winscpdll
  27.  
  28. # Setup session options
  29. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  30. Protocol = [WinSCP.Protocol]::Sftp
  31. PortNumber = "$port"
  32. HostName = "$sitehost"
  33. UserName = "$user"
  34. Password = "$pass"
  35. SshHostKeyFingerprint = "$sshkey"
  36. }
  37.  
  38. # Create Session
  39. $session = New-Object WinSCP.Session
  40.  
  41. # Connect
  42. $session.Open($sessionOptions)
  43.  
  44. # Find files and count
  45. $remotefiles = $session.EnumerateRemoteFiles($remote, "*.txt", [WinSCP.EnumerationOptions]::None)
  46. $remotefilescount = ($remotefiles.count).count
  47.  
  48. if ($remotefilescount -gt 0) {
  49.  
  50. Write-Host "$remotefilescount Files found" -ForegroundColor Green
  51.  
  52. # Download the file and throw on any error
  53. $downloadresult = $session.GetFiles(($remote),($local))
  54.  
  55. foreach ($download in $downloadresult) {
  56.  
  57. if ($download.IsSuccess) {
  58.  
  59. Write-Host "$($download.error)"
  60.  
  61. }
  62.  
  63. else {
  64.  
  65. Write-Host "$download download was not successful"
  66.  
  67. }
  68.  
  69. }
  70.  
  71.  
  72.  
  73. # Remove file
  74. #$removeresult = $session.RemoveFiles($remote)
  75.  
  76. #if ($removeresult.IsSuccess) {
  77. #Write-Host "The files were removed!"
  78. #}
  79. #else {
  80. #Write-Host "There was an issue removing"
  81. #}
  82.  
  83. # Disconnect session
  84. $session.dispose()
  85.  
  86. }
  87.  
  88. else {
  89.  
  90. Write-Host "There were no files found!" -ForegroundColor Red
  91.  
  92. }
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement