The below script will ping a server to see if it's up, check disk space, and check services.
I want to add a list of exclusions for the services, so that if certain services aren't running, that an alert is not generated.
I want to add a list of exclusions for the services, so that if certain services aren't running, that an alert is not generated.
Code:
'=======================================
'Configure Script
FreeSpaceSize = 0.500
'=======================================
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Check that ServerList.txt exists
If Not objFSO.FileExists("ServerList.txt") Then
WScript.Echo "File ServerList.txt Doesn't Exist. Create file ServerList.txt in same folder as script."
WScript.Echo "Add server names to this file, each on it's own line."
WScript.Quit
End If
'Check that ExcludedServices.txt exists
If Not objFSO.FileExists("ExcludedServices.txt") Then
WScript.Echo "File ExcludedServices.txt Doesn't Exist. Create file ExcludedServices.txt in same folder as script."
WScript.Echo "Add services to this file, each on it's own line."
WScript.Quit
End If
Set objFileServers = objFSO.OpenTextFile("ServerList.txt", 1)
Set objFileExcludedServices = objFSO.OpenTextFile("ExcludedServices.txt", 1)
'Read's file one line at a time until the end of the file
Do Until objFileServers.AtEndOfStream
strLine = objFileServers.ReadLine
'Checks if server is up
If Ping(strLine) = True Then
'Checks disk space
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strLine & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk Where DriveType = 3 ")
WScript.Echo vbCrLf & "" & Ucase(strLine) & " disk space check"
For Each objDisk in colDisks
If FormatNumber (intFreeSpace /1024/1024/1024 ,2) < FreeSpaceSize Then
WScript.Echo "Disk " & objDisk.DeviceID & " has " & Formatnumber(objDisk.Freespace / 1024/1024/1024 ,2) & " GB free space"
End If
Next
'Checks services
Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")
WScript.Echo vbCrLf & "" & Ucase(strLine) & " services check"
For Each objService in colRunningServices
If objService.State = "Stopped" And objService.StartMode = "Auto" Then
'Wscript.Echo ExcludedServices(objService.DisplayName)
WScript.Echo objService.DisplayName & VbTab & objService.State
End If
Next
Else
WScript.Echo "Warning - Server Down"
End If
Loop
objFileServers.Close()
objFileExcludedServices.Close()
Function Ping(strHost)
Dim objPing, objRetStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strHost & "'")
For Each objRetStatus In objPing
If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode<>0 Then
Ping = False
Else
Ping = True
End If
Next
End Function
'Function ExcludedServices(ServiceName)
' Do Until objFileExcludedServices.AtEndOfStream
' strLine = objFileExcludedServices.ReadLine
' Wscript.Echo "* File " & strLine
' Wscript.Echo "* Service " & ServiceName
' If strLine = ServiceName Then
' ExcludedServices = False
' Else
' ExcludedServices = True
' End If
' Loop
'End Function