Last active
October 18, 2021 05:25
-
-
Save clong/16aa113c16d2812141dcd5f427e3454a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$software = "VMware Tools"; | |
$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $software }) -ne $null | |
If ((-Not $installed) -And (Test-Path "C:\Windows\Temp\vmware-vm*")) { | |
Write-Output "VMware Tools does not appear to be installed. Attempting to install now..." | |
Write-Output "First, downloading and installing 7z..." | |
if (!( Test-Path "C:\Windows\Temp\7z2102-x64.msi")) { | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z2102-x64.msi', 'C:\Windows\Temp\7z2102-x64.msi') | |
} | |
if (!(Test-Path "C:\Windows\Temp\7z2102-x64.msi")) { | |
Start-Sleep 5; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z2102-x64.msi', 'C:\Windows\Temp\7z2102-x64.msi') | |
} | |
cmd /c msiexec /qb /i C:\Windows\Temp\7z2102-x64.msi | |
Try { | |
# Disabling the progress bar speeds up IWR https://github.com/PowerShell/PowerShell/issues/2138 | |
$ProgressPreference = 'SilentlyContinue' | |
$pageContentLinks = (Invoke-WebRequest('https://softwareupdate.vmware.com/cds/vmw-desktop/ws') -UseBasicParsing).Links | where-object { $_.href -Match "[0-9]" } | Select-Object href | % { $_.href.Trim('/') } | |
$versionObject = $pageContentLinks | % { new-object System.Version ($_) } | sort-object -Descending | select-object -First 1 -Property:Major, Minor, Build | |
$newestVersion = $versionObject.Major.ToString() + "." + $versionObject.Minor.ToString() + "." + $versionObject.Build.ToString() | out-string | |
$newestVersion = $newestVersion.TrimEnd("`r?`n") | |
$nextURISubdirectoryObject = (Invoke-WebRequest("https://softwareupdate.vmware.com/cds/vmw-desktop/ws/$newestVersion/") -UseBasicParsing).Links | where-object { $_.href -Match "[0-9]" } | Select-Object href | where-object { $_.href -Match "[0-9]" } | |
$nextUriSubdirectory = $nextURISubdirectoryObject.href | Out-String | |
$nextUriSubdirectory = $nextUriSubdirectory.TrimEnd("`r?`n") | |
$newestVMwareToolsURL = "https://softwareupdate.vmware.com/cds/vmw-desktop/ws/$newestVersion/$nextURISubdirectory/windows/packages/tools-windows.tar" | |
Write-Host "The latest version of VMware tools has been determined to be downloadable from $newestVMwareToolsURL" | |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile("$newestVMwareToolsURL", 'C:\Windows\Temp\vmware-tools.tar') | |
} | |
Catch { | |
Write-Host "Unable to determine the latest version of VMware tools. Falling back to hardcoded URL." | |
(New-Object System.Net.WebClient).DownloadFile('https://softwareupdate.vmware.com/cds/vmw-desktop/ws/15.5.5/16285975/windows/packages/tools-windows.tar', 'C:\Windows\Temp\vmware-tools.tar') | |
} | |
cmd /c "C:\PROGRA~1\7-Zip\7z.exe" x C:\Windows\Temp\vmware-tools.tar -oC:\Windows\Temp | |
Move-Item c:\windows\temp\VMware-tools-windows-*.iso c:\windows\temp\windows.iso | |
Try { Remove-Item "C:\Program Files (x86)\VMWare" -Recurse -Force -ErrorAction Stop } Catch { Write-Host "Directory didn't exist to be removed." } | |
cmd /c "C:\PROGRA~1\7-Zip\7z.exe" x "C:\Windows\Temp\windows.iso" -oC:\Windows\Temp\VMWare | |
cmd /c C:\Windows\Temp\VMWare\setup.exe /S /v "/qn REBOOT=R" | |
Write-Output "Checking if VMware tools installation was successful..." | |
$installed = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where { $_.DisplayName -eq $software }) -ne $null | |
if ($installed) { | |
Write-Output "VMware tools successfully installed!" | |
} | |
Else { | |
Write-Output "VMware tools installation failed. You should probably file an issue on Github to report this" | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment