2

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.

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

BSanders
  • 295
  • 1
  • 6
  • 29

1 Answers1

2

UPDATE

Why not to use 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.

SOURCE

Use WScript.Arguments property:

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.

omegastripes
  • 12,351
  • 4
  • 45
  • 96
  • thanks. the wscript.arguments works great. this is a script that will run on the back end of my network monitoring software so that is why it needs to be the vbscript and has to have the formatted echo. – BSanders Mar 01 '15 at 02:49