nmckaskle
Enthusiast
Enthusiast

PowerCLI - List All View Connected Users and VM HostNames

Jump to solution

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

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

26 Replies
LucD
Leadership
Leadership

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

nmckaskle
Enthusiast
Enthusiast

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.

0 Kudos
LucD
Leadership
Leadership

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

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.

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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?

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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?

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

You did do a Connect-HVServer before?


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

nmckaskle
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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.

0 Kudos
LucD
Leadership
Leadership

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

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

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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.

0 Kudos
nmckaskle
Enthusiast
Enthusiast

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.

0 Kudos
LucD
Leadership
Leadership

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