You can customize and use the following sample script to install View Agent on any number of Linux desktop machines.

Before you run the script, make sure that View Agent is not already installed on the desktop machines.

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

  • View Agent tar ball path

  • IP address of View Connection Server

  • Administrator login name for View Connection Server

  • Administrator password for View Connection Server

  • Domain name of the View Connection Server administrator

Script Content

<#
.SYNOPSIS
Create Clones from a Master VM

.DESCRIPTION
The Tool is to upload the Linux Agent installer tar ball to desktination VM and do installation

.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))
"-----------------------------------------------------"
$agentInstaller = Read-Host 'Type the View Agent tar ball path. Please take care the installer arch'
#$csvFile = Read-Host 'Csv File '
$csvFile = '.\CloneVMs.csv'

"-----------------------------------------------------"
$brokerAddress = Read-Host 'Type the View Connection Server address'
$brokerAdminName = Read-Host 'Type the View Connection Server Admin user name'
"Please type the View Connection Server Admin user password."
"Plase note that special character in password may not work with the script"
$brokerAdminPassword = Read-Host 'Your broker admin password' -AsSecureString
$brokerAdminPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($brokerAdminPassword))
$domainName = Read-Host 'Type the View Connection Server Admin user domain name'

#check if file exists
if (!(Test-Path $agentInstaller))
{
write-host  -ForeGroundColor Red "installer File not found"
exit
}

#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
$destFolder = "/home/$guestUser/"
foreach ($line in $csvData)
{
    $VMName = $line.VMName
    $cmd = "rm -rf VMware-viewagent-linux-*"
    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
 
    #Upload installer tar ball to Linux VM
    Write-Host "Upload File '$agentInstaller' to '$destFolder' of VM '$VMName' with user '$guestUser'"
    Copy-VMGuestFile -HostUser $hostAdmin -HostPassword $hostPassword -VM $VMName -GuestUser $guestUser -GuestPassword $guestPassword -Confirm:$false -LocalToGuest -Destination $destFolder -Source $agentInstaller

    $cmd = "tar -xvf VMware-viewagent-linux-*.tar"
    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 hostname $VMName"
    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 = "cd VMware-viewagent-linux-* && ./install_viewagent.sh -n $VMName -b $brokerAddress -d $domainName -u $brokerAdminName -p $brokerAdminPassword"
    Write-Host "Run install 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 messages are from an execution of the script:

PowerCLI C:\scripts> .\InstallAgent.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: *******
--------------------------------------------------
Type the View Agent tar ball path. Please take care of the installer arch: .\VMware-viewagent-linux-x86_64-1.0-1234567.tar
--------------------------------------------------
Type the View Connection Server address: 10.117.45.93
Type the View Connection Server Admin user name: viewadmin
Please type the View Connection Server Admin user password.
Please note that special character in password may not work with the script
Your broker admin password: *******
Type the View Connection Server Admin user domain name: domain1

After the script completes, you can log in to View Administrator and view the Linux desktop machines by navigating to View Configuration > Registered Machines and clicking the Others tab.