Skip to content

Commit b9daba6

Browse files
committed
Download, unpack, and install the latest version of VirtualBox and the corresponding Extension Pack.
1 parent f92ab3e commit b9daba6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Install-VirtualBox.ps1

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function Prepare-IwrCommand {
2+
# create reg key to skip IExplore/Edge's first run wizard so that Invoke-WebRequest works properly
3+
4+
$keyPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Internet Explorer\Main'
5+
try {
6+
New-Item -Path $keyPath -Force -ErrorAction Stop | Out-Null
7+
Set-ItemProperty -Path $keyPath -Name 'DisableFirstRunCustomize' -Value 1
8+
} catch [System.Security.SecurityException],[System.UnauthorizedAccessException] {
9+
10+
if ($MyInvocation.MyCommand.Definition.Replace("`r",'') -match 'try {\n(?<adminCommands>(.|\n)+)\n\s*} catch') {
11+
$adminCommands = $Matches.adminCommands.Replace(' ','').Replace('$keyPath',"'$keyPath'")
12+
Start-Process -FilePath powershell.exe -Verb RunAs -Wait -ArgumentList '-Command',$adminCommands
13+
}
14+
}
15+
16+
}
17+
18+
function Install-VirtualBox {
19+
# TODO: check if virtualbox and ext pack are alreadu installed before downlaoding
20+
# TODO: check if Hyper-V hypervisor is enabled and throw error
21+
$downloadPageUri = 'https://download.virtualbox.org/virtualbox'
22+
$global:downloadDestination = New-Item -Path $PSScriptRoot -Name Files -ItemType Directory | Select-Object -ExpandProperty FullName
23+
try {
24+
$latestVersion = Invoke-WebRequest -Uri "$downloadPageUri/LATEST.TXT" -Method Get
25+
} catch [System.NotSupportedException] {
26+
Prepare-IwrCommand
27+
$latestVersion = Invoke-WebRequest -Uri "$downloadPageUri/LATEST.TXT" -Method Get
28+
}
29+
30+
$version = $latestVersion.Content.Trim()
31+
$versionFilesPage = Invoke-WebRequest -Uri "$downloadPageUri/$version/"
32+
33+
# download the installer file to the configured destination
34+
if ($versionFilesPage.Content -match '<a href="(?<filename>.*Win\.exe)"') {
35+
$installerUri = "$downloadPageUri/$version/$($Matches['filename'])"
36+
$installerFile = Join-Path -Path $downloadDestination -ChildPath $Matches['filename']
37+
Invoke-WebRequest -Uri $installerUri -Method Get -OutFile $installerFile
38+
}
39+
40+
# download the extension pack to the configured destination
41+
if ($versionFilesPage.Content -match '<a href="(?<extPack>.*\.vbox-extpack)"') {
42+
$extPackUri = "$downloadPageUri/$version/$($Matches['extPack'])"
43+
$extPackFile = Join-Path -Path $downloadDestination -ChildPath $Matches['extPack']
44+
Invoke-WebRequest -Uri $extPackUri -Method Get -OutFile $extPackFile
45+
}
46+
47+
# silently extract the MSI from the installer EXE
48+
Start-Process -FilePath $installerFile -Verb runas -Wait -ArgumentList '-extract','-silent','-path',$downloadDestination
49+
50+
# silently install by passing the extracted msi to msiexec with correct arguments
51+
$installerMsi = Get-ChildItem -Path $downloadDestination | Where-Object -Property Name -Value 'msi$' -Match | Select-Object -ExpandProperty FullName
52+
Start-Process -FilePath msiexec.exe -Verb runas -Wait -ArgumentList '/i',$installerMsi,'/qn','VBOX_INSTALLDESKTOPSHORTCUT=0'
53+
54+
# add virtualbox install directory to PATH
55+
$Env:Path += ';C:\Program Files\Oracle\VirtualBox'
56+
57+
# install extension pack
58+
'y' | vboxmanage extpack install $extPackFile
59+
60+
# create VirtualBox VMs root folder
61+
# TODO: make this customizable
62+
$global:VMroot = New-Item -Path $PSScriptRoot -Name VMs -ItemType Directory | Select-Object -ExpandProperty FullName
63+
}

0 commit comments

Comments
 (0)