在 Vbscript 中终止进程

发布于 2024-09-10 23:26:41 字数 549 浏览 6 评论 0原文

我试图终止名为“AetherBS.exe”的进程的所有实例,但以下 VBscript 不起作用。我不太确定在哪里/为什么会失败。

那么我怎样才能杀死“AetherBS.exe”的所有进程呢?

CloseAPP "AetherBS.exe"

Function CloseAPP(Appname)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Process", , 48)
    For Each objItem In colItems
        If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then
            objItem.Terminate
        End If
    Next
End Function

I am trying to kill all instances of a process called "AetherBS.exe" but the following VBscript is not working. I am not exactly sure where/why this is failing.

So how can I kill all process of "AetherBS.exe?"

CloseAPP "AetherBS.exe"

Function CloseAPP(Appname)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Process", , 48)
    For Each objItem In colItems
        If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then
            objItem.Terminate
        End If
    Next
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眉目亦如画i 2024-09-17 23:26:41

这是终止进程的函数:

Sub KillProc( myProcess )
'Authors: Denis St-Pierre and Rob van der Woude
'Purpose: Kills a process and waits until it is truly dead

    Dim blnRunning, colProcesses, objProcess
    blnRunning = False

    Set colProcesses = GetObject( _
                       "winmgmts:{impersonationLevel=impersonate}" _
                       ).ExecQuery( "Select * From Win32_Process", , 48 )
    For Each objProcess in colProcesses
        If LCase( myProcess ) = LCase( objProcess.Name ) Then
            ' Confirm that the process was actually running
            blnRunning = True
            ' Get exact case for the actual process name
            myProcess  = objProcess.Name
            ' Kill all instances of the process
            objProcess.Terminate()
        End If
    Next

    If blnRunning Then
        ' Wait and make sure the process is terminated.
        ' Routine written by Denis St-Pierre.
        Do Until Not blnRunning
            Set colProcesses = GetObject( _
                               "winmgmts:{impersonationLevel=impersonate}" _
                               ).ExecQuery( "Select * From Win32_Process Where Name = '" _
                             & myProcess & "'" )
            WScript.Sleep 100 'Wait for 100 MilliSeconds
            If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
                blnRunning = False
            End If
        Loop
        ' Display a message
        WScript.Echo myProcess & " was terminated"
    Else
        WScript.Echo "Process """ & myProcess & """ not found"
    End If
End Sub

用法:

KillProc "AetherBS.exe"

Here is the function to kill the process:

Sub KillProc( myProcess )
'Authors: Denis St-Pierre and Rob van der Woude
'Purpose: Kills a process and waits until it is truly dead

    Dim blnRunning, colProcesses, objProcess
    blnRunning = False

    Set colProcesses = GetObject( _
                       "winmgmts:{impersonationLevel=impersonate}" _
                       ).ExecQuery( "Select * From Win32_Process", , 48 )
    For Each objProcess in colProcesses
        If LCase( myProcess ) = LCase( objProcess.Name ) Then
            ' Confirm that the process was actually running
            blnRunning = True
            ' Get exact case for the actual process name
            myProcess  = objProcess.Name
            ' Kill all instances of the process
            objProcess.Terminate()
        End If
    Next

    If blnRunning Then
        ' Wait and make sure the process is terminated.
        ' Routine written by Denis St-Pierre.
        Do Until Not blnRunning
            Set colProcesses = GetObject( _
                               "winmgmts:{impersonationLevel=impersonate}" _
                               ).ExecQuery( "Select * From Win32_Process Where Name = '" _
                             & myProcess & "'" )
            WScript.Sleep 100 'Wait for 100 MilliSeconds
            If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
                blnRunning = False
            End If
        Loop
        ' Display a message
        WScript.Echo myProcess & " was terminated"
    Else
        WScript.Echo "Process """ & myProcess & """ not found"
    End If
End Sub

Usage:

KillProc "AetherBS.exe"
萌酱 2024-09-17 23:26:41

问题出在以下行:

If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then

这里将 Win32_Process.Name 属性值转换为大写,但不将 Appname 转换为大写。默认情况下,InStr 执行区分大小写的搜索,因此如果输入字符串相同但大小写不同,您将不会获得匹配项。

要解决此问题,您也可以将 Appname 转换为大写:

If InStr(1, UCase(objItem.Name), UCase(Appname)) >= 1 Then

或者您可以使用 vbTextCompare 参数忽略字母大小写:

If InStr(1, objItem.Name, Appname, vbTextCompare) >= 1 Then

但是,实际上根本不需要此检查,因为您可以将其直接合并到查询中:

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Process WHERE Name='" & Appname & "'", , 48)

The problem is in the following line:

If InStr(1,Ucase(objItem.Name),Appname) >= 1 Then

Here you convert the Win32_Process.Name property value to uppercase, but don't convert the Appname to uppercase. By default, InStr performs a case-sensitive search, so if the input strings are the same but differ in case, you won't get a match.

To fix the problem, you can convert Appname to uppercase as well:

If InStr(1, UCase(objItem.Name), UCase(Appname)) >= 1 Then

or you can use the vbTextCompare parameter to ignore the letter case:

If InStr(1, objItem.Name, Appname, vbTextCompare) >= 1 Then

However, there's actually no need in this check at all as you can incorporate it directly in your query:

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Process WHERE Name='" & Appname & "'", , 48)
最冷一天 2024-09-17 23:26:41

批处理脚本尝试下面的操作

wmic path win32_process Where "Caption Like '%%AetherBS.exe%%'" Call Terminate

使用 cmd 行中的

wmic path win32_process Where "Caption Like '%AetherBS.exe%'" Call Terminate

Try out below with batch script

wmic path win32_process Where "Caption Like '%%AetherBS.exe%%'" Call Terminate

from cmd line use

wmic path win32_process Where "Caption Like '%AetherBS.exe%'" Call Terminate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文