VB Script to find a service status on mutiple server list
Below is the script to know a service status, whether it's Installed\running\stopped:
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1
Const ForWriting = 2
'filenames for input/output
strComputerList = "Servers.txt"
strDefaultLogFile = "Service Details.log"
strCSVFile = "Services.csv"
set input = WScript.StdIn
set output = WScript.StdOut
set wshobj = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set source = fso.OpenTextFile(strComputerList, ForReading)
Set target = fso.CreateTextFile(strDefaultLogFile, ForWriting)
Set CSVTarget = fso.CreateTextFile(strCSVFile, ForWriting)
CSVTarget.WriteLine "NetBiosName" & "," & "Servicename Status"
' Read the list of computers to check
Do While source.AtEndOfStream <> True
strComputer = source.Readline
'Target.writeline "Computer Name: " &strComputer
'CSVTarget.WriteLine "Service Name" & "," & "WU Service Status"
CSVTarget.WriteLine strComputer & "," & Services(strComputer)
Loop
target.close
source.close
Function Services(strComputer)
On Error Resume Next
strServiceState = ""
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
If Err.Number <> 0 Then
Services = "Error"
Wscript.Echo "Error Connecting to Machine."
Target.writeline "Error Connecting to Machine."
Exit Function
End IF
Set colServiceState = objWMIService.ExecQuery _
("SELECT * FROM Win32_service where Name = 'Servicename'")
For Each objServiceState in colServiceState
strServiceState = objServiceState.State
wscript.echo strServiceState
Target.Writeline "Servicename Status: " &strServiceState
Target.Writeline "####################"
Target.Writeline
Next
Services = strServiceState
End Function
------------------------------------------------------------------------------------------------------------------------
Where "Servers.txt" is the text file which contains the list of server names you like to know the service status and you should replace the "Servicename " field with the name of the service you would like to find the status.
Loading. . .
╔════════════════╗
║████████████ 99.99%
╚════════════════╝