Getting started with PowerCLI 6.5 and Horizon View

One of the recent enhancement released in PowerCLI 6.5 R1 was the addition of a new Horizon View Module which allows you to manage Horizon View from a remote connection, this is a huge enhancement from the previous PowerShell implementation which fell short by many means.

With the new Horizon View Module you will be able to access 100% of the public API meaning if its available as a feature in the UI then the likelihood is that you will be able to automate it and being able to use these from a scripting host with all the other PowerCLI features, your own workstation or a jump box makes this very convenient.

What do I need?

Lets get started…

image

Get-Module –ListAvailable VMware*Horizon* | Import-Module

Get-Command –Module Vmware.VimAutomation.HorizonView

 

Once you have installed PowerCLI 6.5 R1 and launched it you will see that if we import the module and list the commands all we have is 2 cmdlets to work with, well that isn’t much use is it?! or is it?

Clearly the first thing we need to do is connect to the Horizon View server using our credentials, you can do this as follows:

image

As I mentioned before, you have access to 100% of the API so now we are connected lets take a look at the connection variable which gets created automatically and holds the keys to the kingdom, or in this case the entire API.  You will see that a global variable gets created called DefaultHVServers, this stores the connection information to the connected Horizon View Servers and can be access with $global:DefaultHVServers as seen below

image

This gives us a little more interesting information but the real fun stuff is under Extensiondata so lets take a look at that:

image

If you have taken a look at the Horizon View documentation as linked at the start of this post then you will start to recognize some of these areas, the extensiondata holds the access to the entire API, lets see how we might use this. So now we have got this far and understand how to access the API lets take a simple use case and see what we can do.

My use case is that I want to programmatically check the vSphere components section to ensure they are all connected OK, I might then take this script once I have written it and check it on a periodic basis through some monitoring software to ensure my Horizon environment is healthy, a simple use case to get us started. The screen shot below shows the type of data I want to get back:

image

First of all lets look through the extensiondata for something that might look right:

image

Great, we have found a property called VirtualCenterHealth, lets dive deeper into this with the PowerShell command Get-Member, this will tell us all the properties and methods we can use with this API:

image

We can see that we have a number of methods and 2 properties, one of the methods looks like what we need VirtualCenterHealth_List, lets call the method on this object and see what information it returns:

image

This looks like exactly the type of data we need, in fact we can see both the HostData and the DatastoreData from our screenshots so lets take a look at each of these:

image

Perfect, this is exactly what we are looking for, we can see that the accessible property is true and the status property is connected so we are all good and we have the information we need.  One thing we can do is now make this easier to access by wrapping this code into a simple function which we can save, share and re-use in the future as shown below:

image

Conclusion

This may have been a very simple example but its enough to get us going and has the fundamentals that we can use when exposing the Horizon View API, in a future post I will show you how the Horizon View team has used a similar practice to create some great advanced functions which will take the hard work out of you needing to do this for some of the common areas.

 

3 thoughts on “Getting started with PowerCLI 6.5 and Horizon View”

  1. Connecting HVserver via PCLI fails with below error,Any alternate than creating a separate AD account for this purpose.

    Connect-HVServer : ExceptionType : VMware.Hv.EntityNotFound
    ErrorMessage : Could not find user or group in AD
    Id : VMware.Hv.UserOrGroupId
    At line:1 char:1
    + Connect-HVServer -Server .
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Connect-HVServer], Exception
    + FullyQualifiedErrorId : System.Exception,VMware.VimAutomation.HorizonView.Commands.Cmdlets.ConnectHVServer

  2. Hrmm…The API doesn’t seem to mention how you would put a desktop into maintenance mode. This was one the biggest headaches with automating View as there was NO automation interface for it 🙁

Leave a Reply