Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. $ftpServer = "RandomFTPServer"
  2. $ftpUser = "Username"
  3. $ftpPassword = read-host "Password"-AsSecureString
  4.  
  5. $credentials=New-ObjectSystem.Net.NetworkCredential($ftpUser,$ftpPassword)
  6.  
  7. function Get-FtpRequest($ftpPath){
  8. $ftpRequest = [System.Net.FtpWebRequest]::create("$ftpServer/$ftpPath")
  9. $ftpRequest.Credentials = $credentials
  10. $ftpRequest.UseBinary = $true
  11. $ftpRequest.KeepAlive = $true
  12. $ftpRequest.UsePassive=$true
  13. return $ftpRequest
  14. }
  15.  
  16. # Load WinSCP .NET assembly
  17. Add-Type -Path "WinSCPnet.dll"
  18.  
  19. # Setup session options
  20. $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
  21. Protocol = [WinSCP.Protocol]::Ftp
  22. HostName = "ftp.example.com"
  23. UserName = "user"
  24. Password = "mypassword"
  25. }
  26.  
  27. try
  28. {
  29. # Connect
  30. $session = New-Object WinSCP.Session
  31. $session.Open($sessionOptions)
  32.  
  33. # List files
  34. $remotePath = "/remote/path"
  35. $directoryInfo = $session.ListDirectory($remotePath)
  36.  
  37. # Find old files
  38. $limit = (Get-Date).AddDays(-15)
  39.  
  40. $oldFiles =
  41. $directoryInfo.Files |
  42. Where-Object { -Not $_.IsDirectory } |
  43. Where-Object { $_.LastWriteTime -lt $limit }
  44.  
  45. # Delete them
  46. foreach ($oldFileInfo in $oldFiles)
  47. {
  48. $session.RemoveFiles($session.EscapeFileMask($remotePath + "/" + $oldFileInfo.Name)).Check()
  49. }
  50.  
  51. Write-Host "Done"
  52. }
  53. finally
  54. {
  55. # Disconnect, clean up
  56. $session.Dispose()
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement