Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. # Set the credentials
  2. $Password = ConvertTo-SecureString 'Password1' -AsPlainText -Force
  3. $Credential = New-Object System.Management.Automation.PSCredential ('root', $Password)
  4.  
  5. # Set local file path, SFTP path, and the backup location path which I assume is an SMB path
  6. $FilePath = "C:FileDumptest.txt"
  7. $SftpPath = '/Outbox'
  8. $SmbPath = '\filer01Backup'
  9.  
  10. # Set the IP of the SFTP server
  11. $SftpIp = '10.209.26.105'
  12.  
  13. # Load the Posh-SSH module
  14. Import-Module C:TempPosh-SSH
  15.  
  16. # Establish the SFTP connection
  17. New-SFTPSession -ComputerName $SftpIp -Credential $Credential
  18.  
  19. # Upload the file to the SFTP path
  20. Set-SFTPFile -SessionId 0 -LocalFile $FilePath -RemotePath $SftpPath
  21.  
  22. # Disconnect SFTP session
  23. (Get-SFTPSession -SessionId 0).Disconnect()
  24.  
  25. # Copy the file to the SMB location
  26. Copy-Item -Path $FilePath -Destination $SmbPath
  27.  
  28. # Load WinSCP .NET assembly
  29. Add-Type -Path "WinSCPnet.dll"
  30.  
  31. # Setup session options
  32. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  33. Protocol = [WinSCP.Protocol]::Sftp
  34. HostName = "example.com"
  35. UserName = "user"
  36. Password = "mypassword"
  37. SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
  38. }
  39.  
  40. $session = New-Object WinSCP.Session
  41.  
  42. try
  43. {
  44. # Connect
  45. $session.Open($sessionOptions)
  46.  
  47. # Upload
  48. $session.PutFiles("C:FileDumpexport.txt", "/Outbox/").Check()
  49. }
  50. finally
  51. {
  52. # Disconnect, clean up
  53. $session.Dispose()
  54. }
  55.  
  56. pscp -sftp -pw passwd c:filedump* user@host:/Outbox/
  57. mv c:filedump* c:backup*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement