This script parses a text file that has a list of servers and services for which you like to monitor. The script uses the split command to create an array from a csv type of file. Your csv file will need to look something like this:
Make sure that you edit the path to your text file contained in the variable txtFile. In addition, please ensure that you DO NOT have a space between the commas.
' NAME: <ArrayReadTxtFileCheckServices.vbs>
' COMMENT: Key concepts are listed below:
'1.Creates an array of servers and services from a text file
'2.Uses WMI to check on the status of those services on those servers
'3.Uses Win32_Service wmi class.
'4. uses file system object to open up a text file and read it.
'5. The csv file MUST not have any spaces between the commas, or script will fail
Option Explicit ' is used to force the scripter to declare variables
'On Error Resume Next ' is used to tell vbscript to go to the next line if it encounters an error
' Dim is used to declare varable names that are used in the script
Dim objFSO ' holds connection to file system object
Dim objTextFile ' holds hook to the text file
Dim arrServiceList ' produced by split command. an array of servers and services
Dim strNextLine ' produced by reading a line from the text file
Dim i, j ' counter variables for looping operations. nothing exciting
Dim TxtFile ' holds path to text file that has names of servers and services
dim boundary ' upper boundary of array. Changes with each line of text file
Dim computerName ' comes from element (0) in on each line of text file
Dim ServiceName ' comes from element (1) to upperboundary (ubound)
Dim wmiRoot ' target of WMI operation, includes moniker.
Dim wmiQuery ' carefully paramaterized query. Variable from array for the name of service
Dim objWMIService ' hook into the WMI repository
Dim colServices ' the collection of services that comes back from wmi query.
TxtFile = "E:\VBScriptWorkshop\Labs\Extras\\RealServersAndServices.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
Do until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
boundary = Ubound(arrServiceList)
computerName = arrServiceList(0)
WScript.echo "Status of services on " & computerName
wmiRoot = "winmgmts:\\" & computerName & "\root\cimv2"
Set objWMIService = GetObject(wmiRoot)
For i = 1 to boundary
ServiceName = arrServiceList(i)
wmiQuery = "Select state from Win32_Service where name = " & "'" & ServiceName & "'"
'WScript.echo wmiQuery 'debug statement
Set colServices = objWMIService.ExecQuery (wmiQuery)
For Each j In colServices
WScript.Echo vbtab & (servicename) & " Is: " & j.state