This comprehensive post will explore the benefits of using PowerShell to efficiently automate your VMware Horizon Agent upgrade. By the end of this guide, you will have gained the knowledge and confidence to streamline your upgrade process, saving time and effort while ensuring a seamless transition to the latest supported version of Horizon Agent for your environment.
Prerequisites
- Powershell 3.0 or newer on both the Local system and the Remote VM.
- Enable PS-Remote on the Remote VM.
- Use an account with Administrative Permissions on the Remote VM.
- Make sure the local system and the Remote VM allow connectivity to each other.
- Make sure to set an exception in your Antivirus/Security software.
- Download the VMware Horizon Agent supported by your deployment of Horizon.
- A network share that the Local system has write access to and the Remote VM has at least read access to.
- The FQDN or IP Address of each Remote VM.
- The file name and the fully qualified path where the Horizon Agent installer will be located on the network share.
Powershell Script
- Open your preferred Powershell text editor. I use Visual Studio Code.
- Start a new file and save it using the name “HorizonAgentUpgrade.ps1”, make sure you know the location of the file.
- Now paste the text from the code block below into the file.
# Remote VMs
$computers = @("Computer1","Computer2")
foreach ($computer in $computers) {
try {
# Check if the computer is responding by testing the connection
if (Test-Connection -ComputerName $computer -Count 1 -Quiet) {
# Set file name, source, and destination
$filename = "VMware-Horizon-Agent-xxxxxxx.exe"
$source = "\\path\to\file\$filename"
$destination = "\\$computer\c$\"
# Copy the file to the remote computer
Get-Item -Path $source | Copy-Item -Destination $destination -Force
# Create Session
$session = New-PSSession -ComputerName $computer
# Install the application on the remote computer
Invoke-Command -Session $session -ScriptBlock {
$command = "C:\$filename /s /v`"/qn suppress_runonce_check=1 VDM_VC_MANAGED_AGENT=1 ADDLOCAL=Core,RTAV,GEOREDIR,V4V,VmwVaudio,VmwVidd,TSMMR,BlastUDP,SdoSensor,PerfTracker,HelpDesk,PrintRedir,PSG`""
Start-Process -FilePath "cmd.exe" -ArgumentList "/c", $command -Wait
Remove-Item -Path C:\$filename -ErrorAction SilentlyContinue
Restart-Computer -Force
}
} else {
# The computer is not responding
Write-Host "$computer is not responding."
}
} catch {
Write-Host "An error occurred: $_.Exception.Message"
} finally {
# Close the session
if ($session) {
Remove-PSSession $session
}
}
}
- We need to edit the following variables.
- $computers
- This should be the FQDN or the IP address of each computer you would like to target. Make sure that each entry in the array is formatted using the following syntax @(“FQDN”,”IP address”,”FQDN”).
- $filename
- Change this to the file name of the horizon agent installer you noted earlier.
- $source
- Replace the part of the string “\\path]to\file\” with the fully qualified path you noted earlier.
- $computers
- Please review the flags in the $command variable. I have provided a link to the VMware support article that covers the different flags. Ensure you have selected the version of Horizon you are using when you open the page as they may have changed since I wrote the script (Horizon 2306). https://docs.vmware.com/en/VMware-Horizon/2306/virtual-desktops/GUID-3096DA8B-034B-435B-877E-5D2B18672A95.html
- Save the script, and then we are ready to run it.
Running The Upgrade
- Open a Powershell window as Administrator and set the working directory to the location of your new script using the Set-Location or CD commands.
- Type the command ‘PowerShell -ExecutionPolicy Bypass -File “HorizonAgentUpgrade.ps1″‘ and sit back while it upgrades and reboots each Remote VM.
nice script…
But you do not have handling for active session 🤔 the agent install will fail if there is an active Horizon session, isn’t it?
You make a good point. I will look into adding that to the script or either updating the post to make others aware they need to verify if there are any active Horizon sessions. Great comment!