views:

1034

answers:

3

Hi there,

I've got the following code to end a process, but I still receive an error code 2 (Access Denied).

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'MSSEARCH.exe'")
For each objProcess in colProcessList
  wscript.echo objProcess.processid
  intrc = objProcess.Terminate()
  if intrc = 0 then wscript.echo "succesfully killed process" else wscript.echo "Could not kill process. Error code: " & intrc End if
+2  A: 

It's quite legitimate to get "access denied" for ending a program. If it's a service (which I'm guessing mssearch.exe is), then it is probably running as the "SYSTEM" user, which has higher privileges than even the Administrator account.

You can't log on as the SYSTEM account, but you could probably write a service to manage other services...

MrZebra
A: 

As a non-privileged user, you can only end processes you own. In a multiuser environment this can bite you in the ankle, because WMI would return equally named processes from other users as well, unless you write a more specific WQL query.

If your process is a service, and your script runs under a privileged account, you may still need to take "the regular route" to stop it, for example using WScript.Shell to call net stop or sc.exe, or, more elegantly, using the Win32_Service class:

Set Services = objWMIService.ExecQuery _
               ("SELECT * FROM Win32_Service WHERE Name = '" & ServiceName & "'")

For Each Service In Services
  Service.StopService()
  WSCript.Sleep 2000 ' wait for the service to terminate '
Next
Tomalak
A: 

If you look on this page: http://msdn.microsoft.com/en-us/library/aa393907(VS.85).aspx you would see that error code 2 is access denied instead of file not found

MysticSlayer