Query Service Overview

The Query Service is a generic service for listing or filtering large sets of results.

Using the Query Service

The query service supports two types of querying: paged and virtual list.

Filtering

The Query Service provides the ability to filter on properties of the resulting data object. The exact list of supported filters is detailed in the left pane. Further restrictions on which properties can be used as part of a filter will be specified as part of the documentation on that specific data object or the specific filter. Not all fields can be used to filter. In particular, the "id" field of any data object cannot be used as a field to filter upon.

Result controls

There are various types of controls that can be used to control the result set. These are documented in the QueryDefinition.

Examples

Here are a few examples of using the Query Service.

Example: Enumerating all Farms

This example demonstrates how to handle the results from the QueryResults object. This will be omitted in the remaining examples.

$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'FarmSummaryView'
$queryResults = $queryService.QueryService_Create($hvServices, $defn)

# Handle results
try {
   while ($queryResults.results -ne $null) {
      foreach ($result in $queryResults.Results) {
         [VMware.Hvi.FarmSummaryView]$farmSummaryView = $result
         # Do work.
      }

      # Fetch next page
      if ($queryResults.id -eq $null) {
         break;
      }
      $queryResults = $queryService.QueryService_GetNext($hvServices, $queryResults.id)
   }
} finally {
   if ($queryResults.id -ne $null) {
      $queryService.QueryService_Delete($hvServices, $queryResults.id)
   }
}

Example: List enabled RDS Servers

$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'RDSServerInfo'
defn.setFilter = New-Object VMware.Hv.QueryFilterEquals -property @{'memberName'='settings.enabled'; 'value' ='$true'}
$queryResults = $queryService.QueryService_Create($hvServices, $defn)
# Handle results

Example: List desktops (by name in descending order)

$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'DesktopSummaryView'
$defn.sortBy = 'desktopSummaryData.name'
$defn.sortDescending = $true
QueryResults queryResults = queryService.create($hvServices, $defn);
# Handle results

Example: Virtual List query

This example will retrieve the third page of 10 ApplicationInfo objects, without needing to access the first two pages.

$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'ApplicationInfo'
$defn.startingOffset = 20
$defn.limit = 10
$queryResults = $queryService.QueryService_Query($hvServices, $defn)

# Handle results.  Don't need to delete query as there is no server side clean-up needed.

Example: Count persistent disks with names starting with "a"

QueryDefinition defn = new QueryDefinition();
$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'PersistentDiskInfo'
defn.setFilter = New-Object VMware.Hv.QueryFilterStartsWith -property @{'memberName'='general.name'; 'value' ='a'}
$count = $queryService.QueryService_GetCount($hvServices, $defn)
Back to Home