如何使用命令行参数调用vbscript中的函数?

时间:2015-02-28 23:11:08

标签: vbscript cmd command-line-arguments

我有一个远程执行的脚本,以查看程序是否正在运行。它工作正常,但我需要它来检查几个服务器上的几个程序,我不想重写它。我试图看看是否可以通过命令行在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

1 个答案:

答案 0 :(得分:1)

<强>更新

为什么不到use WMIC? E. g。在命令行输入:

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

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