Requirements:
- Share containing the installation file of the Horizon agent version and a . bat containing the command to silently install the Horizon agent
- List of VMs on which to perform the operation
- A user to access vCenter with administrative rights
- One user to install horizon agent on VMs
The script includes:
- Credential request
- First request the user to access the vCenter (line 6)
- Second request the user to remove and install the Horizon Agent on the VMs (line 8)
- Import the VM list (line 12)
- Connecting to the vCenter (line 13)
- Part a for each machine contained in the file with the list of VMs (line 14)
- Check if the Horizon Agent is present (line 25)
- If present, remove it and reboot (line 29), if not present, switch to the installation fa
- Installing the Horizon agent (line 54)
- Share mount
- Running the .bat contained in the share
- Waiting for the installation to finish and reboot
There are 3 “procedures” in the script
For verification if the Horizon Agent is installed (line 18 to 20):
$script = @”
Get-WmiObject Win32_Product -filter “Name=’VMware Horizon Agent'” | Select Caption
” @
For the removal the Horizon agent (line 22 to 24):
$removeapp= @”
wmic Product Where “Name=’VMware Horizon Agent'” Call Uninstall /NoInteractive
” @
For agent installation (Line 50 to 54):
$installapp = @”
New-PSDrive -Name “S” -Root “\\vimng03\share” -Persist -PSProvider “FileSystem”
S:\agentinstallv8.bat
” @
In this last agent installation procedure, you must modify:
- S 🡪 letter with which the share will be temporarily mounted on the VM (which we can change but must also be modified in the installation file .Bat
- \\vimng03\share –> put the share where you want the Horizon agent installation file and the installation file .bat
- S:\agentinstallv8.bat is the file that will install the agent in silently mode
Where inside it is start:
s:\VMware-Horizon-Agent-x86_64-8.0.0-16530789.exe /s /v”/qn ADDLOCAL=BlastUDP,Core,HelpDesk,RDP,RTAV,TSMMR,USB,VmVideo,VmwVaudio,VmwVdisplay,VmwVidd”
to be parameterized according VMware’s guide.
in my case the file will look like this
#The script need: #List the VMs name where remove e reinstall the agent (file c:\vdi.txt or where you want) #Share where is the horizon agent installation file and the file agentinstallv8.bat that contain the silent command for installation #When the script start ask the vCenter Credential and the Admin User Credential for install the Horizon Agent on the VM #Credential for access to vCenter $credential = Get-Credential #Credential with administrator role for install horizon agent $VMCredential = Get-Credential #vcenter $vcenter = "<FQDNvCenter>" #List of VMs where remove e install new agent version $VDIs = Get-Content "c:\vdi.txt" connect-viserver $vcenter -Credential $credential foreach ($VDI in $VDIs){ $VM = Get-VM -Name $VDI Write-Host "Start remove agent from $VM" #Script for verify if the agent is installed $script = @" Get-WmiObject Win32_Product -filter "Name='VMware Horizon Agent'" | Select Caption "@ #Script for remove $removeapp= @" wmic Product Where "Name='VMware Horizon Agent'" Call Uninstall /NoInteractive "@ $value = Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $script -GuestCredential $VMCredential #Check if horizon agent are install if present the script remove it and reboot the VM if ($value.ScriptOutput -like "*Horizon*") { Write-Host "Horizon agent is installed" Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $removeapp -GuestCredential $VMCredential -RunAsync While(Test-Connection $VM -Quiet -Count 1){ Write-Progress -Activity "Rebooting $VM" -Status "Waiting for $VM to shut down." Start-Sleep -sec 1 } While(!(Test-Connection $VM -Quiet -Count 1)){ Write-Progress -Activity "Rebooting $VM" -Status "Waiting for $VM to come back up." Start-Sleep -sec 1 } if ($value.ScriptOutput -cnotlike "*Horizon*") { Write-Host "Agent removed from $VM and $VM rebooted" } } else { Write-Host "Horizon agent is not installed on $VM" } #####Agent Installation Write-Host "Start the Horizon Agent installation in $VM" Sleep 15 #Installation with share change the fileserver,the share name, the labl and the file $installapp = @" New-PSDrive -Name "S" -Root "\\vimng03\share" -Persist -PSProvider "FileSystem" S:\agentinstallv8.bat "@ Invoke-VMScript -VM $VM -ScriptType powershell -ScriptText $installapp -GuestCredential $VMCredential -RunAsync While(Test-Connection $VM -Quiet -Count 1){ Write-Progress -Activity "Rebooting $VM" -Status "Waiting for $VM to shut down." Start-Sleep -sec 1 } While(!(Test-Connection $VM -Quiet -Count 1)){ Write-Progress -Activity "Rebooting $VM" -Status "Waiting for $VM to come back up." Start-Sleep -sec 1 } Write-Host "$VM after installation is UP" $value = Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText $script -GuestCredential $VMCredential if ($value.ScriptOutput -like "*Horizon*") { Write-Host "New Horizon agent is installed in $VM" } else { Write-Host "New Horizon agent is not installed in $VM" } } Disconnect-VIServer $vcenter -Force