简体   繁体   English

如何在带有命令行参数的vbscript中调用函数?

[英]How can I call a function in a vbscript with command line arguments?

I have a script that executes remotely to see if a program is running. 我有一个脚本,可以远程执行以查看程序是否正在运行。 It works fine but I need it to check for several programs on several servers and I don't want to re-write it. 它工作正常,但是我需要它来检查多个服务器上的多个程序,并且我不想重写它。 I'm trying to see if I can call the function within the vbscript via command line so that I can use 1 script and change the arguments at the command line. 我试图查看是否可以通过命令行在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

What I'm hoping for is to be able to run this via cmd like: run.vbs IsprocessRunning Server3 Program2.exe 我希望能够通过cmd像这样运行:run.vbs IsprocessRunning Server3 Program2.exe

UPDATE UPDATE

Why not to use WMIC ? 为什么不使用WMIC E. g. 例如 type in command line: 在命令行中输入:

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

to get all launched explorers process ID on server1. 在server1上获取所有启动的资源管理器进程ID。

SOURCE 资源

Use WScript.Arguments property: 使用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

Better to add some check if the appropriate command line arguments provided to script. 最好添加一些检查,以便为脚本提供适当的命令行参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM