I’m a big fan of automation and always installing the latest available version of any software. It takes some time to get going, but after that, each and every time my pipeline build a new Master Image its 100% up to date.
I have a huge library of prescripted applications you can steal with pride over at my Github repo. That repo is step one before they make it into the Evergreen PowerShell Gallery (Works on Windows, MacOS and Linux).
In this post I’m going to show you how we can use PowerShell Web Scraping when API isn’t available. In this example we’re going to use TightVNC. Taking a look at their web site we can see that the heading for new releases are constant “TightVNC for Windows“.
So by using the following code we can grab that heading.
1 2 3 4 |
$uri = "https://www.tightvnc.com/download.php" $web = wget -UseBasicParsing -Uri $uri $m = $web.ToString() -split "[`r`n]" | Select-String "Download TightVNC for Windows" | Select-Object -First 1 write-host $m |
As you can see we have some <h3> tags that we need to get ride off.
1 2 |
$m = $m -replace "<((?!@).)*?>" write-host $m |
Now let’s remove the heading we used for search.
1 2 |
$m = $m.Replace('Download TightVNC for Windows (Version','') write-host $m |
Now let’s remove that ) character at the end.
1 2 |
$m = $m.Replace(')','') write-host $m |
The final step before we wrap up is removing the white spaces.
1 2 3 |
$m = $m.Replace(' ','') $Version = $m write-host $Version |
When we download the software the URL looks like this: https://www.tightvnc.com/download/2.8.27/tightvnc-2.8.27-gpl-setup-64bit.msi
Clearly we can easily replace that version number with $Version and boom we have made our self an Evergreen script for TightVNC. Make sure to download from Github, the code gets screwed in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
function Get-TightVNCVersion { [cmdletbinding()] [outputtype([string])] $uri = "https://www.tightvnc.com/download.php" $web = wget -UseBasicParsing -Uri $uri $m = $web.ToString() -split "[`r`n]" | Select-String "Download TightVNC for Windows" | Select-Object -First 1 $m = $m -replace "<((?!@).)*?>" $m = $m.Replace('Download TightVNC for Windows (Version','') $m = $m.Replace(')','') $m = $m.Replace(' ','') $Version = $m Write-Output $Version } # PowerShell Wrapper for MDT, Standalone and Chocolatey Installation - (C)2015 xenappblog.com # Example 1: Start-Process "XenDesktopServerSetup.exe" -ArgumentList $unattendedArgs -Wait -Passthru # Example 2 Powershell: Start-Process powershell.exe -ExecutionPolicy bypass -file $Destination # Example 3 EXE (Always use ' '): # $UnattendedArgs='/qn' # (Start-Process "$PackageName.$InstallerType" $UnattendedArgs -Wait -Passthru).ExitCode # Example 4 MSI (Always use " "): # $UnattendedArgs = "/i $PackageName.$InstallerType ALLUSERS=1 /qn /liewa $LogApp" # (Start-Process msiexec.exe -ArgumentList $UnattendedArgs -Wait -Passthru).ExitCode Clear-Host Write-Verbose "Setting Arguments" -Verbose $StartDTM = (Get-Date) Write-Verbose "Installing Modules" -Verbose if (!(Test-Path -Path "C:\Program Files\PackageManagement\ProviderAssemblies\nuget")) {Find-PackageProvider -Name 'Nuget' -ForceBootstrap -IncludeDependencies} if (!(Get-Module -ListAvailable -Name Evergreen)) {Install-Module Evergreen -Force | Import-Module Evergreen} Update-Module Evergreen $Vendor = "Misc" $Product = "TightVNC" $PackageName = "TightVNC-win64" #$Evergreen = Get-NotepadPlusPlus | Where-Object {$_.Architecture -eq "x64"} $Version = $(Get-TightVNCVersion) $URL = "https://www.tightvnc.com/download/$Version/tightvnc-$Version-gpl-setup-64bit.msi" $InstallerType = "msi" $Source = "$PackageName" + "." + "$InstallerType" $LogPS = "${env:SystemRoot}" + "\Temp\$Vendor $Product $Version PS Wrapper.log" $LogApp = "${env:SystemRoot}" + "\Temp\$PackageName.log" $Destination = "${env:ChocoRepository}" + "\$Vendor\$Product\$Version\$packageName.$installerType" $ProgressPreference = 'SilentlyContinue' $UnattendedArgs = "/i $PackageName.$InstallerType ALLUSERS=1 /qn /liewa $LogApp" Start-Transcript $LogPS | Out-Null If (!(Test-Path -Path $Version)) {New-Item -ItemType directory -Path $Version | Out-Null} CD $Version Write-Verbose "Downloading $Vendor $Product $Version" -Verbose If (!(Test-Path -Path $Source)) {Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $Source} Write-Verbose "Starting Installation of $Vendor $Product $Version" -Verbose (Start-Process msiexec.exe -ArgumentList $UnattendedArgs -Wait -Passthru).ExitCode Write-Verbose "Customization" -Verbose Write-Verbose "Stop logging" -Verbose $EndDTM = (Get-Date) Write-Verbose "Elapsed Time: $(($EndDTM-$StartDTM).TotalSeconds) Seconds" -Verbose Write-Verbose "Elapsed Time: $(($EndDTM-$StartDTM).TotalMinutes) Minutes" -Verbose Stop-Transcript |
Automation Framework Module, can you help me with this error?
Hi, where did you find that Automation Framework code? Its not part of the blog post, if it was from inside the members area please send an email to [email protected] and do not publish private code public.
finding the version number would be easier using regex match:
$m -match ‘\d{1,2}.\d{1,2}.\d{1,2}’ | %{$Matches.0}