powershell_check_for_updates
This is an old revision of the document!
Powershell Check for Updates
#requires -version 3.0 #### Check for updates #### Version 1.5 #### Last updated 2014-01-02 #### The canonical version of this script is at http://tallguyracing.com/wiki/doku.php?id=powershell_check_for_updates $appsToCheck = @{ 'Dummy' = 'Dummy' ; 'TrueCrypt' = '7.1a' ; 'Anki' = '2.0.20' ; 'ConEmu' = '131225' ; 'Paint.NET' = '3.5.11' ; 'Rockbox' = '3.13' ; 'DokuWiki' = '2013-05-10' ; 'Notepad++' = '6.5.3' # Hopefully, this can be reverted to a fully automatic check in the future. # ; 'Notepad++' = 'Notepad++\notepad++.exe' ; 'GIMP' = 'GIMP 2\bin\gimp-2.8.exe' ; '7zip' = '7-Zip\7z.exe' ; 'WinMerge' = 'WinMerge\WinMergeU.exe' ; 'Foobar2000' = 'foobar2000\foobar2000.exe' ; 'Fiddler' = 'Fiddler2\fiddler.exe' ; 'Process Explorer' = 'ProcessExplorer\procexp.exe' ; 'Process Monitor' = 'ProcessMonitor\procmon.exe' ; 'VLC' = 'VideoLAN\VLC\vlc.exe' ; 'FileZilla' = 'FileZilla FTP Client\filezilla.exe' ; 'ImageMagick' = 'ImageMagick\mogrify.exe' ; 'TortoiseSVN' = 'TortoiseSVN\bin\TortoiseSVN.dll' ; 'WinDirStat' = 'WinDirStat\windirstat.exe' ; 'Expresso' = 'Ultrapico\Expresso\Expresso.exe' ; 'LibreOffice' = 'LibreOffice 4\program\soffice.exe' } function WebRequest ($url) { [net.httpwebrequest]$httpwebrequest = [net.webrequest]::create($url) [net.httpWebResponse]$httpwebresponse = $httpwebrequest.getResponse() if (!$httpwebresponse) { return $null } $reader = new-object IO.StreamReader($httpwebresponse.getResponseStream()) $WSResponse = $reader.ReadToEnd() $reader.Close() out-file -filepath C:\Temp\Debug.txt -inputobject $WSResponse return $WSResponse } function GetVersionNumberFromWeb ($appName, $url, $regexPattern) { $response = WebRequest $url if (!$response) { Write-Host "FAILED: ${appName}: Could not get the web page." -ForegroundColor Red return } $versionNumberMatches = ([regex]$regexPattern).matches($response) if ($versionNumberMatches.Count -eq 0) { Write-Host "FAILED: ${appName}: Could not get version number from the web page. Nothing matched the regex." -ForegroundColor Red return } if ($versionNumberMatches.Count -ne 1) { Write-Host "FAILED: ${appName}: Could not get version number from the web page. The regex had more than one match." -ForegroundColor Red return } if (! $versionNumberMatches[0].Success) { Write-Host "FAILED: ${appName}: Could not get version number from the web page. The regex match was not successful." -ForegroundColor Red return } if (! $versionNumberMatches[0].Groups["versionNumber"].Success) { Write-Host "FAILED: ${appName}: Could not get version number from the web page. The regex group was not successful." -ForegroundColor Red return } $latestVersionNumber = $versionNumberMatches[0].Groups["versionNumber"].Value Write-Host "${appName}: The latest version number is $LatestVersionNumber." return $latestVersionNumber } function CheckLocalFile ($appName, $url, $regexPattern, $localFilename, $extraFormatting) { if (Test-Path $localFilename) { $fullLocalFilename = $localFilename } elseif (Test-Path "C:\Program Files (x86)\$localFilename") { $fullLocalFilename = "C:\Program Files (x86)\$localFilename" } elseif (Test-Path "C:\Program Files\$localFilename") { $fullLocalFilename = "C:\Program Files\$localFilename" } else { Write-Host "FAILED: The filename ${localFilename} or any of its variants does not exist." -ForegroundColor Red return } $appInfo = Get-Command $fullLocalFilename $currentVersionNumber = $appInfo.FileVersionInfo.ProductVersion if ($extraFormatting.Contains(';CommaToFullStop;')) { $currentVersionNumber = $currentVersionNumber.Replace(',', '.') } if ($extraFormatting.Contains(';RemoveSpaces;')) { $currentVersionNumber = $currentVersionNumber.Replace(' ', '') } if ($extraFormatting.Contains(';TrimTrailingDotZero;') -and $currentVersionNumber.EndsWith('.0')) { $currentVersionNumber = $currentVersionNumber.Substring(0, $currentVersionNumber.Length - 2) } while ($extraFormatting.Contains(';TrimTrailingDotZeros;') -and $currentVersionNumber.EndsWith('.0')) { $currentVersionNumber = $currentVersionNumber.Substring(0, $currentVersionNumber.Length - 2) } if ($extraFormatting.Contains(';FirstThreeDigitsOnly;')) { if (! ($currentVersionNumber -match "\d+(?:\.\d+){2}")) { Write-Host "FAILED: ${appName}: Could not get the first three digits of the version number from '$currentVersionNumber'." -ForegroundColor Red return } $currentVersionNumber = $Matches.Values } CheckCurrentVersionNumber $appName $url $regexPattern $currentVersionNumber } function CheckCurrentVersionNumber ($appName, $url, $regexPattern, $currentVersionNumber) { $latestVersionNumber = GetVersionNumberFromWeb $appName $url $regexPattern if (! $latestVersionNumber) { return } Write-Host "${appName}: The current version number is $currentVersionNumber." if ($currentVersionNumber -ne $latestVersionNumber) { Write-Host "${appName}: Update required!" -ForegroundColor Green } else { Write-Host "${appName}: No update required." } } function CheckAppCurrentVersionNumber ($appName) { switch ($appName) { 'Dummy' { } #### Semi-automatic checks #### 'TrueCrypt' { CheckCurrentVersionNumber 'TrueCrypt' 'http://www.truecrypt.org/downloads' ` '\>Latest Stable Version - (?<versionNumber>\d+\.\d+\w+)\<' ` $appsToCheck['TrueCrypt'] } 'Anki' { CheckCurrentVersionNumber 'Anki' 'http://ankisrs.net/' ` 'href="http://ankisrs\.net/download/mirror/anki-(?<versionNumber>\d+(?:\.\d+){2})\.exe"' ` $appsToCheck['Anki'] } 'ConEmu' { CheckCurrentVersionNumber 'ConEmu' 'http://conemu.codeplex.com/' ` 'ConEmu (?<versionNumber>\d+\w+) \[Alpha\]' ` $appsToCheck['ConEmu'] } 'Paint.NET' { CheckCurrentVersionNumber 'Paint.NET' 'http://www.getpaint.net/index.html' ` '>Paint\.NET\s*v(?<versionNumber>\d+(?:\.\d+){2})<' ` $appsToCheck['Paint.NET'] } 'Rockbox' { CheckCurrentVersionNumber 'Rockbox' 'http://www.rockbox.org/download/' ` '<h1>Rockbox (?<versionNumber>\d+(?:\.\d+){1,2}) Download</h1>' ` $appsToCheck['Rockbox'] } 'DokuWiki' { CheckCurrentVersionNumber 'DokuWiki' 'http://download.dokuwiki.org/' ` '<a href="src/dokuwiki/dokuwiki-stable\.tgz">\(direct link\)</a>\s*<span class="hint">(?<versionNumber>\d{4}-\d{2}-\d{2}).+?</span>' ` $appsToCheck['DokuWiki'] } 'Notepad++' { CheckCurrentVersionNumber 'Notepad++' 'http://notepad-plus-plus.org/download/' ` '<title>Notepad\+\+ v(?<versionNumber>\d+(?:\.\d+){1,2}) - Current Version</title>' ` $appsToCheck['Notepad++'] } #### Fully-automatic checks #### 'GIMP' { CheckLocalFile 'GIMP' 'http://www.gimp.org/downloads/' ` 'href="http://ftp\.gimp\.org/pub/gimp/[^/]+/windows/gimp-(?<versionNumber>\d+(?:\.\d+){2})-setup.exe"' ` $appsToCheck['GIMP'] '' } '7zip' { CheckLocalFile '7zip' 'http://www.7-zip.org/' ` 'Download 7-Zip (?<versionNumber>\d+(?:\.\d+){1,2}) \(\d+-\d+-\d+\) for Windows' ` $appsToCheck['7zip'] '' } 'WinMerge' { CheckLocalFile 'WinMerge' 'http://winmerge.org/' ` 'href="http://downloads.sourceforge.net/winmerge/WinMerge-(?<versionNumber>\d+(?:\.\d+){2})-Setup.exe"' ` $appsToCheck['WinMerge'] '' } 'Foobar2000' { CheckLocalFile 'Foobar2000' 'http://www.foobar2000.org/download' ` 'href="/getfile/[^/]+/foobar2000_v(?<versionNumber>\d+(?:\.\d+){1,2}).exe">foobar2000 v\d+(?:\.\d+){1,2}' ` $appsToCheck['Foobar2000'] ` ';TrimTrailingDotZeros;' } 'Fiddler' { CheckLocalFile 'Fiddler' 'http://fiddler2.com/get-fiddler' ` '\<p id="Content_C014_ctl00_ctl00_BottomContentTag"\>Version (?<versionNumber>\d+(?:\.\d+){2,3})\</p\>' ` $appsToCheck['Fiddler'] '' } 'Process Explorer' { CheckLocalFile 'Process Explorer' 'http://technet.microsoft.com/en-US/sysinternals/bb896653' ` '<h1>Process Explorer v(?<versionNumber>\d+\.\d+)</h1>' ` $appsToCheck['Process Explorer'] '' } 'Process Monitor' { CheckLocalFile 'Process Monitor' 'http://technet.microsoft.com/en-US/sysinternals/bb896645' ` '<h1>Process Monitor v(?<versionNumber>\d+\.\d+)</h1>' ` $appsToCheck['Process Monitor'] '' } # Next time the version changes, try using this rather than the Semi-automatic check. # 'Notepad++' # { # CheckLocalFile 'Notepad++' 'http://notepad-plus-plus.org/download/' ` # '<title>Notepad\+\+ v(?<versionNumber>\d+(?:\.\d+){1,2}) - Current Version</title>' ` # $appsToCheck['Notepad++'] '' # } 'VLC' { CheckLocalFile 'VLC' 'http://videolan.org/vlc/' ` 'href=''//get.videolan.org/vlc/(?:\d+(?:\.\d+){2})/win32/vlc-(?<versionNumber>\d+(?:\.\d+){2})-win32.exe''>' ` $appsToCheck['VLC'] ` ';CommaToFullStop;TrimTrailingDotZero;' } 'FileZilla' { CheckLocalFile 'FileZilla' 'http://filezilla-project.org/download.php?type=client' ` '>The latest stable version of FileZilla Client is (?<versionNumber>\d+(?:\.\d+){2})<' ` $appsToCheck['FileZilla'] ` ';CommaToFullStop;RemoveSpaces;TrimTrailingDotZero;' } 'ImageMagick' { CheckLocalFile 'ImageMagick' 'http://www.imagemagick.org/script/binary-releases.php' ` '>ImageMagick-(?<versionNumber>\d+(?:\.\d+){2})(?:-\d+)?-Q16-x64-dll\.exe<' ` $appsToCheck['ImageMagick'] '' } 'TortoiseSVN' { CheckLocalFile 'TortoiseSVN' 'http://tortoisesvn.net/downloads.html' ` '>The current version is (?<versionNumber>\d+(?:\.\d+){2})<' ` $appsToCheck['TortoiseSVN'] ` ';FirstThreeDigitsOnly;' } 'WinDirStat' { CheckLocalFile 'WinDirStat' 'http://windirstat.info/' ` '>Latest version: (?<versionNumber>\d+(?:\.\d+){2})<' ` $appsToCheck['WinDirStat'] ` ';FirstThreeDigitsOnly;' } 'WinDirStat' { CheckLocalFile 'WinDirStat' 'http://windirstat.info/' ` '>Latest version: (?<versionNumber>\d+(?:\.\d+){2})<' ` $appsToCheck['WinDirStat'] ` ';FirstThreeDigitsOnly;' } 'Expresso' { CheckLocalFile 'Expresso' 'http://www.ultrapico.com/ExpressoDownload.htm' ` '\(Version (?<versionNumber>\d+(?:\.\d+){2}) - ' ` $appsToCheck['Expresso'] ` ';FirstThreeDigitsOnly;' } 'LibreOffice' { CheckLocalFile 'LibreOffice' 'http://www.libreoffice.org/download' ` 'Windows, version (?<versionNumber>\d+(?:\.\d+){2}), English' ` $appsToCheck['LibreOffice'] ` ';FirstThreeDigitsOnly;' } default { Write-Host "FAILED: ${appName}: Unknown app." -ForegroundColor Red } } } foreach ($appName in $AppsToCheck.Keys) { CheckAppCurrentVersionNumber ($appName) } #CheckAppCurrentVersionNumber 'Foobar2000' Write-Host "Press any key to exit." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
powershell_check_for_updates.1388633579.txt.gz · Last modified: 2017/01/01 19:51 (external edit)