get-hvmachine only finds 1000 desktops

Edit: this is supposed to be fixed already in the vmware.hv.helper module so make sure you have the latest version of it. I was late in writing it out :@

A while ago I wanted to list all desktops in use from one of our Connection servers. After several tries I kept having issues in finding some of the desktops. This prompted me to do a count on the amount with

(get-hvmachine).count

and it turned out that I received only a 1000 results while I had over 1100+ in that pod. After talking to some people in the VMware Code slack it turned out that this is a limit in the query system that the View API uses. This was verified by running the query myself instead of using the module. Sadly I forgot to make screenshots when I did this and don’t have access to this environment anymore.

$hvServer1 = Connect-HVServer -Server CONNECTIONSERVER
$Services1= $hvServer1.ExtensionData
$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.limit= 10000
$defn.maxpagesize = 10000
$defn.queryEntityType = 'MachineNamesView'

$QueryResults = $queryService.queryservice_create($Services1, $defn)
$queryresults.count

Despite setting the maximums to 1000 or not even filling them I always ended up with 1000 max. After again going over this on Slack I decided to create a do until to pull these results with each time a maximum of 1000 results until no results are received..

$hvServer1 = Connect-HVServer -Server CONNECTIONSERVER
$Services1= $hvServer1.ExtensionData
$queryService = New-Object VMware.Hv.QueryServiceService
$offset=0
$defn = New-Object VMware.Hv.QueryDefinition
$defn.limit= 1000
$defn.maxpagesize = 1000
$defn.queryEntityType = 'MachineNamesView'
[email protected]()
do{
$defn.startingoffset = $offset
$QueryResults = $queryService.queryservice_create($Services1, $defn)
if (($QueryResults.results).count -eq 1000){
$maxresults=1
}
else {
    $maxresults=0
}

$offset+=1000
$output+=$queryresults
}
until ($maxresults -eq 0)

($output.results).count

For this I did make a screenshot:

As you see it kind of stacks the results but the count is ok so I should be able to incorporate this into other scripts.

Bookmark the permalink.

Comments are closed.