Performance difference between GET-VM and GET-VIEW

Today a colleague has asked me how to optimize a PowerCLI query for virtual machines in a certain virtual datacenter. He want’s to avoid a expensive GET-VM query and he seeks a solution to do that with get-view.

I checked my scripts to see how i solve this problem and i compared it with the default GET-VM query.

GET-VM

This is a short query with code easy to understand and maintain:

  • This is a short query with code easy to understand and maintain:

Get-Datacenter -Name “DCNAME” | Get-VM | Where-Object {$_.PowerState -eq “poweredOn”}

  • First the VIObject for the datacenter is queried, then the piped GET-VM query finds all VM’s inside that datacenter and then the list is filtered by VM’s that are powered on. If you provide the “-Name” option to GET-VM you can search for one or more virtual machines with certain names in that datacenter.
GET-VIEW
  • With this get-view query you get the same list of virtual machines.

get-view -ViewType VirtualMachine -Filter @{“Runtime.PowerState”=”poweredOn”;”Config.Template”=”false”} -SearchRoot $(get-view -ViewType Datacenter -Property Name -Filter @{“Name” = “^DCNAME$”}| select -ExpandProperty MoRef)

  • This code is harder to read and you must provide more informations to get the same result. Get-view is restricted to the viewtype virtualmachine, filters the result by the status “poweredon” and “template=false”. With the “-Searchroot” option and a second get-view query for the datacenter, the virtualmachine query is limited to this datacenter.
GET-VIEW with filter
  • Every time i use the get-view CMDlet i try to limit the number of returned properties with the “-Property” option.

get-view -ViewType VirtualMachine -Property Name,Summary -Filter @{“Runtime.PowerState”=”poweredOn”;”Config.Template”=”false”} -SearchRoot $(get-view -ViewType Datacenter -Property Name -Filter @{“Name” = “^DCNAME$”}| select -ExpandProperty MoRef)

  • In this case only the Name and the Summary section should be fetched. This makes the lists smaller and the script needs less memory and cpu cycles.

I have compared the runtime of the queries in one of the larger vCenter installations. It is interesting to see that GET-VM is faster than GET-VIEW without filters applied. VMware seems to have optimized the GET-VM CMDlet in the current PowerCLI version (5.1 Release 2 build 1012425).

Get-VM GET-VIEW GET-VIEW with a property filter
# virtual Machines 1159 1159 1159
Runtime 23.08s 40.53s 6.29s
Runtime per VM 0,017s 0,035s 0,005s
Speed up -56% 366%