One of my customers asked the question if it is possible to get a quick sessioncount for a script that they can run very often for a correct logging of license usage. While this could easily be done by grabbing all the sessions I thought this could be a slow process. I remembered though that the first release of the vmware.hv.helper module had a function called get-podsessions that only returned a sessioncount. I decided to see what was used for this. By going back in time at github I found that the GlobalSessionQueryService was still used but with the GlobalSessionQueryService_GetCountWithSpec method. It needs the service and a spec of the type VMware.Hv.GlobalSessionQueryServiceCountSpec.

the spec itself can hold one of the many options to get a count for

As you can see there is a globalentitlement property that needs to be set using the id so let’s grab that one first.
$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'GlobalEntitlementSummaryView'
$globalentitlements = ($queryService.QueryService_Create($Services1, $defn)).results

I will use the first globalentitlement to grab the sessioncount
$globalentitlement=$globalentitlements | select -first 1
$globalsessionqueryservice_helper = New-Object VMware.Hv.GlobalSessionQueryServiceService
$count_spec = New-Object VMware.Hv.GlobalSessionQueryServiceCountSpec
$count_spec.globalentitlement=$globalentitlement.id
$sessioncountperglobalentitlements=$globalsessionqueryservice_helper.GlobalSessionQueryService_GetCountWithSpec($services1,$count_spec)

As you can see we actually get a count per pod so to get all the counts from all pods from all globalentitlements I have created a script with a couple foreach’s.
$hvserver1=connect-hvserver SERVERNAME
$services1=$hvserver1.extensiondata
$queryService = New-Object VMware.Hv.QueryServiceService
$defn = New-Object VMware.Hv.QueryDefinition
$defn.queryEntityType = 'GlobalEntitlementSummaryView'
$globalentitlements = ($queryService.QueryService_Create($Services1, $defn)).results
$queryservice.QueryService_DeleteAll($services1)
$sessioncount=@()
foreach ($globalentitlement in $globalentitlements){
$globalsessionqueryservice_helper = New-Object VMware.Hv.GlobalSessionQueryServiceService
$count_spec = New-Object VMware.Hv.GlobalSessionQueryServiceCountSpec
$count_spec.globalentitlement=$globalentitlement.id
$sessioncountperglobalentitlements=$globalsessionqueryservice_helper.GlobalSessionQueryService_GetCountWithSpec($services1,$count_spec)
foreach ($sessioncountperglobalentitlement in $sessioncountperglobalentitlements){
$pod=$services1.pod.pod_get($sessioncountperglobalentitlement.id)
$sessioncount+= New-Object PSObject -Property @{
"Global_Entitlement_Name" = $globalentitlement.base.displayname;
"Pod_Name"=$pod.displayname
"Pod_Sessioncount" = ($sessioncountperglobalentitlement | select-object -expandproperty count);
"Site_Name"= ($services1.site.site_get($pod.site)).base.Displayname;
}
}
}
return $sessioncount | select-object Global_Entitlement_Name,Pod_Name,Site_Name,Pod_Sessioncount

The W10_MGMT global entitlement only has a pool in pod1 so even though the pod doesn’t have a pool inside the global entitlement it will still return a sessioncount.
Performance
I also decided to time it but in my small environment it took 3 seconds and 3 of those where for connecting to the connection server. If I removed the connecting part it was 0.7 seconds.
Measure-Command {D:\scripts\dev\session_count.ps1}


Back at the customer I decided to compare this against dumping all global sessions, this will give some better data since it has a couple more sessions in it (around 3500 at the moment of testing)
The script I used for getting all global sessions is the code that I used for the get-hvglobalsession in the vmware.hv.helper module
$query_service_helper = New-Object VMware.Hv.GlobalSessionQueryServiceService
$query=new-object vmware.hv.GlobalSessionQueryServiceQuerySpec
$SessionList = @()
foreach ($pod in $services1.Pod.Pod_List()) {
$query.pod=$pod.id
$queryResults = $query_service_helper.GlobalSessionQueryService_QueryWithSpec($services1, $query)
$GetNext = $false
do {
if ($GetNext) { $queryResults = $query_service_helper.GlobalSessionQueryService_GetNext($services1, $queryResults.id) }
$SessionList += $queryResults.results
$GetNext = $true
} while ($queryResults.remainingCount -gt 0)
$query_service_helper.GlobalSessionQueryService_Delete($services1, $queryresults.id)
}
return $sessionlist
Screenshots from the timing:


so the getcountwithspec method is about 2.5 seconds faster but the data in the globalsession is way more extensive and usable for all kinds of management overviews.