使用 VBScript 检查 Windows 进程是否为 运行 始终返回 True

Cheking if a Windows process is running by using VBScript Always returning True

为什么这个脚本总是进入 If 语句,即使进程不是 运行?

If isProcessRunning("Allplan_2022.exe") Then
    MsgBox "Allplan is running!"
    WScript.Quit
End If

Function isProcessRunning(ByVal processName)
    Dim objProcessList

    Set isProcessRunning = FALSE
    Set objProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process") 
 
    For Each item In objProcessList
        If item.Name = processName Then
            isProcessRunning = TRUE
            Exit For
        End If
    Next
End Function

这是一个检查mspaint.exe是否为运行ning的例子,让一个实例为运行 !


Option Explicit
Dim WS,ProcessName,Msg,Title
Title = "Cheking if a Windows process is running by using VBScript"
Set WS = CreateObject("wscript.shell")
ProcessName = "mspaint.exe"

If isProcessRunning("mspaint.exe") Then
    Msg = "ATTENTION ! There is another instance in execution !"
    ws.Popup Msg & VbCrLF &_
    chr(34) & ProcessName & chr(34),"5",Title,VbExclamation+vbSystemModal
    WScript.Quit(1)
End If

WS.Run ProcessName,1,False
'------------------------------------------------------------------------------------------
Function isProcessRunning(ProcessName)
    Dim objProcessList,item
    isProcessRunning = FALSE
    set objProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process") 
    For Each item In objProcessList
        If item.Name = processName Then
            isProcessRunning = TRUE
            Exit For
        End If
    Next
End Function
'-------------------------------------------------------------------------------------------