Horizon View API – Query Service

Whilst working on my Horizon View API page, trying to figure out which API calls return the data that you can see in the Horizon Administrator Console, it soon became clear that the API Query Service is used to collect a lot of the data seen within the UI. The way I managed to figure this was by analyzing a Horizon View Example Desktop Script created by Alan Renouf. So I’m going to try and document how to use the Query Service here so that other can use begin to use this really powerful tool.

Hopefully, this will be useful to you. If you have any questions, please either comment below or contact me directly. I will update this page as I learn more.

  • There are few commands that we need to execute to enable the query service, I believe that these three commands will remain the same regardless of what query you are trying to run.
C:\> $ViewAPI = $global:DefaultHVServers[0].ExtensionData
C:\> $query_service = New-Object "Vmware.Hv.QueryServiceService"
C:\> $query = New-Object "Vmware.Hv.QueryDefinition"

  • The next command needs to be modified depending on the data you are trying to capture. You can define the ‘queryEntityType‘ with any of the Queryable Data Objects.
C:\> $query.queryEntityType = 'DesktopSummaryView'

  • Once the Data Object has been defined we can now run the following command. The variable name $Pool can be changed to reflect the data you are collecting.
C:\> $Pools = $query_service.QueryService_Query($ViewAPI,$query)

  • Now we have the Variable defined we can start to find the data we need. In this example, I am looking to get a list Pools. Do get this information I can run the following:

Read the rest of this entry »

PowerCLI: A Simple VM Backup Script

Just lately I've been doing a lot of work in a Lab environment. Some of the work I'm doing is quite important to me so I decided to workout a way I can backup my VM's onto a backup device. To my surprise this was pretty simple to do.

Here is how I decided the script should function:

  1. Send myself an email telling me that the backup process has started
  2. Import the name of the VM to be backed up and the destination datastore from a CSV file
  3. Create a Snapshot of the VM I want to backup
  4. Create a Clone'd VM from the Snapshot
    • Place the backup onto my Backup Datastore
    • Name the backup <master VM name>-<date stamp>
    • Thin provision the backup
  5. Remove the Snapshot from the master VM
  6. Send myself an email telling me that the backup process has completed

So that's what I wanted to do, this is what I ended up with.

Read the rest of this entry »

PowerCLI: Reconfiguring NTP Servers on ESX Hosts

Just lately I’ve been creating a lot of PowerCLI scripts to help configure various aspects of out ESX environment. We’ve just implemented a new NTP Server to our network. So I was given the job to update all of our ESX Hosts. I didn’t fancy spending all morning manually changing them, so I set to work in creating a PowerCLI script to do the business. Here is the result.

$Cluster = "<cluster name>" 
$Hosts = Get-Cluster $Cluster | Get-VMHost 
ForEach ($Host in $Hosts) 
Remove-VmHostNtpServer -NtpServer "<old ntp server>" -VMHost $Host | Out-Null  
Add-VmHostNtpServer -NtpServer "<new ntp server>" -VMHost $Host | Out-Null 
Get-VmHostService -VMHost $Host | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | Out-Null 
write "NTP Server was changed on $Host" 

Adding Virtual PortGroups to all ESX Hosts in a Cluster

Connect-VIServer -Server <vCenter Server>
$AllHosts = Get-Cluster <Cluster Name>
foreach ($ESXHost in ($AllHosts | Get-VMHost))
 Get-VMHost $ESXHost | Get-VirtualSwitch -Name <vSwitch Number> | New-VirtualPortGroup -Name
<Virtual PortGroup Name> -VLanId <VLan ID>
Disconnect-VIServer -Confirm:$false

Adding a Virtual PortGroup to all Hosts

Connect-VIServer -Server <vCenter Server>
Get-VMHost | Get-VirtualSwitch -Name <vSwitch Number> | New-VirtualPortGroup -Name <Virtual PortGroup
 Name> -VLanId <VLan ID>
Disconnect-VIServer -Confirm:$false

Saving Event Logs Using PoSH – My First Ever Script!

Finally after months and months of thinking about it,  I have finally been able to play with Powershell and make it do something useful.

My task was to save the Event Logs Errors off of all of our production servers on a daily basis. I was handed a piece of paper with a list of servers and told to get on with it. I could have easily done it manually, but I would have had to have changed my job title to “Event Log Saver Guy” as that would be all I would have time to do.

Read the rest of this entry »

PowerShell Scripting, its about time i learnt

If i had a £1 for every time i heard/read about how good PowerShell is i probably wouldn’t have to write this post myself!

So I’ve decided to bite the bullet and I’m going to learn. I’m lucky enough to regularly chat to Alan Renouf from the Virtual-Al Blog, and he’s agreed to help me with my journey from a complete Novice to a “PowerShell Guru” like himself.

Update: Alan has just pointed me to this excerlent VI-Toolkit Labs document which has been release from VMWorld Europe 2009.

Personally I think the best way to learn PowerShell is to start off by being given a set of basic tasks that needs to be done and then working out how to use PowerShell to run this task for me.

This part of my Blog will be mostly for my own benefit, but I’m pretty sure that the scripts i come up with could come in handy to other users. The posts i make on PowerShell will not appear on this blogs front page or in my RSS feeds but they will be Tweeted on Twitter, so if you don’t follow me already and your interested in what I’m writing add me now.

Update: Tom Howarth from PlanetVM has just told me that he too has decided to learn PowerShell, hes also using the VI-Toolkit Labs document from VMWorld Europe 2009.

PowerShell Scripting Resources

Here is a list of PowerShell Scripting Resources that i’m using. I will continue update it: