user726720
user726720

Reputation: 1247

VBS Invalid Query in Windows 10

Why does the following VBS code work in Windows 7 but gives error on Windows 10:

strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'notepad.exe'")
Wscript.Echo colProcesses.Count

The error I'm getting in Windows 10 is:

test_2.vbs(4,1) SWbemObjectSet: Invalid query

What am I doing wrong here?

Upvotes: 1

Views: 895

Answers (1)

Mithilesh Indurkar
Mithilesh Indurkar

Reputation: 489

Try and use the below code. This should work

strQuery = "select * from win32_process where Name = " & """" & "Notepad.exe" & """"
strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery(strQuery)
Wscript.Echo colProcesses.Count

Upvotes: 1

Related Questions