【问题标题】:Cheking if a Windows process is running by using VBScript Always returning True使用 VBScript 检查 Windows 进程是否正在运行 始终返回 True
【发布时间】:2021-12-19 14:21:49
【问题描述】:

关于为什么这个脚本总是进入 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

【问题讨论】:

  • 使用局部变量来存储布尔标志,而不是直接将其分配给函数返回值。此外,布尔分配不需要Set,因为它仅用于分配对象实例。

标签: vbscript


【解决方案1】:

这是一个检查mspaint.exe是否正在运行并让一个实例运行的示例!


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
'-------------------------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 2015-05-28
    • 2018-04-12
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多