Query user's session ID from remote workstations (PsExec,Query is executed in Powershell ISE x64, and is not in Powershell ISE x86)

Hello, Dear Colleagues.

I have a PS script to query user's session ID from remote workstations.

$machinelist = "pc01", "pc02", "pc03"
foreach ($machine in $machinelist){
    $results = psexec \\$machine query user
    $id = $results | ? { $_ -match '(\d+)\s+Active' } | % { $matches[1] }
    $results
    $id
}

Work fine in Powershell ISE x64, and does not in Powershell ISE x86.

First question: What the catch?

Error:

The term 'psexec' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.At line:3 char:16
+     $results = psexec \\$machine query user
+                ~~~~~~
    + CategoryInfo          : ObjectNotFound: (psexec:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Second question: there is a better way to query user's sessions ID from remote workstations? 

Fount this, but works only in Powershell ISE x86. And too complicated for me now to modify this script for my need.

Thanks. Have a nice day.

PS.

Can be done without PsExec, but only on x64 ISE too:

$machinelist = "pc01", "pc02", "pc03"
foreach ($machine in $machinelist){
    $results = query user /server:$machine
    $id = $results | ? { $_ -match '(\d+)\s+Active' } | % { $matches[1] }
    $results
    $id
}

The same error:

The term 'query' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.At line:3 char:16
+     $results = query user /server:$machine
+                ~~~~~
    + CategoryInfo          : ObjectNotFound: (query:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
And both questions are still of current interest




  • Edited by fapw Thursday, January 29, 2015 2:18 PM
January 29th, 2015 2:14pm

It is an application that is not stored on the current path,  Use full path.

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 5:16pm

Hello, jrv.  The same result:

The term 'C:\windows\system32\psexec.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.At line:3 char:16
+     $results = C:\windows\system32\psexec.exe \\$machine query user
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\windows\system32\psexec.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
January 29th, 2015 5:20pm

the program is not installed in that location andshouldn't be.

You should never copy tools into the system area.

Place the tool in a local folder and run it from there. It wil work.  YOu are in a 64 bit world.  if you look at that folder fromm a 32 bit prompt you will see it isn't there.

do this from 64 bit powershell:

dir c:\windows\system32\psexec.exe
dir c:\windows\SysWow64\psexec.exe

Which folder is it in?

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 5:36pm

jrv, you are right.

Both files was in System32 folder. Copied PsExec.exe and Query.exe from System32 in SysWOW64 folder - working in both environments now. 

Thanks for that. 

Can you advice something about Q2: 

there is a better way to query user's sessions ID from remote workstations? 

Fount this, but works only in Powershell ISE x86. And too complicated for me now to modify this script for my needs.



  • Edited by fapw Thursday, January 29, 2015 3:14 PM
January 29th, 2015 6:13pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics