简体   繁体   English

结束win32进程vbscript

[英]End win32 process vbscript

I've got the following code to end a process, but I still receive an error code 2 (Access Denied).我有以下代码来结束一个进程,但我仍然收到错误代码 2(拒绝访问)。

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

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.如果它是一项服务(我猜 mssearch.exe 是),那么它可能以“SYSTEM”用户身份运行,该用户甚至比管理员帐户拥有更高的权限。

You can't log on as the SYSTEM account, but you could probably write a service to manage other services...您无法以 SYSTEM 帐户登录,但您可能可以编写一个服务来管理其他服务...

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.在多用户环境中,这可能会咬你一口,因为 WMI 也会从其他用户返回同名进程,除非你编写更具体的 WQL 查询。

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:如果您的进程是一个服务,并且您的脚本在特权帐户下运行,您可能仍然需要采取“常规路线”来停止它,例如使用WScript.Shell调用net stopsc.exe ,或者,更优雅,使用Win32_Service类:

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

如果您查看此页面: http : //msdn.microsoft.com/en-us/library/aa393907(VS.85).aspx,您会看到错误代码 2 是访问被拒绝而不是文件未找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM