Can I find the session ID for a user logged on to another machine?

7

2

I want to open an application on another computer on the same network via the command line. The scenario here is that the user is in a room surrounded by about 20 computers and wants to be able to launch the same app on every computer without walking from screen to screen opening it up on each individual machine. I've discovered that I can get the basic functionality for this using PsExec as follows:

psexec \\[computer] -u [username] -p [password] -d -i [SessionID] [program]

For computer, username, password, and program, I'm good. Does anyone know of a way I can figure out which SessionID is assigned to a particular user logged on to a particular machine on the network? Alternately, is there a better way to go about what I'm trying to accomplish?

Dan Tao

Posted 2010-03-23T17:52:29.113

Reputation: 999

Answers

4

You can use the qwinsta (or query session) tool to list all Terminal Server sessions.

Some servers allow to use it remotely directly:

qwinsta /server remotehost

In most cases, though, you'll have to run it through psexec:

psexec \\remotehost qwinsta

user1686

Posted 2010-03-23T17:52:29.113

Reputation: 283 655

3

psexec does not require a session id.

Andrew Wiggin

Posted 2010-03-23T17:52:29.113

Reputation: 31

2It doesn't require a session ID in the sense that it simply defaults to 0, but that default is useless if there are multiple users logged in and you want to pick a specific one. – user1686 – 2013-07-01T16:53:56.470

3

You can try the PowerShell version:

Get-Process powershell | Select-Object SessionId | Format-List

or the MSDOS batch version:

tasklist /FI "IMAGENAME eq tasklist.exe" /FI "USERNAME eq %USERNAME%" /FO LIST | find "Session#:"

Wernight

Posted 2010-03-23T17:52:29.113

Reputation: 571

1

Using Process Explorer on the computer you can use the Users menu to determine the session ID. The number at the beginning of each entry in the menu is the session ID.

It looks like typically you should be able to use 0 for the default session.

heavyd

Posted 2010-03-23T17:52:29.113

Reputation: 54 755

1Looks quite useful; however, I'm looking for the session ID assigned to a user on another computer. It looks like Process Explorer is only applicable to the machine on which it is running. Is that right? – Dan Tao – 2010-03-23T19:32:22.177

Yes that is true. I haven't found a way to do this remotely. – heavyd – 2010-03-23T19:54:55.027

0

Overkill for this question, but just in case this comes in handy

FUNCTION Get-UserNameSessionIDMap ($Comp)
{
  $quserRes = quser /server:$comp | select -skip 1
  if (!$quserRes) { RETURN }
  $quCSV = @()
  $quCSVhead = "SessionID","UserName","LogonTime"
  foreach ($qur in $quserRes) 
  {
    $qurMap = $qur.Trim().Split(" ") | ? {$_}
    if ($qur -notmatch " Disc   ") { $quCSV += $qurMap[2] + "|" + $qurMap[0] + "|" + $qurMap[5] + " " + $qurMap[6] }
    else { $quCSV += $qurMap[1] + "|" + $qurMap[0] + "|" + $qurMap[4] + " " + $qurMap[5] } #disconnected sessions have no SESSIONNAME, others have ica-tcp#x
  }
  $quCSV | ConvertFrom-CSV -Delimiter "|" -Header $quCSVhead
} #end function Get-UserNameSessionIDMap

noam

Posted 2010-03-23T17:52:29.113

Reputation: 123

0

Using the correct session id can indeed be necessary to adequately start a job on a remote machine that is accessed by several distinct users.

Here's what I found after tinkering with a combination of batch commands and psexec. I rolled it all in a *.bat file taking 4 parameters: (P1)UserName, (P2)ServerName, (P3)Login, (P4)Password.

After execution (and if successful), the session info is simply stored in 4 variables: (V1)SessionName, (V2)SessionUser, (V3)SessionId, (V4)SessionState.

The batch is designed for remote info access, but can easily be adapted to local usage.

@echo off 

REM This program gets remote session info on a given server, for a given user.
REM It needs 4 parameters: UserName, ServerName, Login, Password

REM For a local session, simply replace the 'psexec \\...' command parsed by the 'for'
REM loop with: 'query session ^| find /i "%UserName%"'
REM In that case, only the UserName parameter is necessary to call this batch.

set UserName=%1
set ServerName=%2
set Login=%3
set Password=%4

set SessionName=
set SessionUser=
set SessionId=
set SessionState=

for /f "tokens=1,2,3,4 delims=^> " %%a in ('psexec \\%ServerName% -u %Login% -p %Password% query session ^| find /i "%UserName%"') do (

  REM Test iterator because a disconnected session may no longer have a name!

  if /I "%%b"=="%UserName%" (
    set SessionName=%%a
    set SessionUser=%%b
    set SessionId=%%c
    set SessionState=%%d
  )

  if /I "%%a"=="%UserName%" (
    set SessionName=[unknown]
    set SessionUser=%%a
    set SessionId=%%b
    set SessionState=%%c
  )
)

echo Session info:
echo - Name:  %SessionName%
echo - User:  %SessionUser%
echo - ID:    %SessionId%
echo - State: %SessionState%

NOTES:

  • The loop delimiters are '>' and 'space'. That's because the 'query session' command output contains a '>' to indicate the active session on the targeted machine. I had to find a way for the loop to ignore this troublesome character, as it is normally interpreted as redirection to a file.
  • I voluntarily ignored the session type and device outputs. I believe they could eventually be added using %%e and %%f to feed 2 more variables in the loop if need be.
  • As explained in the batch comment, to use the batch on the local machine you shouldn't use the 'psexec \\...' call but: 'query session ^| find /i "%UserName%"' instead. In that case, only parameter P1 is needed.

Fabien Teulieres

Posted 2010-03-23T17:52:29.113

Reputation: 1