Guest User

Powershell FTP-Download Script

a guest
Sep 15th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Here are the variables to change to your needs
  2. # FTP server variables:
  3. $ftpPath = 'ftp://ftp.server.com/'
  4. $ftpUser = 'user'
  5. $ftpPass = 'password'
  6. $localPath = 'C:\install\app\'
  7.  
  8. # Email variables:
  9. $smtpServer = "192.168.254.254"
  10. $smtpFrom = "task1@domain.com"
  11. $smtpTo = "administrator@domain.com"
  12. $messageSubject = "APPNAME :: New Version downloaded"
  13.  
  14. # Fileserver share variables:
  15. $share = '\\server1\Applications\Appname'
  16. # end of changes
  17.  
  18.  
  19. # get the file listing of the FTP folder
  20. function Get-FtpDir ($url, $credentials)
  21. {
  22.   $request = [Net.FtpWebRequest]::Create($url)
  23.   if ($credentials) { $request.Credentials = $credentials }
  24.   $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
  25.   (New-Object IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() -split "`r`n"
  26. }
  27.  
  28.  
  29. # Needed for the download
  30. $webclient = New-Object System.Net.WebClient
  31. $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)  
  32. $webclient.BaseAddress = $ftpPath
  33.  
  34. Get-FTPDir $ftpPath $webclient.Credentials |
  35.   ? { $_ -like 'APPNAME*.exe' } |
  36.   % {
  37.       if (Test-Path ($localPath+$_)) { exit }
  38.       $webClient.DownloadFile($_, $localPath+$_)
  39.   }
  40.  
  41. # get the file listing of the FTP folder
  42. function Get-FtpDir2 ($url, $credentials)
  43. {
  44.   $request = [Net.FtpWebRequest]::Create($url)
  45.   if ($credentials) { $request.Credentials = $credentials }
  46.   $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
  47.   (New-Object IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() -split "`r`n"
  48. }
  49.  
  50.  
  51. # Needed for the download
  52. $webclient = New-Object System.Net.WebClient
  53. $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)  
  54. $webclient.BaseAddress = $ftpPath
  55.  
  56. Get-FTPDir2 $ftpPath $webclient.Credentials |
  57.   ? { $_ -like 'updates.txt' } |
  58.   % {
  59.       if (Test-Path ($localPath+$_)) { Remove-Item $localPath\updates.txt }
  60.       $webClient.DownloadFile($_, $localPath+$_)
  61.   }
  62.  
  63. # Remove oldest files but the three latest
  64. Get-ChildItem $localPath -filter *.exe |
  65. sort CreationTime -Descending |
  66. select -Skip 3 |
  67. Remove-Item
  68.  
  69. # Read filenames and version from downloaded files
  70. $fn = Split-Path "$localPath\*.*" -leaf -resolve
  71. $split = $fn.split()
  72.     $text1 = echo $split[0]
  73.     $text2 = echo $split[1]
  74.     $text3 = echo $split[2]
  75.  
  76. $body = "The following files have been downloaded: `n$text1 `n$text2 `n$text3"
  77.  
  78.  
  79. # Send Email
  80. Send-MailMessage -from $smtpfrom -to $smtpTo -Subject $messageSubject -Body $body -smtpServer $smtpserver
  81.  
  82.  
  83. # Read the Product Version of the latest downloaded MAX-PAC.exe
  84. $version = (Get-Item $localPath\$text1).VersionInfo.ProductVersion
  85.  
  86. # Test for the folder on the server share and if not exists create it
  87. $DirExists = Test-Path $share\v$version
  88. If ($DirExists -eq $True)
  89.     { exit }
  90.     else {
  91.  
  92.     # Copy the freshly downloaded files onto the share
  93.     Copy-Item "C:\install\app\*.exe" -Destination (New-Item $share\v$version -Type Directory)
  94.     } # End of Else
Add Comment
Please, Sign In to add comment