I confess I thought that the value in the session ID matched the sessions and the brief testing that I did bore this out.

Maybe it's because I have a Citrix environment with RDP disabled.

Anyhoo, well done on finding an alternative that works for you and thanks for posting the info back.

BTW, I don't think that you need to differentiate the WMI code for non-TS machines. I found that on a normal desktop login the session ID is "0", so just change your GetSessionID UDF to:
Code:
Function GetSessionID()

$GetSessionID = CInt(SubStr(%TEMP%,1+InStrRev(%TEMP%,"\")))
EndFunction



You can now use the same WMI SELECT statement regardless of login type:
Code:
$ExecQuery="select * from Win32_Process where Name='"+$exe+"' and SessionId='"+GetSessionID()+"'"