【问题标题】:How can I call a function in a vbscript with command line arguments?如何在带有命令行参数的 vbscript 中调用函数?
【发布时间】:2015-05-01 12:31:54
【问题描述】:

我有一个远程执行的脚本来查看程序是否正在运行。它工作正常,但我需要它来检查多个服务器上的多个程序,我不想重新编写它。我正在尝试查看是否可以通过命令行在 vbscript 中调用该函数,以便我可以使用 1 个脚本并在命令行更改参数。

Function IsProcessRunning(strComputer, strProcess)
    Dim Process, strObject
    IsProcessRunning = 0
    strObject   = "winmgmts://" & strComputer
    For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
    If UCase( Process.name ) = UCase( strProcess ) Then
        IsProcessRunning = 1 
        If IsProcessRunning = 1 Then
            WScript.echo 1 & ": " & strProcess & " is currently running on " & strComputer      
        End If
        Exit Function
    End If
    Next
    WScript.echo 0 & ": " & strProcess " is NOT running on " & strComputer
End Function

我希望能够通过 cmd 运行它,例如: run.vbs IsprocessRunning Server3 Program2.exe

【问题讨论】:

    标签: vbscript cmd command-line-arguments


    【解决方案1】:

    更新

    为什么不use WMIC?例如。输入命令行:

    wmic /node:server1 process where name='explorer.exe' get processid

    获取 server1 上所有已启动的资源管理器进程 ID。

    来源

    使用WScript.Arguments属性:

    IsProcessRunning WScript.Arguments(0), WScript.Arguments(1)
    
    Function IsProcessRunning(strComputer, strProcess)
        Dim Process, strObject
        IsProcessRunning = 0
        strObject = "winmgmts://" & strComputer
        For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
            If UCase(Process.name) = UCase(strProcess) Then
                IsProcessRunning = 1 
                WScript.echo 1 & ": " & strProcess & " is currently running on " & strComputer      
                Exit Function
            End If
        Next
        WScript.echo 0 & ": " & strProcess & " is NOT running on " & strComputer
    End Function
    

    最好添加一些检查是否为脚本提供了适当的命令行参数。

    【讨论】:

    • 谢谢。 wscript.arguments 效果很好。这是一个将在我的网络监控软件后端运行的脚本,因此它需要是 vbscript 并且必须具有格式化的 echo。
    猜你喜欢
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2011-01-29
    • 2012-11-12
    • 2015-08-20
    • 2019-09-09
    • 1970-01-01
    • 2020-03-22
    相关资源
    最近更新 更多