Check Running Processes on Remote Computers with VBScript

December 9, 2008 14:09 by randy

When things get muddy in a small IT shop, it is time for everyone to help out!

That is how a developer gets pulled into helping the network and Help Desk folks.

That said, I created this quick (kinda) and dirty (definitely) VBScript to check for a specific process running on each of the machines on the network.  The process utilizes a file containing a list of all machines on the network.  The process name is a local variable. It would not be too hard to take this script and put it into a function and pass the process name (and machine list file name for that matter) as a parameter.

This script utilizes Scripting API for WMI to interrogate the remote machines.

Take a look.

' Set up some constants
Const ForReading = 1
Const ForAppending = 8
 
' get the FSO open
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
' Process you are looking for
sProcess = "csrsc.exe"
 
' Build the date tag for the log file name
sDate = right("00" & cstr(month(date())),2) & right("00" & cstr(day(date())),2) & right("200" & cstr(year(date())),4) & right("00" & cstr(hour(time)),2) & right("00" & cstr(minute(time)),2) & right("00" & cstr(second(time)),2)
' Build the full log file name
sOutFileName = "C:\Temp\scan_log_" & sDate & ".txt"
 
' Open the file with containing all of the computers to scan (name or IP address)
Set objTextFile = objFSO.OpenTextFile ("C:\Temp\machineList.txt", ForReading)
' Create and open the file for logging
Set objTextFile_Write = objFSO.OpenTextFile(sOutFileName, ForAppending, True)
 
' Loop through the file containing machine names/IP addresses
Do Until objTextFile.AtEndOfStream
    ' get the machine name from the current row of the file
    sMachine = objTextFile.Readline
    
    ' duh
    Dim Process, sResponse, sWriteMe
 
    ' Default the test response
    sResponse = "No"
 
    ' duh
    ON ERROR RESUME NEXT
    
    '   Ping the machine to make sure it is on the network
    Set WshShell = CreateObject("WScript.Shell")
    bPing = Not CBool(WshShell.run("ping -n 1 " & sMachine, 0, True))      
 
    ' only check for the process if the machine is on the network
    If bPing = True Then
        Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set cProcList = objWMI.ExecQuery ("Select * from Win32_Process Where Name = '" & sProcess & "'")
  
        ' if there are items in the collection ....
        If cProcList.Count > 0 Then
            ' Found it!
            sResponse = "Yes"
        End If
   
    Else
        ' Set the appropriate response
        sResponse = "Not on network"
    End If
    
    ' If there was an error ... Log it!    
    If err.number > 0 Then
        sResponse = err.Description
    End If
    
    sWriteMe = sMachine & ": " & "Running " & sProcess & " - " & sResponse
    objTextFile_Write.WriteLine(sWriteMe)
    
  Loop

Thanks for listening


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading