杀死具有特定名称的所有进程 [英] Kill all processes with a certain name

查看:38
本文介绍了杀死具有特定名称的所有进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行备份程序的 HTA.对于备份,我使用了 SyncToy 命令行可执行文件,但在某些情况下无法正常停止.

I have an HTA that runs a backup routine. For the backup I'm using the SyncToy command line executable, which in some instances does not properly cease.

因此,我试图终止任何名为 SyncToyCmd.exe 的进程.我利用 window_onBeforeUnload 事件并从那里调用 KillSyncToy() 子.

Beacuse of this, I'm trying to kill any process with the name SyncToyCmd.exe. I make use of the window_onBeforeUnload event and call the KillSyncToy() sub from there.

该函数正在正确检测 SyncToyCmd.exe 的实例,但是在尝试终止进程时我收到错误 -

The function is correctly detecting instances of the SyncToyCmd.exe, however when trying to kill the process I'm receiving an error -

错误:系统找不到指定的文件.

Error: The system cannot find the file specified.

我猜我在这里做错了什么,欢迎任何帮助.

I'm guessing that I'm doing something wrong here and any assistance would be welcome.

Sub KillSyncToy()
    Dim WMIService : Set WMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    Dim ProcessList : Set ProcessList = WMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'SyncToyCmd.exe'")

    Dim Process
    For Each Process in ProcessList
        '** Note that 'Shell' is a global instace of the 'WScript.Shell' object, so
        'there is no need to declare it here *'
        Shell.Exec "PSKill " & Process.ProcessId
    Next

    Set WMIService = Nothing
    Set ProcessList = Nothing
End Sub

推荐答案

该错误信息表示 Exec 找不到 pskill.exe,因此很可能是可执行文件不在 %PATH% 或当前工作目录中.您可以通过指定可执行文件的完整路径来缓解这种情况.

The error message means that Exec can't find pskill.exe, so the executable most likely isn't located in the %PATH% or the current working directory. You could mitigate that by specifying the full path to the executable.

但是,查询返回的对象Win32_Process 有一个 Terminate 方法.我建议使用它而不是炮轰:

However, the objects returned from querying Win32_Process have a Terminate method. I'd recommend using that instead of shelling out:

For Each Process in ProcessList
    Process.Terminate
Next

这篇关于杀死具有特定名称的所有进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆