VMware Horizon Community
Mozeps
Contributor
Contributor
Jump to solution

VMware View API - Get more than 1000 Users

Hello guys,

how do I manage to get more than 1000 users via the View API? Our system runs on version 7.7.0 build-11038474.

I use Powershell to access the API.

Use case:

I want to retrieve all Ad-Users as Horizon View objects. After this I want to check all Userentitlements and delete every entitlement without a VM in the designated Pool.

We use a 1:1 User-To-VM entitlement.

Example:

User John Doe has 3 User Entitlements in Global Entitlements

- GlobalEnt1 with VM myVm1

- GlobalEnt2 with VM myVm2

- GlobalEnt3 without a VM

In this case the Userentitlement to GlobalEnt 3 needs to be deleted because the User has no VM in it.

My code is the following:

function Get-AllHvUsers ($services) {

    $results = @();

    $offset = 0;


    $queryService = new-object VMware.Hv.QueryServiceService;

    $queryDefinition = New-Object VMware.Hv.QueryDefinition;

    $queryDefinition.QueryEntityType = "ADUserOrGroupSummaryView";

    $queryDefinition.Limit = 1000;

    $queryDefinition.MaxPageSize = 1000;

    while($true) {

        $queryDefinition.StartingOffset = $offset;

        [System.Array]$curResults = $queryService.QueryService_Query($services, $queryDefinition).Results

        if($curResults.Length -eq 1000) {

            $offset += 1000;

            [System.Array]$results += $curResults.PSObject.Copy();

        } else {

            [System.Array]$results += $curResults.PSObject.Copy();

            break;

        }

    }

    return $results;

}

Error:

"ExceptionType : VMware.Hv.InvalidQuery

ErrorMessage : ADUserOrGroupQuery does not support a starting offset"

In Zeile:1 Zeichen:1

+ [System.Array]$curResults = $queryService.QueryService_Query($service ...

If I don't use an offset I am stuck at 1000 results which is less than 10% of all users.

Increasing the Limit or MaxPageSize also doesn't do anything.

I hope someone has an answer to this.

Thanks

1 Solution

Accepted Solutions
freydaddy
VMware Employee
VMware Employee
Jump to solution

Another example, not tested as I don't have access to an environment with more than 1000 users at the moment, but this should be close. Note that we are using QueryService_Create() method to initiate the query, instead of QueryService_Query(). It's also important to use QueryService_Delete() to free up server side resources, once you're finished.

function Get-AllHvUsers ($services) {


   $results = @()

   $queryService = new-object VMware.Hv.QueryServiceService

   $queryDefinition = New-Object VMware.Hv.QueryDefinition

   $queryDefinition.QueryEntityType = "ADUserOrGroupSummaryView"


   $curResults = $queryService.QueryService_Create($services, $queryDefinition)

   $results += $curResults.Results


   if($curResults.RemainingCount -gt 0) {

   do {

   $queryResponse = $queryService.QueryService_GetNext($services,$curResults.Id)

   $results += $queryResponse.Results

   $remaining = $queryResponse.RemainingCount

  } while { $remaining -gt 0 }


  }


   $queryService.QueryService_Delete($services, $curResults.Id)


   return $results

}

View solution in original post

3 Replies
freydaddy
VMware Employee
VMware Employee
Jump to solution

Instead of offset, use GetNext()...

Example in the docs here: https://vdc-download.vmware.com/vmwb-repository/dcr-public/d2c10467-1d32-485a-bc12-cd1a9af9e991/f2b3...

$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)

  }

}

0 Kudos
freydaddy
VMware Employee
VMware Employee
Jump to solution

Another example, not tested as I don't have access to an environment with more than 1000 users at the moment, but this should be close. Note that we are using QueryService_Create() method to initiate the query, instead of QueryService_Query(). It's also important to use QueryService_Delete() to free up server side resources, once you're finished.

function Get-AllHvUsers ($services) {


   $results = @()

   $queryService = new-object VMware.Hv.QueryServiceService

   $queryDefinition = New-Object VMware.Hv.QueryDefinition

   $queryDefinition.QueryEntityType = "ADUserOrGroupSummaryView"


   $curResults = $queryService.QueryService_Create($services, $queryDefinition)

   $results += $curResults.Results


   if($curResults.RemainingCount -gt 0) {

   do {

   $queryResponse = $queryService.QueryService_GetNext($services,$curResults.Id)

   $results += $queryResponse.Results

   $remaining = $queryResponse.RemainingCount

  } while { $remaining -gt 0 }


  }


   $queryService.QueryService_Delete($services, $curResults.Id)


   return $results

}

Mozeps
Contributor
Contributor
Jump to solution

It works perfectly thanks. I can also use this method with all the other object types.

This makes the api now much easier to use Smiley Happy

0 Kudos