Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #### Modify the following values as necessary
  2. $url = "https://files.fm/down.php?i=2xyfrrqq&n=test.zip"
  3. $outfile = "C:\tmp\test2.zip"
  4. $extractpath = "C:\tmp\extracted"
  5. $versionfile = "version.txt"
  6. $installpath = "C:\tmp\installed"
  7. $startfile = "C:\tmp\installed\application.exe"
  8. ####
  9.  
  10. ### Leave these values as is
  11. $oldversion = "0.00"
  12. $newversion = "0.01"
  13. $arg = ("e "+$outfile+" -o"+$extractpath)
  14.  
  15. function DownloadFile($url, $targetFile)
  16. {
  17.    if (Test-Path $outfile) {
  18.     remove-item $outfile -force
  19.    }
  20.    $timeA = get-date
  21.    $uri = New-Object "System.Uri" "$url"
  22.    $request = [System.Net.HttpWebRequest]::Create($uri)
  23.    $request.set_Timeout(15000) #15 second timeout
  24.    $response = $request.GetResponse()
  25.    $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
  26.    $responseStream = $response.GetResponseStream()
  27.    $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
  28.    $buffer = new-object byte[] 10KB
  29.    $count = $responseStream.Read($buffer,0,$buffer.length)
  30.    $downloadedBytes = $count
  31.    while ($count -gt 0)
  32.    {
  33.        $targetStream.Write($buffer, 0, $count)
  34.        $count = $responseStream.Read($buffer,0,$buffer.length)
  35.        $downloadedBytes = $downloadedBytes + $count
  36.        Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength)  * 100)
  37.    }
  38.    $timeB = get-date ; $timeC = new-timespan $timeA $timeB ; $timeD = ($timeC.minutes)*60 + ($timeC.seconds)
  39.    $filesize = (get-item $targetFile).length/1KB ; $avgspd = [math]::round(($filesize / $timeD),1)
  40.    Write-Output "Downloaded '$($url.split('/') | Select -Last 1)' in $timeD seconds ($avgspd KB/s avg)"
  41.    $targetStream.Flush() ; $targetStream.Close() ; $targetStream.Dispose() ; $responseStream.Dispose()
  42. }
  43.  
  44. function RemoveLineCarriage($object)
  45. {
  46.     $result = [System.String] $object;
  47.     $result = $result -replace "`t","";
  48.     $result = $result -replace "`n","";
  49.     $result = $result -replace "`r","";
  50.     $result = $result -replace " ;",";";
  51.     $result = $result -replace "; ",";";
  52.    
  53.     $result = $result -replace [Environment]::NewLine, "";
  54.    
  55.     $result;
  56. }
  57.  
  58. DownloadFile $url $outfile
  59. if (Test-Path $extractpath) {
  60.   remove-item $extractpath -force -recurse
  61. }
  62. start-process "c:\program files\7-zip\7z.exe" $arg -wait
  63. remove-item $outfile -force ; write-output "Deleted file: $outfile"
  64. $oldversion = (get-content ($installpath + "\" + $versionfile) | out-string)
  65. $newversion = (get-content ($extractpath + "\" + $versionfile) | out-string)
  66. $oldversion = RemoveLineCarriage($oldversion)
  67. $newversion = RemoveLineCarriage($newversion)
  68. write-output ("Existing version is $oldversion | New version is: $newversion")
  69. if ($oldversion -eq $newversion) {
  70.   write-output "Version match, retaining existing files..."
  71.     if (Test-Path $extractpath) {
  72.         remove-item $extractpath -force -recurse
  73.         write-output "Extracted files have been deleted"
  74.     }
  75.   }
  76. else {
  77.   write-output "Version mismatch"
  78.     if (Test-Path $installpath) {
  79.         remove-item $installpath -force -recurse
  80.         write-output "Installed files have been deleted"
  81.     }
  82.   write-output "Moving files from $extractpath TO $installpath"
  83.   move-item $extractpath $installpath -force
  84.   }
  85.  
  86. write-output (""+"Running: $startfile")
  87. start-process $startfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement