I really like these automation puzzles because they let me to do something that nobody has done before (I always start out researching), forces me to learn more PowerShell, grows my Evergreen Github repo and helping out the community at the same time.
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
Function Get-VirtualBoxVersion { <# .NOTES Author: Trond Eirik Haavarstein Twitter: @xenappblog #> $url = "https://download.virtualbox.org/virtualbox/LATEST.TXT" try { $temp = New-TemporaryFile Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $temp -ErrorAction SilentlyContinue $Version = get-content $temp Write-Output $Version } catch { Throw "Failed to connect to URL: $url with error $_." } } Function Get-VirtualBoxUri { <# .SYNOPSIS Gets the latest VirtualBox download URI. .DESCRIPTION Gets the latest agent VirtualBox download URI for Win, OSX and Linux .NOTES Author: Trond Eirik Haavarstein Twitter: @xenappblog .LINK https://github.com/aaronparker/Get.Software #> $Platform = "Win" $Version = "$(Get-VirtualBoxVersion)" $dir = "https://download.virtualbox.org/virtualbox/$Version/" $file = (wget -Uri $dir -UseBasicParsing).links.href | Select-String -Pattern "$Platform" $url = "$dir" + "$file" Write-Output $url } Clear-Host Write-Verbose "Setting Arguments" -Verbose $StartDTM = (Get-Date) $Vendor = "Oracle" $Product = "VirtualBox" $PackageName = "VirtualBox" $Version = "$(Get-VirtualBoxVersion)" $InstallerType = "exe" $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" $UnattendedArgs = '--silent' $url = "$(Get-VirtualBoxUri)" $ProgressPreference = 'SilentlyContinue' Start-Transcript $LogPS if( -Not (Test-Path -Path $Version ) ) { New-Item -ItemType directory -Path $Version } CD $Version Write-Verbose "Downloading $Vendor $Product $Version" -Verbose If (!(Test-Path -Path $Source)) { Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $Source } Else { Write-Verbose "File exists. Skipping Download." -Verbose } Write-Verbose "Starting Installation of $Vendor $Product $Version" -Verbose (Start-Process "$PackageName.$InstallerType" $UnattendedArgs -Wait -Passthru).ExitCode Write-Verbose "Starting Installation of $Vendor $Product $Version Extension Pack" -Verbose $ExtPack = "vbox-extpack" $dir = "https://download.virtualbox.org/virtualbox/$Version/" $extpackfile = (wget -Uri $dir -UseBasicParsing).links.href | Select-String -Pattern "$Version.$ExtPack" $extpackurl = "$dir" + "$extpackfile" Invoke-WebRequest -UseBasicParsing -Uri $extpackurl -OutFile $extpackfile Set-Alias vboxmanage "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" "y" | vboxmanage extpack install --replace $extpackfile 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 |