VMware

Horizon view PowerCLI help

I’m trying to write a small PowerShell script that gets the username of a person logged into a VM.

Get-HVMachineSummary -PoolName (pool) | select -ExpandProperty base | Select-Object user, name | Format-Table -Wrap | Out-File -FilePath (path)

for some reason, the “user” parameter gives me [this](https://i.imgur.com/1eQMemC.png) in the output text file. It should be the name of whoever is logged into the VM. I am just an intern at my college learning and teaching myself little bits about scripting and VMware, so I don’t have a big grasp on what I’m doing tbh. I looked up documentation and everything but cannot find anything that helps. Not sure if this is the right place to post, but I thought I would at least try! Thanks 🙂


View Reddit by MITS96View Source

Related Articles

3 Comments

  1. This should work. Just validated in a lab.

    Get-HVMachineSummary -PoolName (pool) | Select-Object -ExpandProperty NamesData | Format-Table -Wrap | Out-File -FilePath (path)

  2. Hi!

    Here is a quick example:

    ​

    #Connect to Horizon API
    Connect-HVServer -server my-horizon-server

    #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)

    #Delete the Query
    $HorizonQueryService.QueryService_Delete($HorizonServerServices, $HorizonQueryResults.Id)

    With the $HorizonSessions variable, you can simply do this:

    $HorizonSessions.NamesData | Select-Object -Property UserName, MachineOrRDSServerName

    Or if you only want users with active sessions:

    $ConnectedHorizonSessions = $HorizonSessions | Where-Object -FilterScript {$_.SessionData.SessionState -eq “CONNECTED”}
    $ConnectedHorizonSessions.NamesData | Select-Object -Property UserName, MachineOrRDSServerName

    or if you like one-liners

    $HorizonSessions | Where-Object -FilterScript {$_.SessionData.SessionState -eq “CONNECTED”} | Select-Object -ExpandProperty NamesData | Select-Object -Property UserName, MachineOrRDSServerName

    Edit. Bonus example:

    Say you know the username, and want to find out the name of the VDI:

    $UserName = “your.username”
    $HorizonServerServices = $global:DefaultHVServers[0].ExtensionData
    $HorizonQuery = New-Object VMware.Hv.QueryDefinition
    $HorizonQuery.QueryEntityType = ‘SessionLocalSummaryView’
    $QueryFilterEquals = New-Object VMware.Hv.QueryFilterEquals
    $QueryFilterEquals.MemberName = ‘namesData.userName’
    $QueryFilterEquals.value = “your.domain.name$Username”
    $HorizonQuery.Filter = $QueryFilterEquals
    $HorizonQueryService = New-Object VMware.Hv.QueryServiceService
    $SearchResult = $HorizonQueryService.QueryService_Query($HorizonServerServices, $HorizonQuery)

    if ($SearchResult.Results)
    {
    $SearchResult.Results.Namesdata
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Close