Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ftpServer = "RandomFTPServer"
- $ftpUser = "Username"
- $ftpPassword = read-host "Password"-AsSecureString
- $credentials=New-ObjectSystem.Net.NetworkCredential($ftpUser,$ftpPassword)
- function Get-FtpRequest($ftpPath){
- $ftpRequest = [System.Net.FtpWebRequest]::create("$ftpServer/$ftpPath")
- $ftpRequest.Credentials = $credentials
- $ftpRequest.UseBinary = $true
- $ftpRequest.KeepAlive = $true
- $ftpRequest.UsePassive=$true
- return $ftpRequest
- }
- # Load WinSCP .NET assembly
- Add-Type -Path "WinSCPnet.dll"
- # Setup session options
- $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
- Protocol = [WinSCP.Protocol]::Ftp
- HostName = "ftp.example.com"
- UserName = "user"
- Password = "mypassword"
- }
- try
- {
- # Connect
- $session = New-Object WinSCP.Session
- $session.Open($sessionOptions)
- # List files
- $remotePath = "/remote/path"
- $directoryInfo = $session.ListDirectory($remotePath)
- # Find old files
- $limit = (Get-Date).AddDays(-15)
- $oldFiles =
- $directoryInfo.Files |
- Where-Object { -Not $_.IsDirectory } |
- Where-Object { $_.LastWriteTime -lt $limit }
- # Delete them
- foreach ($oldFileInfo in $oldFiles)
- {
- $session.RemoveFiles($session.EscapeFileMask($remotePath + "/" + $oldFileInfo.Name)).Check()
- }
- Write-Host "Done"
- }
- finally
- {
- # Disconnect, clean up
- $session.Dispose()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement