I'm trying to extract a list of VMs and their assigned users (in username format) from a specific pool in our Horizon environment. Googling has led me to this code:
$ViewAPI = $global:DefaultHVServers[0].ExtensionData
$query_service = New-Object "Vmware.Hv.QueryServiceService"
$query = New-Object "Vmware.Hv.QueryDefinition"
$query.queryEntityType = 'MachineNamesView'
$Desktops = $query_service.QueryService_Query($ViewAPI,$query)
$Desktops.Results
Which returns an error:
Exception calling "QueryService_Query" with "2" argument(s): "ExceptionType : VMware.Hv.EntityNotFound
ErrorMessage : Could not find user or group in AD
Id : VMware.Hv.UserOrGroupId"
At line:5 char:1
+ $Desktops = $query_service.QueryService_Query($ViewAPI,$query)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
When attempting other methods of extracting the information - I can't seem to correctly access what I'm looking for. Using the Hv.Helper modules allows me to call Get-HVMachine.base, but it doesn't return the User in any kind of usable format:
Name : CompName
DnsName : CompName.Domain.Com
User : VMware.Hv.UserOrGroupId
AccessGroup : VMware.Hv.AccessGroupId
Desktop : VMware.Hv.DesktopId
DesktopName : PoolName
Session :
BasicState : AVAILABLE
Type : MANAGED_VIRTUAL_MACHINE
OperatingSystem : Windows 10
OperatingSystemArchitecture : 64_bit
AgentVersion : 7.8.0
AgentBuildNumber : 12599301
RemoteExperienceAgentVersion :
RemoteExperienceAgentBuildNumber : 12599301
.base.user just gives me a token ID correlating to the user name, and not the actual user name.
This doesn't seem like it should be that difficult to do, but I can't seem to figure out how to extract this information through the API.
# --- Connnect to Horizon API Service ---
$HVserver1 = Connect-HVServer -server <COMPUTERNAME> -user <USERNAME>-password <PASSWORD> -domain <DOMAIN>
$ViewAPI = $global:DefaultHVServers[0].ExtensionData
$Assignments = @()
$Desktops = Get-HVMachine -PoolName <POOLNAME>
ForEach ($desktop in $desktops) {
If($desktop.base.user) { $User = ($ViewApi.AdUserOrGroup.AduserOrGroup_Get($desktop.base.user)).base.LoginName } Else { $User = ' '}
$Assignments += [PSCUSTOMOBJECT] @{
Desktop = $desktop.base.name
User = $user
}
}
$Assignments | sort desktop
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Look at for a reference, the modules in there have code you may be able to adjust, and maybe you can share it with the maintainer and it may be something to add there. I use this report, with some modifications daily.
GitHub - vCheckReport/vCheck-HorizonView: vCheck for Horizon View
Thank you. I will look into this.
Immediately after posting I think I found my answer here... VMware.Hv.EntityNotFound ErrorMessage : Could not find user or group in AD. So now I am trying to figure out how to set a Pool filter on my query, as I know there are no "dead" assignments on this pool.
# --- Connnect to Horizon API Service ---
$HVserver1 = Connect-HVServer -server <COMPUTERNAME> -user <USERNAME>-password <PASSWORD> -domain <DOMAIN>
$ViewAPI = $global:DefaultHVServers[0].ExtensionData
$Assignments = @()
$Desktops = Get-HVMachine -PoolName <POOLNAME>
ForEach ($desktop in $desktops) {
If($desktop.base.user) { $User = ($ViewApi.AdUserOrGroup.AduserOrGroup_Get($desktop.base.user)).base.LoginName } Else { $User = ' '}
$Assignments += [PSCUSTOMOBJECT] @{
Desktop = $desktop.base.name
User = $user
}
}
$Assignments | sort desktop
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
THANK YOU!
This worked. I see the logic now. I'm still getting my feet wet with API interaction. I couldn't wrap my head around the right API reference or how to call it.