<# .SYNOPSIS Install VMware Tools on a virtual machine. .DESCRIPTION https://vm.osdcloud.ch .NOTES Version: 0.1 Creation Date: 06.05.2025 Author: Akos Bakos Company: SmartCon GmbH Contact: akos.bakos@smartcon.ch Copyright (c) 2025 SmartCon GmbH HISTORY: Date By Comments ---------- --- ---------------------------------------------------------- 06.05.2025 Akos Bakos Script created #> #region Helper Functions function Write-DarkGrayDate { [CmdletBinding()] param ( [Parameter(Position=0)] [System.String] $Message ) if ($Message) { Write-Host -ForegroundColor DarkGray "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $Message" } else { Write-Host -ForegroundColor DarkGray "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) " -NoNewline } } function Write-DarkGrayHost { [CmdletBinding()] param ( [Parameter(Mandatory=$true, Position=0)] [System.String] $Message ) Write-Host -ForegroundColor DarkGray $Message } function Write-DarkGrayLine { [CmdletBinding()] param () Write-Host -ForegroundColor DarkGray "=========================================================================" } function Write-SectionHeader { [CmdletBinding()] param ( [Parameter(Mandatory=$true, Position=0)] [System.String] $Message ) Write-DarkGrayLine Write-DarkGrayDate Write-Host -ForegroundColor Cyan $Message } function Write-SectionSuccess { [CmdletBinding()] param ( [Parameter(Position=0)] [System.String] $Message = 'Success!' ) Write-DarkGrayDate Write-Host -ForegroundColor Green $Message } #endregion $Title = "Install VMware Tools" $host.UI.RawUI.WindowTitle = $Title [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials $env:APPDATA = "C:\Windows\System32\Config\SystemProfile\AppData\Roaming" $env:LOCALAPPDATA = "C:\Windows\System32\Config\SystemProfile\AppData\Local" $Env:PSModulePath = $env:PSModulePath+";C:\Program Files\WindowsPowerShell\Scripts" $env:Path = $env:Path+";C:\Program Files\WindowsPowerShell\Scripts" $Global:Transcript = "$((Get-Date).ToString('yyyy-MM-dd-HHmmss'))-Install-VMwareTools.log" Start-Transcript -Path (Join-Path "$env:ProgramData\Microsoft\IntuneManagementExtension\Logs\OSD\" $Global:Transcript) -ErrorAction Ignore | Out-Null If ((Get-CimInstance -ClassName Win32_computersystem).model -like "VMware*") { #================================================= Write-SectionHeader "Install VMware Tools" #================================================= $Title = 'Installing VMware Tools' $host.ui.RawUI.WindowTitle = "$Title" $host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.size(2000, 2000) # Define the base URL for the latest VMware Tools directory $baseUrl = "https://packages.vmware.com/tools/releases/latest/windows/x64/" # Get the HTML content of the directory page Write-DarkGrayHost "Fetching the latest VMware Tools installer URL" $htmlContent = Invoke-WebRequest -Uri $baseUrl -UseBasicParsing # Parse the HTML to find the installer file $installerFile = $htmlContent.Links | Where-Object { $_.href -match "VMware-tools-.*-x64.exe" } | Select-Object -First 1 if ($null -ne $installerFile) { $installerUrl = $baseUrl + $installerFile.href $toolsInstallerPath = "$env:windir\Temp\VMware-tools-latest-x64.exe" # Download the installer Write-DarkGrayHost "Downloading the latest VMware Tools from $installerUrl" # Invoke-WebRequest -Uri $installerUrl -OutFile $toolsInstallerPath -UseBasicParsing curl.exe -L $installerUrl -o $toolsInstallerPath # Check if the download was successful if (Test-Path $toolsInstallerPath) { Write-DarkGrayHost "Download successful --> Installing VMware Tools" try { # Install VMware Tools silently $Process = Start-Process -FilePath $toolsInstallerPath -ArgumentList "/S /v`"/qn REBOOT=ReallySuppress ADDLOCAL=ALL /l*v C:\Windows\Temp\VMware_Tools_Install.log`"" -NoNewWindow -Wait -PassThru # Now we can reliably get the exit code $exitCode = $process.ExitCode # Check the exit code switch ($exitCode) { 0 { Write-DarkGrayHost "VMware Tools installation completed successfully with exit code: 0" } 3010 { Write-DarkGrayHost "VMware Tools installation completed successfully with exit code: 3010 (System restart required)" } 1641 { Write-DarkGrayHost "VMware Tools installation initiated a restart with exit code: 1641" } default { Write-Host -ForegroundColor Yellow "VMware Tools installation completed with non-standard exit code: $exitCode. Check logs for details." } } } catch { Write-Host -ForegroundColor Yellow "Error during VMware Tools installation: $($_.Exception.Message)" } Write-DarkGrayHost "Clean up the installer" Remove-Item $toolsInstallerPath -Force } else { Write-DarkGrayHost "Failed to download VMware Tools installer" } } else { Write-DarkGrayHost "No installer file found at $baseUrl" } } else { Write-DarkGrayHost "Machine is not a virtual machine" -ForegroundColor Yellow }