0

I have a Server with multiple Sessions on it, which all have to have a process running. My Program should start up that process as soon as it stopped and the Session is online. I checked for the process, and if I didn't find it I just started it. The problem is, I see the same process over all Sessions. How can I just get the processes of my Session and not the whole Server's? This was my try, but it throws me an Error:

option explicit
DIM strComputer
DIM strProcessName
DIM WshShell
DIM strWMIQuery
DIM strSessionID

Set wshShell = CreateObject("WScript.Shell")
strSessionID= wshShell.ExpandEnvironmentStrings("%SESSIONID%")

strComputer = "." 
strProcessName = "FortiSwitch-Replacer.exe"

strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "' AND SessionId like '" & strSessionID & "'"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

if objWMIService.ExecQuery(strWMIQuery).Count > 0 then
    'Dont do anything
else
    'start process
end if

The Error is at the "ExecQuery()" Part (line 18) saying "Invalid query"

2
  • 1
    Just tested your code and got the same outcome, tried running the query in wbemtest.exe with the same result, but notice that the property SessionId is a CIM_UINT32 not a CIM_STRING so you can't use LIKE against it as you would a string. Once I changed to the query to SessionId = " & numericVariable it worked without the error. Commented Jun 15, 2020 at 9:01
  • For more information about what is expected see the Win32_Process class documentation. Commented Jun 15, 2020 at 9:07

1 Answer 1

0

Lankymart found out the Answer:

The SessionId needs to be an Integer while mine was a String. By Changing the Value into an Integer the program would've worked.

What I want to add:

The ExpandEnvironmentStrings don't offer the SessionId. Right now I've used a small script from an Answer of this Question: Is it possible to run Powershell code from VBScript? It Runs the simple Powershell command to get the SessionId in VBS.

If anyone else has some different Ideas on how to get the SessionId another way I'm interested to learn and see them :)

Sign up to request clarification or add additional context in comments.

Comments