Using ControlUp with VMware PowerCLI in Horizon Environments

ControlUp ConsolePowerShellvCenterVDIVirtual MachineVMware Horizon

Introduction

ControlUp 8.1 integrates with Horizon, VMware’s virtual desktop infrastructure (VDI) solution. By itself, the ControlUp Management Console (CMC) is a powerful monitoring tool for Horizon, but to fully utilize the power of CMC, you should use it with ControlUp Script Actions (SAs). By doing so, you can begin the journey to a self-managing and a self-healing VDI environment, reducing the time you spend troubleshooting and fixing issues so you can spend more time on strategic projects.

Microsoft PowerShell is the scripting language of choice for SAs, and VMware created PowerCLI to allow VMware solutions to interact with PowerShell. PowerCLI provides more than 700 cmdlets for managing and automating VMware’s various solutions, including vSphere, vROPs , vSAN, NSX-T, and Horizon.

Although relatively simple to install and use, PowerCLI does have a few gotchas, so in this article I will provide you with a step-by-step guide on how to set up and use PowerCLI. For convenience, I will be installing it on a Windows 10 virtual machine running in my home lab, but these same steps will work in larger environments and with Windows Server.

To show you how to install the components needed to create an environment in which SAs can execute, I started out with a fresh install of Windows 10 on a dual-proc 4GB virtual machine (VM) with 64 GB storage being hosted on an ESXi 6.7 server.

Installing PowerShell

PowerShell is installed by default on Windows 10. I started PowerShell by entering PowerShell in Windows Search, and then selected Run ISE as Administrator.

Starting Powershell
Figure 1 – Starting Powershell

The PowerShell Integrated Scripting Environment (ISE) is used to create, run, and debug commands and scripts. The ISE consists of a menu bar, Windows PowerShell tabs, toolbar, script tabs, Script Pane, Console Pane, status bar, text-size slider, and context-sensitive Help.

To verify which version of PowerShell I was using, I entered Get-Host | Select-object Version, and then entered $PSVersionTable in the Console Pane; both of these commands showed that I was running PowerShell 5.1.

PowerShell version
Figure 2 – PowerShell version

Installing PowerCLI

There are different ways that you can download and install PowerCLI (e.g., from the VMware website, etc.), but most users find it easiest to install it directly from PowerShell using the following commands:

Find-Module -Name VMware.PowerCLI
Install-Module -Name VMware.PowerCLI -Scope AllUsers
Set-ExecutionPolicy RemoteSigned

I listed all of the PowerCLI modules by entering Get-Command -Module *VMWare* (Figure 3).

 Installing PowerCLI
Figure 3 – Installing PowerCLI

To test if PowerShell was installed and working properly, I created the following script to connect to my vCenter Server and list all the virtual machines (VMs) powered on within the vCenter Server:

# opt out of joining CEIP
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
#Connect to a vCenter Server
Connect-VIServer -Server 10.0.0.66 -Password PutYourPasswordHere -User administrator@vsphere.local
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
#Get Virtual Machines
Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’} | select name, PowerState | Format-Table

If you want to create a script that you can reuse, select New from the File drop-down menu of the ISE, then save the file to the location of your choice. To run it, select the Run icon (green right arrow) on the ISE menu bar.

Creating Reusable Scripts
Figure 4 – Creating Reusable Scripts

One of the reasons I like using the PowerShell ISE is that I can use the context-sensitive pane to help me craft my PowerCLI commands.

Powershell ISE context-sensitive pane

Figure 5 – Powershell ISE context-sensitive pane

Installing PowerCLI Horizon

There are different ways you can install the Horizon API Module but, again, most users find it easiest to install it from PowerShell directly by entering Get-Module –ListAvailable VMware*Horizon* | Import-Module in the ISE console.

Installing PowerCLI Horizon

Using PowerCLI Horizon

Once you have the Horizon module imported, you can use Connect-HVServer cmdlet to establish a connection to the Horizon API service of the Horizon Connection server:

Connect-HVServer -server 10.0.0.61 -User AD_Admin_Name -Password password -Domain fentronics.local
$hzServices = $Global:DefaultHVServers.ExtensionData
$CServers = $hzServices.ConnectionServer.ConnectionServer_List().General.name

Using PowerCLI Horizon

To list all the Horizon sessions that are active, enter the following:

Write-host “List of all running Horizon session” -ForegroundColor Yellow
$query = New-Object “Vmware.Hv.QueryDefinition”
$query.queryEntityType = ‘SessionLocalSummaryView’
$qSrv = New-Object “Vmware.Hv.QueryServiceService”
$qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) |
Select -ExpandProperty Results |
Select -ExpandProperty NamesData |
Select-Object -Property UserName,DesktopName,DesktopType,MachineOrRDSServerDNS | Format-Table

List of all running Horizon sessions

Conclusion

By using VMware’s PowerCLI, along with its Horizon add-on library, you can create PowerShell scripts to do just about any monitoring function that you could normally carry out with the vSphere Client or the Horizon Console.

Tom Fenton

Tom Fenton is a Technical Marketing manager here at ControlUp (in addition to an all-around great guy). He’s THE subject matter expert for Edge DX, our physical endpoint monitoring solution, as well as an expert in all things VMware (FACT: he used to work at VMware, teaching their employees about their technology). He creates valuable, educational content for the ControlUp blog, leads deep-dive webinars, and educates our sales teams and other IT professionals with tips and tricks about how to use ControlUp solutions. In his spare time, he writes for StorageReview.com and Virtualization Review magazine, and enjoys outdoor sports in the Pacific Northwest. Connect with him on Twitter @vDoppler.