You can customize and use the following sample script to uninstall View Agent on any number of Linux desktop machines so that you can install another View Agent build. The script does not remove the desktop machines from View Connection Server.

To copy and paste the script content without page breaks, use the HTML version of this topic, available from the Horizon 6 documentation page at https://www.vmware.com/support/pubs/view_pubs.html.

Script Input

This script reads one input file, which is described in Input File for the Sample PowerCLI Scripts to Deploy Linux Desktops. This script also interactively asks for the following information:

  • IP address of the vCenter Server

  • Administrator login name for the vCenter Server

  • Administrator password for the vCenter Server

  • Administrator login name for the ESXi host

  • Administrator password for the ESXi host

  • User login name for the Linux guest operating system

  • User password for the Linux guest operating system

Script Content

<#
.SYNOPSIS
Create Clones from a Master VM

.DESCRIPTION
The Tool is to uninstall the Linux Agent from desktination VM
.NOTES
#>


#------------------- Handle input --------------------
"-----------------------------------------------------"
$vcAddress = Read-Host 'Your vCenter address'
$vcAdmin = Read-Host 'Your vCenter admin user name'
$vcPassword = Read-Host 'Your vCenter admin user password' -AsSecureString
$vcPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vcPassword))
"-----------------------------------------------------"
$hostAdmin = Read-Host 'Your ESXi host admin user name, such as root'
$hostPassword = Read-Host 'Your ESXi admin user password' -AsSecureString
$hostPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($hostPassword))
"-----------------------------------------------------"
$guestUser = Read-Host 'Your VM guest OS user name'
$guestPassword = Read-Host 'Your VM guest OS user password' -AsSecureString
$guestPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($guestPassword))
"-----------------------------------------------------"
#$csvFile = Read-Host 'Csv File '

$csvFile = '.\CloneVMs.csv'

#check if file exists
if (!(Test-Path $csvFile))
{
write-host  -ForeGroundColor Red "CSV File not found"
exit
}
"-----------------------------------------------------"
#------------------- Functions ------------------------
function RunCmdInVM($VMName, $cmd)
{
    Write-Host "Run cmd '$cmd' in VM '$VMName' with user '$guestUser'"
    Invoke-VMScript  -HostUser $hostAdmin -HostPassword $hostPassword -VM $VMName -GuestUser $guestUser -GuestPassword $guestpwd -Confirm:$false -ScriptText $cmd  -ScriptType Bash
}

function UploadFileTovM($VMName, $localFile, $destFolder)
{
    Write-Host "Upload File '$localFile' to '$destFolder' of VM '$VMName' with user '$guestUser'"
    Copy-VMGuestFile -HostUser $hostAdmin -HostPassword $hostPassword -VM $VMName -GuestUser $guestUser -GuestPassword $guestpwd -Confirm:$false -LocalToGuest -Destination $destFolder -Source $localFile
}

#--------------------- Main --------------------------
#Read input CSV file
Disconnect-VIServer $vcAddress -Confirm:$false
#Connect-VIServer $vcAddress -ErrorAction Stop -user $vcAdmin -password $vcPassword
Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword
$csvData = Import-CSV $csvFile

foreach ($line in $csvData)
{
    $VMName = $line.VMName
    $cmd = "sudo /usr/lib/vmware/viewagent/bin/uninstall_viewagent.sh"
    Write-Host "Run uninstall cmd in VM '$VMName' with user '$guestUser'"
    Invoke-VMScript  -HostUser $hostAdmin -HostPassword $hostPassword -VM $VMName -GuestUser $guestUser -GuestPassword $guestPassword -Confirm:$false -ScriptType Bash -ScriptText $cmd
    
    $cmd = "sudo rm -rf /var/log/vmware/*"
    Write-Host "Run cmd '$cmd' in VM '$VMName' with user '$guestUser'"
    Invoke-VMScript  -HostUser $hostAdmin -HostPassword $hostPassword -VM $VMName -GuestUser $guestUser -GuestPassword $guestPassword -Confirm:$false -ScriptType Bash -ScriptText $cmd    
    
    $cmd = "sudo rm -rf /tmp/vmware-root"
    Write-Host "Run cmd '$cmd' in VM '$VMName' with user '$guestUser'"
    Invoke-VMScript  -HostUser $hostAdmin -HostPassword $hostPassword -VM $VMName -GuestUser $guestUser -GuestPassword $guestPassword -Confirm:$false -ScriptType Bash -ScriptText $cmd        
    
}

Disconnect-VIServer $vcAddress -Confirm:$false
exit

Script Execution

The following screen messages show a sample execution of the script:

PowerCLI C:\scripts> .\UninstallAgent.ps1
Your vCenter address: 10.117.44.17
Your vCenter admin user name: administrator
Your vCenter admin user password: *******
--------------------------------------------------
Your ESXi host admin user name, such as root: root
Your ESXi host admin user password: *******
--------------------------------------------------
Your VM guest OS user name: ViewUser
Your VM guest OS user password: *******

After the script completes, View Agent no longer runs on the desktop machines, but the machines and the desktop pool are not removed from View Connection Server. You can run the View Agent installation script to install a different View Agent build and the same desktop pool and desktop machines will be available.