Question

I want to get the session ID of a user's session in a remote machine. I could achieve this through the command

$result = Query Session account17 /server:localhost 

and

$result = qwinsta account17 /SERVER:localhost 

But this is done locally.

But when trying to run the command on a remote session I am getting the error 'No session exists for account17', even if the user account17 is logged in. I am able to get the session of the user by performing the above command on the machine locally.

Is there a way to retrieve the session ID of a remote machine?

Était-ce utile?

La solution

You can get that from WMI by the Win32_LoggedOnUser class, but you have to do some parsing to get it nicely:

gwmi win32_loggedonuser -ComputerName $computername |
foreach {
          [PSCustomObject]@{
             User = $_.antecedent -replace '.+Domain="(.+)",Name="(.+)"','$1\$2'
             Session = $_.dependent -replace '.+LogonID="(\d+)"','$1'
             }
        }

Or you can use CIM:

Get-CimInstance win32_loggedonuser -ComputerName $computername |
foreach {
          [PSCustomObject]@{
             User = $_.antecedent -replace '.+Name = "(.+)", Domain = "(.+)".','$2\$1'
             Session = $_.dependent -replace '.+LogonID = "(\d+)".','$1'
             }
        }

Autres conseils

Try the Terminal Services PowerShell Module

PS> Get-TSSession -ComputerName server1 -UserName account17
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top