VMware Cloud Community
nmckaskle
Enthusiast
Enthusiast
Jump to solution

PowerCLI - List All View Connected Users and VM HostNames

I am trying to find a means of listing all the View sessions, not the vCenter sessions, the user sessions of all users connected to a View guest, their user ID, their hostname, and other information. I can't find anywhere in the powershell documentation where I would do this. Does anyone know where to look? This is for VMWare 6.x.

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$query = New-Object "Vmware.Hv.QueryDefinition"

$query.queryEntityType = 'SessionLocalSummaryView'

$qSrv = New-Object "Vmware.Hv.QueryServiceService"

$qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) |

Select -ExpandProperty Results |

Select -ExpandProperty NamesData |

Select-Object -Property UserName,DesktopType,DesktopName,MachineOrRDSServerDNS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

27 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$query = New-Object "Vmware.Hv.QueryDefinition"

$query.queryEntityType = 'SessionLocalSummaryView'

$qSrv = New-Object "Vmware.Hv.QueryServiceService"

$qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) |

Select -ExpandProperty Results |

Select -ExpandProperty NamesData |

Select-Object -Property UserName,DesktopType,DesktopName,MachineOrRDSServerDNS


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nmckaskle
Enthusiast
Enthusiast
Jump to solution

This gets the error straight out the gate "Cannot find type [Vmware.Hv.QueryDefinition]: verify that the assembly containing this type is loaded."

Not sure what I'm missing exactly.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you import the Horizon View module?

Import-Module -Name VMware.VimAutomation.HorizonView


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nmckaskle
Enthusiast
Enthusiast
Jump to solution

Thanks, that last line says cannot index into a null array. Trying to figure it out at the moment. This is a completely new branch of Powershell for me.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Is $qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) returning anything?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

Nothing, that's the problem line, not sure why it isn't returning anything. I am suspecting it has more to do with needing a vCenter connection first?

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

Tried setting up a connection in the script, that did nothing to solve the error though but the connection works.

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

I tried adding this further up:

$ViewAPI = $global:DefaultHVServers[0].ExtensionData

Then tried using that variable the same way, but it said object not set to instance, meaning it's null. Weird. Why is that null?

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

Pretty much stuck here since there is literally nothing out there about this $global:DefaultHVServers[0].ExtensionData and it resulting in a null value.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You did do a Connect-HVServer before?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nmckaskle
Enthusiast
Enthusiast
Jump to solution

I had no idea about that command. I did Connect-VIServer.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You need to do that, the script uses $global:DefaultHVServers[0] , which get filled in after the Connect-HVServer


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

Makes sense, it's like I run into one thing after another: "connect-hvserver : The remote server returned an unexpected response: (400) Bad Request"

I'm using:

connect-hvserver -Server servername -Domain domainname -User username -Password password

I look up this error but it's like nobody has ever talked about this online until now.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try adding the -Verbose switch, see if that provides any additional information.

Can you try the Connect-HVServer with just the Server parameter, the cmdlet should prompt you for the missing parameters.

Does the hostname you pass on the Server parameter resolve correctly (do a nslookup)?

And you are trying to connect to a Horizon View Connection Server?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nmckaskle
Enthusiast
Enthusiast
Jump to solution

I assume I'm supposed to connect to the vcenter server where we manage the View environment?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to retrieve further info from the VM, yes.

When you're only querying the Horizon environment, the Connect-HVServer should be enough.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

I'm not connecting from a view associated VM though. Not sure how it would know where the view environment is. I'm trying to run this command on a Windows 2012 R2 server since that is how I want it to run in the end once I figure all this out.

Reply
0 Kudos
nmckaskle
Enthusiast
Enthusiast
Jump to solution

Verbose is giving me no additional info. This is strange. Not sure why there isn't more documentation on this or if there is, I can't find it with Google.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

When you do the Connect-HVServer the Server parameter specifies to which Connection Server.

And you don't need to be on a View desktop, just a desktop where you have PowerCLI installed.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference