VMware Cloud Community
paynenicholast
Contributor
Contributor

PowerCLI - Query Horizon for Sessions with Username, VDIName, and Connection Status and Search by Username

So I've been all over the internet trying to find a way to create a search variable for a user and pipe that into a command that will search horizon for Sessions where the user matches or is like that variable and then pipe it out to a table.

The closes I've gotten is the below

--

$Search = 'John'

#Create Horizon Services object

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

#Create Query Definition object with EntityType SessionLocalSummaryView

$HorizonQuery = New-Object VMware.Hv.QueryDefinition

$HorizonQuery.QueryEntityType = 'SessionLocalSummaryView'

#Create Query Service object

$HorizonQueryService = New-Object VMware.Hv.QueryServiceService

#Horizon returns 1000 results at a time, so you have to loop until RemainingCount -eq 0

$HorizonQueryResults = $HorizonQueryService.QueryService_Create($HorizonServerServices, $HorizonQuery)

#Variable to save the returned results

$HorizonSessions = $HorizonQueryResults.Results

do

{

$HorizonQueryResults = $HorizonQueryService.QueryService_GetNext($HorizonServerServices,$HorizonQueryResults.Id)

$HorizonSessions += $HorizonQueryResults.Results

} while ($HorizonQueryResults.RemainingCount -gt 0)

$SearchHorizonSessions = $HorizonSessions | Where-Object -FilterScript {$_.NamesData.Username -like "*$Search*"}

$NamesData = $SearchHorizonSessions.NamesData.Username

$SessionData = $SearchHorizonSessions.SessionData.SessionState

$NamesData | Foreach {$i=0}{new-object pscustomobject -prop @{Username=$_;SessionsState=$SessionData[$i]}; $i++} | Format-Table

--
This works fine until I try to search it. For some reason it breaks and only gives me one letter of the "SessionState". It also doesn't give me vdi Name which I need.

SessionsState Username                

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

            C DOMAIN.LOCAL\John.Doe

I've also tried this which gives me all the data but when I try to search or filter it I can't get it to do it.

--

#Query Horizon VMs by logged in User

$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 | where UserName -Like "*$Search*" |

Format-Table -Property UserName,DesktopName,MachineOrRDSServerDNS

--

I've also tried messing with Get-HVMachineSummary and I can filter and search it fine but for some reason it does not display the user field. Its just blanklp

Any help would be much appreciated

0 Kudos
5 Replies
LucD
Leadership
Leadership

What is the idea behind this line?

$NamesData | Foreach {$i=0}{new-object pscustomobject -prop @{Username=$_;SessionsState=$SessionData[$i]}; $i++} | Format-Table

To be honest, it doesn't make any sense to me.


What is in $NamesData?

Can you do a

$NamesData | Format-Custom -Depth 2


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

0 Kudos
paynenicholast
Contributor
Contributor

Let me explain my part of the script line by line and maybe that is where my problem is.

$SearchHorizonSessions = $HorizonSessions | Where-Object -FilterScript {$_.NamesData.Username -like "*$Search*"} - I'm using this portion of the script to do A search of the usernames that are output in the original script I found online

So in the next part I could find a way to pull the .NamesData.Username information and .SessionData.SessionState information and pump into a single table so i pulled each individually and pumped it into the following two variables

$NamesData = $SearchHorizonSessions.NamesData.Username

$SessionData = $SearchHorizonSessions.SessionData.SessionState

I then use this final line to actually take those two variable lists and pump them into a single table basically it creates a custom object defines properties. the $NamesData variable populates the Usernames property and the $SessionData populates the SessionState property

This is probably not the best way to do this. but Its the only thing I could come up with not knowing much about the api functionality. But it does work up until the point I added in the $SearchHorizonSessions line to filter out individual users.

EDIT: Also the $NamesData | Format-Custom -Depth 2
Command just outputs a bunch of usernames looks like the whole list stored in $NamesData without any headers.

0 Kudos
LucD
Leadership
Leadership

Can you do the same for $sessionData?
I don't quite understand what you are trying to place in the PSCustomObject
That foreach and the variable $i makes no sense to me.


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

0 Kudos
paynenicholast
Contributor
Contributor

Yeah so $NamesData holds the information of usernames pulled from $searchhorizonsessions which searches the variable created by the original script.

The Original script outputs the following stored in $HorizonSessions

PS C:\> $HorizonSessions

Id                  NamesData                  ReferenceData                       SessionData        

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

VMware.Hv.SessionId VMware.Hv.SessionNamesData VMware.Hv.SessionLocalReferenceData VMware.Hv.SessionData

VMware.Hv.SessionId VMware.Hv.SessionNamesData VMware.Hv.SessionLocalReferenceData VMware.Hv.SessionData

... many many more lines

$SearchHorizonSessions = $HorizonSessions | Where-Object -FilterScript {$_.NamesData.Username -like "*$Search*"} - Searches the above data form the search variable for usernames and outputs the following which is stored in $SearchHorizonSessions (just one line)

Id                  NamesData                  ReferenceData                       SessionData        

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

VMware.Hv.SessionId VMware.Hv.SessionNamesData VMware.Hv.SessionLocalReferenceData VMware.Hv.SessionData

$NamesData = $SearchHorizonSessions.NamesData.Username - pulls the username information from the NamesData object from the above command and stores it in $NamesData and looks like this:

DOMAIN.LOCAL\John.Doe

$SessionData = $SearchHorizonSessions.SessionData.SessionState - pulls the sessionsstate information out of the Session Data object from the $SearchHorizonSessions and stores it in the variable and looks like this:

CONNECTED

$NamesData | Foreach {$i=0}{new-object pscustomobject -prop @{Username=$_;SessionsState=$SessionData[$i]}; $i++} | Format-Table - Takes the $NamesData variable and the $SessionData variable and puts in a custom object table that looks like this: (Basically it just takes the two separate lists of data and combines it into one formatted table)

SessionsState Username               

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

            C DOMAIN.LOCAL\john.doe

For some reason it deletes everything after the C from the $SessionState Variable. The weird thing is. If I take out the $SearchHorizonSessions line and then have the $NamesData/$SessionData lines reference $HorizonSessions it works fine... but it pumps out every vdi  and not just the one I'm looking for.

Like I said there may be a better way to do this

0 Kudos
paynenicholast
Contributor
Contributor

Let me state my goal and maybe it would help you better help me:

A user calls in and says they are having some issue with their VDI session.

I want a script that a user can launch that will auto connect the technician to Horizon and Vcenter.

He can then search for the users VDI by the users name and see various details including but not limited to username VDI name and Sessions Status (This is where I need help)

The script will then give him the option to kill the session using the Stop-VMGuest command and I am also working on being able to launch a Remote Console through PowerShell as well

0 Kudos