Functions/Install-VirtualBoxGuestAdditions.ps1
function Install-VirtualBoxGuestAdditions { # Install the latest version of VirtualBox Guest Additions $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") if ($isAdmin -ne $true) { Write-Output "Please run this script with admin privileges." Break } else { Write-Output "Installation of VirtualBox Guest Additions for Windows - Beginning" # Get the latest VirtualBox version number $getLatestVersion = Invoke-WebRequest -URI "https://download.virtualbox.org/virtualbox/LATEST.TXT" -UseBasicParsing $getLatestVersion.Content.TrimEnd() | Out-Null $version = ($getLatestVersion).Content.ToString() # Parse the folder containing the latest version to get the name of the "VirtualBox Guest Additions" $url = ("https://download.virtualbox.org/virtualbox/$version").TrimEnd() $fileNameToDownload = (Invoke-WebRequest -Uri $url -UseBasicParsing | ForEach-Object { $_.Links } | Where-Object { $_.href -like "VBoxGuestAdditions*.iso" } | Select-Object -Property href) $fileName = $fileNameToDownload.href $finalUrl = "$url\$fileName" # Save iso file locally $pathToSaveFile = "$env:TEMP\VBoxGuestAdditions.iso" if (Test-Path -Path $pathToSaveFile) { Remove-Item -Path $pathToSaveFile -Force Write-Output "The old version of the file has been deleted." } if (-not (Test-Path -Path $pathToSaveFile)) { Write-Output "Download in progress... Please be patient!" Invoke-WebRequest -Uri "$url\$fileName" -OutFile $pathToSaveFile -UseBasicParsing Write-Output "The file has been downloaded." } # Mount ISO and run the exe in silent mode $isoDrive = Mount-DiskImage -ImagePath $pathToSaveFile -PassThru $isoLetter = ($isoDrive | Get-Volume).DriveLetter $exePath = ($isoLetter + ":\" + "VBoxWindowsAdditions.exe") # Install VBoxGuestAdditions silently Start-Process -FilePath $exePath -ArgumentList @("/S") -Wait Write-Output "VBoxGuestAdditions has been installed" # Dismount ISO Dismount-DiskImage -ImagePath $pathToSaveFile | Out-Null Dismount-DiskImage -ImagePath $pathToSaveFile | Out-Null Write-Output "Installation of VirtualBox Guest Additions for Windows - End" Write-Host "Do you want to restart your computer right now to complete the installation?" Write-Host "[Y]es or [N]o, default is No." $answer = Read-Host if ($answer -eq "Y" -or $answer -eq "Yes") { Write-Output "You said Yes" Restart-Computer } elseif ($answer -eq "N" -or $answer -eq "No") { Write-Output "You said No" Write-Output "Have a nice day!" } else { Write-Output "Your answer is confused, so your computer will not restart." Write-Output "Have a nice day!" } } } |