PowerCLI

 View Only
Expand all | Collapse all

PowerCLI - List All View Connected Users and VM HostNames

  • 1.  PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 18, 2017 04:20 PM

    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.



  • 2.  RE: PowerCLI - List All View Connected Users and VM HostNames
    Best Answer

    Posted Sep 18, 2017 05:55 PM

    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



  • 3.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 18, 2017 06:31 PM

    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.



  • 4.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 18, 2017 06:36 PM

    Did you import the Horizon View module?

    Import-Module -Name VMware.VimAutomation.HorizonView



  • 5.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 01:52 PM

    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.



  • 6.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 02:00 PM

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



  • 7.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 02:06 PM

    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?



  • 8.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 02:08 PM

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



  • 9.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 02:18 PM

    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?



  • 10.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 03:36 PM

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



  • 11.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 05:05 PM

    You did do a Connect-HVServer before?



  • 12.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 05:51 PM

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



  • 13.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 06:02 PM

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



  • 14.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 07:05 PM

    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.



  • 15.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 19, 2017 07:28 PM

    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?



  • 16.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 20, 2017 01:55 PM

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



  • 17.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 20, 2017 02:03 PM

    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.



  • 18.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 20, 2017 02:11 PM

    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.



  • 19.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 20, 2017 02:14 PM

    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.



  • 20.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 20, 2017 04:09 PM

    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.



  • 21.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Sep 20, 2017 04:21 PM

    I just learned the name of our connection servers, I wasn't aware that these are separate, but then again I'm not the main VMware engineer.



  • 22.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Mar 18, 2020 10:09 AM

    Hi All,

    how can we get the count of connected users into a variable i.e. $connectUsers returns 2550 sessions as opposed to listing all the sessions.

    Using the above measure-object and count always returns 1000 which isnt right



  • 23.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Mar 18, 2020 11:24 PM

    The limit of 1000 seems to be a resultsize issue with the query. I see that all the time with AD but don't see how to override in VMware.

    More info here:

    https://www.retouw.nl/powercli/get-hvmachine-only-finds-1000-desktops/



  • 24.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Mar 18, 2020 02:41 PM

    Thank you this worked! Very Helpful.

    I added this into a loop for every 15 Seconds it lists my users .. and i sorted MachineOrRDSServerName. Again Very Helpful!

    $i =1

    while ($i -le 2)  {$qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) | select -ExpandProperty Results | select -ExpandProperty NamesData | Select UserName,MachineOrRDSServerName |sort MachineOrRDSServerName | ft;

    sleep 15 ;

    write-output “————”

    ;clear-host

    write-output “VDI User Sessions”

    write-output “————”

    Write-Host `n

    }



  • 25.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Mar 18, 2020 02:43 PM

    I just added a count as well, so w/ looping the count and sessions I don't need to be in Horizon all the time.

    ($qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query) |select -ExpandProperty Results | select -ExpandProperty NamesData | Select UserName,MachineOrRDSServerName |sort MachineOrRDSServerName).count

    Thanks!!



  • 26.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Oct 05, 2021 02:22 PM

    Do you know the property name to show the Session State? So we can see the state of each VM in the table as well? I tried using Session, SessionState and none of them worked.



  • 27.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Mar 13, 2020 11:44 PM

    Changing to:

    PowerCLI C:\> $rslt = $qSRv.QueryService_Query($global:DefaultHVServers[0].ExtensionData,$query)

    Gets a result:

    PowerCLI C:\> $rslt

    Id StartingOffset RemainingCount Results

    -- -------------- -------------- -------

                    0              0 {VMware.Hv.SessionId, VMware.Hv.SessionId, VMware.Hv.SessionId, VMware.Hv.SessionId...

    Then:

    $rslt | Select -ExpandProperty Results | Select -ExpandProperty NamesData | Select-Object -Property UserName,DesktopType,DesktopName,MachineOrRDSServerDNS

    Returns info.

    Now if I could make it return based upon connection server...



  • 28.  RE: PowerCLI - List All View Connected Users and VM HostNames

    Posted Mar 16, 2020 03:37 PM

    Anyone know how to get the connection state of the guests? This gets a count of both connected and disconnected.

    Import-Module -Name VMware.VimAutomation.HorizonView

    Connect-HVServer connectionserver


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

    $query.queryEntityType = 'SessionLocalSummaryView'


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


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


    $rslt | Select-Object -ExpandProperty Results |

    Select-Object -ExpandProperty NamesData |

    where-Object {$_.SecurityGatewayDNS -like "view.company.org"} |

    Select-Object -Property UserName,MachineOrRDSServerName,MachineOrRDSServerDNS,SecurityGatewayAddress |

    Measure-Object |

    Select-Object count