Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Here are the variables to change to your needs
- # FTP server variables:
- $ftpPath = 'ftp://ftp.server.com/'
- $ftpUser = 'user'
- $ftpPass = 'password'
- $localPath = 'C:\install\app\'
- # Email variables:
- $smtpServer = "192.168.254.254"
- $smtpFrom = "task1@domain.com"
- $smtpTo = "administrator@domain.com"
- $messageSubject = "APPNAME :: New Version downloaded"
- # Fileserver share variables:
- $share = '\\server1\Applications\Appname'
- # end of changes
- # get the file listing of the FTP folder
- function Get-FtpDir ($url, $credentials)
- {
- $request = [Net.FtpWebRequest]::Create($url)
- if ($credentials) { $request.Credentials = $credentials }
- $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
- (New-Object IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() -split "`r`n"
- }
- # Needed for the download
- $webclient = New-Object System.Net.WebClient
- $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)
- $webclient.BaseAddress = $ftpPath
- Get-FTPDir $ftpPath $webclient.Credentials |
- ? { $_ -like 'APPNAME*.exe' } |
- % {
- if (Test-Path ($localPath+$_)) { exit }
- $webClient.DownloadFile($_, $localPath+$_)
- }
- # get the file listing of the FTP folder
- function Get-FtpDir2 ($url, $credentials)
- {
- $request = [Net.FtpWebRequest]::Create($url)
- if ($credentials) { $request.Credentials = $credentials }
- $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
- (New-Object IO.StreamReader $request.GetResponse().GetResponseStream()).ReadToEnd() -split "`r`n"
- }
- # Needed for the download
- $webclient = New-Object System.Net.WebClient
- $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)
- $webclient.BaseAddress = $ftpPath
- Get-FTPDir2 $ftpPath $webclient.Credentials |
- ? { $_ -like 'updates.txt' } |
- % {
- if (Test-Path ($localPath+$_)) { Remove-Item $localPath\updates.txt }
- $webClient.DownloadFile($_, $localPath+$_)
- }
- # Remove oldest files but the three latest
- Get-ChildItem $localPath -filter *.exe |
- sort CreationTime -Descending |
- select -Skip 3 |
- Remove-Item
- # Read filenames and version from downloaded files
- $fn = Split-Path "$localPath\*.*" -leaf -resolve
- $split = $fn.split()
- $text1 = echo $split[0]
- $text2 = echo $split[1]
- $text3 = echo $split[2]
- $body = "The following files have been downloaded: `n$text1 `n$text2 `n$text3"
- # Send Email
- Send-MailMessage -from $smtpfrom -to $smtpTo -Subject $messageSubject -Body $body -smtpServer $smtpserver
- # Read the Product Version of the latest downloaded MAX-PAC.exe
- $version = (Get-Item $localPath\$text1).VersionInfo.ProductVersion
- # Test for the folder on the server share and if not exists create it
- $DirExists = Test-Path $share\v$version
- If ($DirExists -eq $True)
- { exit }
- else {
- # Copy the freshly downloaded files onto the share
- Copy-Item "C:\install\app\*.exe" -Destination (New-Item $share\v$version -Type Directory)
- } # End of Else
Add Comment
Please, Sign In to add comment