Sign in to follow this  
Followers 0
rudi

Terminal Server: Howto get a list of all processes of *THIS* session?

7 posts in this topic

Hi.

For Terminal Server Session Admin Tasks I need to check, if certain EXEs are running for THIS session.

Currently I use a ugly CMD script to catch processes started from *THIS* session.

REM search for the program "DVWIN.EXE", started from "THIS SESSION"
REM
REM for /f "usebackq ...         -> use new semantic, allowing commands in the brackets
REM ... tokens=3" %%a in         -> return 3rd column of each matching result line only. (Default separator = WhiteSpace)
REM (`query session^|find ">"`) 
REM query session                -> this returns all currently existing TS sesstions. The line of "This session" is preceeded by a ">"
REM ^|                           -> pipe the results to the next command, "^" is required to "escape |"
REM find ">"                     -> find the line for "This session" (">")
REM
REM the next search is basically the same, just looking for DVWIN.EXE running in "THIS SESSION ID". 
REM %%r = explicit variable, %%s & %%t = implicit, by the "tokens" statement.
REM to run not from CMD file but directly a the command line, replace the "%%" by "%".

REM the following is ONE line.
for /f "usebackq tokens=3" %%a in (`query session^|find ">"`) do for /f "usebackq tokens=3,4,5" %%r in (`qprocess /ID:%%a ^| find /i "dvwin"`) do echo Session: %%r - PID: %%s - Process Name: %%t

REM Then I can processclose() the DVWIN.EXE started from this session, %%s

Isn't there a possibility to do such a usual thing with pure Autoit? If so, then I miss it 100% >_<

Any suggestions appreciated, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



You could WMI, just one solution. I'm sure there prob a way to do it via DLL, but that's a bit beyond my expertise.

#include <Array.au3>

$arrProcesses = _GetProcessIds(@ComputerName)
_ArrayDisplay($arrProcesses)

Func _GetProcessIds($strHost)
    If Not Ping($strHost,200) Then SetError(1)
    $objWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strHost & "\root\CIMV2")
    If Not IsObj($objWMI) Then SetError(2)
    $colItems = $objWMI.ExecQuery ("SELECT * FROM Win32_Process")
    Dim $arrResults[1][3] = [["Processname","ProcessID","SessionID"]]
    For $objItem In $colItems
        ReDim $arrResults[UBound($arrResults)+1][3]
        $arrResults[UBound($arrResults)-1][0] = $objItem.Name
        $arrResults[UBound($arrResults)-1][1] = $objItem.ProcessId
        $arrResults[UBound($arrResults)-1][2] = $objItem.SessionId
    Next
    Return $arrResults
EndFunc

Share this post


Link to post
Share on other sites

You could WMI, just one solution. I'm sure there prob a way to do it via DLL, but that's a bit beyond my expertise.

#include <Array.au3>

$arrProcesses = _GetProcessIds(@ComputerName)
_ArrayDisplay($arrProcesses)

Func _GetProcessIds($strHost)
    If Not Ping($strHost,200) Then SetError(1)
    $objWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strHost & "\root\CIMV2")
    If Not IsObj($objWMI) Then SetError(2)
    $colItems = $objWMI.ExecQuery ("SELECT * FROM Win32_Process")
    Dim $arrResults[1][3] = [["Processname","ProcessID","SessionID"]]
    For $objItem In $colItems
        ReDim $arrResults[UBound($arrResults)+1][3]
        $arrResults[UBound($arrResults)-1][0] = $objItem.Name
        $arrResults[UBound($arrResults)-1][1] = $objItem.ProcessId
        $arrResults[UBound($arrResults)-1][2] = $objItem.SessionId
    Next
    Return $arrResults
EndFunc

Great, thanks!

With Scriptomatic V2 I just went through all the TerminalServ* and TS*, but couldn't figure out, how to retrieve the session ID of THIS session. Any idea, how to pull that info as well?

I'm thinking of using fileinstall() to a random temp EXE file name, an autoit script that will just run a few seconds to pull from your array the seesion ID first. Then I can lookup what programs are running with the same session ID.

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Great, thanks!

With Scriptomatic V2 I just went through all the TerminalServ* and TS*, but couldn't figure out, how to retrieve the session ID of THIS session. Any idea, how to pull that info as well?

I'm thinking of using fileinstall() to a random temp EXE file name, an autoit script that will just run a few seconds to pull from your array the seesion ID first. Then I can lookup what programs are running with the same session ID.

Regards, Rudi.

Hi Rudi,

search in Forum for WTSQuerySessionInformation. Maybe this helps you.

;-))

Stefan

Share this post


Link to post
Share on other sites

Hi Stefan.

search in Forum for WTSQuerySessionInformation. Maybe this helps you.

I did readup all the hits for WTSQuerySessionInformation, as well as all the WTS* entries nearby this MSDN topic.

It was quite interesting, BUT still I'ven't got a clue how to use these DLL calls to

1.) retrieve the session ID of THIS Session

2.) then to pull a list of all processes started from that Session ID.

To me that's like nice descriptions how to smoothly fly a complicated looping -- but up to now I can't fly at all :/

In the autoit help file there are several "DLL*" entries in the "Process Management" section: Well, I don't know almost anything about DLL calls, so all I can do is to test the sample snippets provided there, but honestly, I don't get the whole thing about calling DLLs, that's all too far beyond my current skills. >_<

I'll try to move on searching for DLL*() calls in the sample code forum here.

Frustrated, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Hi.

My (ugly) workaround to avoid the DLL*() stuff I'm unable to deal with is this:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=z:\TSThisSessionsProcesses.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
Dim $CMD = ' /c @for /f "usebackq tokens=3" %a in (`query session^|find ">"`) do @for /f "usebackq tokens=3,4,5" %r in (`qprocess /ID:%a^|find /i ">"`) do @echo %r\%s\%t'
Dim $MyRead

$PID = Run(@ComSpec & $CMD, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

Sleep(2000) ; then the first stdoutread() will already return the results
While 1
    $MyRead = StdoutRead($PID)
    If @error Then ExitLoop
    ; here I do what's needed. Next line just to demonstrate...
    MsgBox(0, "Session-ID\PID\Process", $MyRead,5)
WEnd

While 1
    $MyRead = StderrRead($PID)
    If @error Then ExitLoop
    MsgBox(0, "STDERR read:", $MyRead,5)
WEnd

MsgBox(0, "Done", "Exiting...",3)

From there I use stringsplit to do what's needed.

>_< DLLCall() or some WMI stuff would be much more nifty :(

Now :( Frustrated :D AND ;) unhappy ;)

Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Hi.

Bump.

Anyone >_<, who knows a much more easy way to retrieve a list of all process IDs started on a Terminal Server from *THIS* session?

Any further suggestions :( mostly appreciated,

regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0