Check for Registry Keys on Remote Computers with VBScript

December 9, 2008 14:35 by randy

I guess this would be Part 2 on VBScripting – for today.

In the first post, Check Running Processes on Remote Computers with VBScript, I outlined a VBScript process to determine if a specific process was running on a remote computer on the same network.

In this post, I have a VBScript that will scan the registry of remote computers on the same network for a specific registry key.  List the remote processes script, this process utilizes a file containing a list of all machines on the network.  The registry key name is a local variable. It would not be too hard to take this script and put it into a function and pass the registry key name (and machine list file name for that matter) as a parameter.

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

Take a look.

' Set up some Constants
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForAppending = 8
Const ForReading = 1
 
' 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\reg_scan_log_" & sDate & ".txt"
 
' set this to the registry key to search
sKeyPath = "SYSTEM\CurrentControlSet\Services"
 
' Get the FSO moving
Set oFSO = CreateObject("Scripting.FileSystemObject")
 
' Open the file with containing all of the computers to scan (name or IP address)
Set objTextFile_List = oFSO.OpenTextFile ("C:\Temp\machineList.txt", ForReading)
' Create and open the file for logging
Set objTextFile = oFSO.OpenTextFile(sOutFileName, ForAppending, True)
 
' Loop through the file containing machine names/IP addresses
Do Until objTextFile_List.AtEndOfStream
    sMachine = objTextFile_List.Readline
 
 
    Set WshShell = CreateObject("WScript.Shell")
    bPing = Not CBool(WshShell.run("ping -n 1 " & sMachine, 0, True))      
 
    If bPing = True Then         
        Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sMachine & "\root\default:StdRegProv")
         
        ' Create an enum of the key values
        oReg.EnumValues HKEY_LOCAL_MACHINE, sKeyPath, arrValueNames, arrValueTypes
         
        ' duh 
        ON ERROR RESUME NEXT
        
        ' check the array count
        If UBound(arrValueNames) > 0 Then
            objTextFile.WriteLine(sMachine & ": " & cstr(UBound(arrValueNames)))
        End If
        
        ' write the error message
        If err.number > 0 Then
            objTextFile.WriteLine(sMachine & ": " & err.Description)
        End If
 
    Else
        ' Set the appropriate response
        objTextFile.WriteLine(sMachine & ": Not on network")
    End If
 
loop

Thanks for listening


Be the first to rate this post

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

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