8

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?

6 Answers 6

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
4

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#:"
3

psexec does not require a session id.

1
  • 2
    It 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. Jul 1, 2013 at 16:53
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.

2
  • 1
    Looks 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
    Mar 23, 2010 at 19:32
  • Yes that is true. I haven't found a way to do this remotely.
    – heavyd
    Mar 23, 2010 at 19:54
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.
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .