如何使用命令行参数调用 vbscript 中的函数?
How can I call a function in a vbscript with command line arguments?
我有一个远程执行的脚本来查看程序是否 运行ning。它工作正常,但我需要它来检查多个服务器上的多个程序,我不想重写它。我正在尝试查看是否可以通过命令行调用 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
更新
为什么不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
最好添加一些检查是否向脚本提供了适当的命令行参数。
我有一个远程执行的脚本来查看程序是否 运行ning。它工作正常,但我需要它来检查多个服务器上的多个程序,我不想重写它。我正在尝试查看是否可以通过命令行调用 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
更新
为什么不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
最好添加一些检查是否向脚本提供了适当的命令行参数。