VBS Script To Monitor SQL Servers SQL Service

 

This VBS Script will allow you to monitor the SQL Server service state for machines in a text file called MachineList.Txt. If the service is stopped the color will be red.

 

VBS Script:

 

strServiceName = "MSSQLSERVER"

 

Set objExcel = CreateObject("Excel.Application")

objExcel.Visible = True

objExcel.Workbooks.Add

intRow = 2

 

objExcel.Cells(1, 1).Value = "Machine Name"

objExcel.Cells(1, 2).Value = "Results"

 

Set Fso = CreateObject("Scripting.FileSystemObject")

Set InputFile = fso.OpenTextFile("MachineList.Txt")

Do While Not (InputFile.atEndOfStream)

strComputer = InputFile.ReadLine

 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_Service Where Name = '" & strServiceName & "'")

For Each objItem in colItems

 

objExcel.Cells(intRow, 1).Value = objItem.SystemName

objExcel.Cells(intRow, 2).Value = objItem.State

If objExcel.Cells(intRow, 2).Value = "Stopped" Then

objExcel.Cells(intRow, 2).Font.ColorIndex = 3

End If

Next

intRow = intRow + 1

loop

 

objExcel.Range("A1:B1").Select

objExcel.Selection.Interior.ColorIndex = 19

objExcel.Selection.Font.ColorIndex = 11

objExcel.Selection.Font.Bold = True

objExcel.Cells.EntireColumn.AutoFit

 

MsgBox "Done"

 

 

Published Monday, February 16, 2009 2:48 PM by dhite
Filed under:

Comments

No Comments