Check Sevice Status
Check Sevice Status
(OP)
Hi
I have a script that goes away and checks free space on drives of a list of servers.
I need to add functionality to this script to query the status of specific services (started, stopped)
For example I need to check if the IIS services is running or not.
Can anybody tell me if this is possible, and if so how.
Cheers
I have a script that goes away and checks free space on drives of a list of servers.
I need to add functionality to this script to query the status of specific services (started, stopped)
For example I need to check if the IIS services is running or not.
Can anybody tell me if this is possible, and if so how.
Cheers
RE: Check Sevice Status
Set objComputer = GetObject("WinNT://computername,computer")
objComputer.Filter = Array("Service")
For Each aService In objComputer
Wscript.Echo aService.Name & "=" & aService.Status
Next
RE: Check Sevice Status
http:
Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
RE: Check Sevice Status
Thanks PHV that is one of the best scripting tools ive ever seen.
Mrmovie how do I specify the service im interested in, in the following line
Set objComputer = GetObject("WinNT://computername,computer")
objComputer.Filter = Array("Service")
does "service" represent the name of the service or the .exe that the service runs under.
for example, how would i specify the alerter service in this code.
cheers
RE: Check Sevice Status
objComputer.Filter = Array("Service")
strServiceName = "blaablaa"
For Each aService In objComputer
If LCase(strServiceName) = LCase(aService.Name) Then
Wscript.Echo aService.Name & "=" & aService.Status
End If
Next
or to bind directly to a service
Set objComputer = GetObject("WinNT://computername,computer")
Set objService = objCOmputer.GetObject("service", "blaablaa")
Msgbox objService.Name & objService.Status
the .Filter can set set to any class the computer object contains
RE: Check Sevice Status
the .Filter is literally a 'Filter'.
you are filtering on "service"'s so after applying the 'Filter' on the Computer object it only contains service objects for the For Each to loop through
RE: Check Sevice Status
RE: Check Sevice Status
"computername" needs to be replaved with the netbios name of the machine you want to question...so you might want something like
GetOBject("WinNT://" & strComputerName & ",computer")
the ",computer" is telling the WinNT provider that you are interested in a 'computer' object
"WinNT" is case-sensetive....i think
RE: Check Sevice Status
RE: Check Sevice Status
"computername" needs to be replaved with the netbios name of the machine you want to question...so you might want something like
GetOBject("WinNT://" & strComputerName & ",computer")
the ",computer" is telling the WinNT provider that you are interested in a 'computer' object
"WinNT" is case-sensetive....i think
RE: Check Sevice Status
How can I modify this script to specify certain services.
On Error Resume Next
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array("localhost")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
WScript.Echo "DisplayName: " & objItem.DisplayName
WScript.Echo "State: " & objItem.State
WScript.Echo "Status: " & objItem.Status
WScript.Echo
Next
Next
Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function
Cheers
RE: Check Sevice Status
Set objComputer = GetObject("WinNT://computername,computer")
objComputer.Filter = Array("Service")
strServiceName = "blaablaa"
For Each aService In objComputer
If LCase(strServiceName) = LCase(aService.Name) Then
Wscript.Echo aService.Name & "=" & aService.Status
End If
Next
this does exactly what you want in about 8 lines of code compared to the WMI approach.
RE: Check Sevice Status
When I run your code, replacing blaablaa with alerter, I get the result alerter = 4
I need this to say running or stopped.
Are there any other variables i can put after the aservice, such as aservice.status to display the results i need.
Cheers
RE: Check Sevice Status
Select Case aService.Status
Case 4
Case 1
etc, etc,
reference MSDN for what all the integer codes mean, there are about
1 = stopped
2 = start pending
3 = stop pending
4 = running
5 = continue_poending
6 = pause_pending
7 = paused
8 = error
you can then do stuff like
If Service.Status = 4 Then
Service.Stop
End If
RE: Check Sevice Status
Thank you again for all your help
Cheers
RE: Check Sevice Status
RE: Check Sevice Status
"." means the local computer. the same with the services you want to retrieve the status from. As you can see, the code does not have to be long when using WMI.
htt
ArrComputer = Array(".")
ArrServices = Array("Alerter", "DHCP Client", "DNS Client")
For Each strComputer In ArrComputer
For Each Service In ArrServices
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName = '" & Service & "'")
For Each objItem in colItems
Wscript.echo "DisplayName: " & objItem.DisplayName
Wscript.echo "State: " & objItem.State & VbCrLf
Next
Next
Next
RE: Check Sevice Status
you should have had one WMI query per machine, and you should have then nested your service array query match below that.
you might have less lines but it will take forever!!
RE: Check Sevice Status
ArrComputer = Array(".")
ArrServices = Array("Alerter", "DHCP Client", "DNS Client")
For Each strComputer In ArrComputer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
For Each objItem in colItems
For Each Service In ArrServices
If Service = objItem.DisplayName Then
Wscript.echo "DisplayName: " & objItem.DisplayName
Wscript.echo "State: " & objItem.State & VbCrLf
End If
Next
Next
Next
RE: Check Sevice Status
For error handlin i would also recommend
.....
Set objWMIService = Nothing
Set colItems = Nothing
Next
RE: Check Sevice Status
ArrComputer = Array(".")
ArrServices = Array("Alerter", "DHCP Client", "DNS Client")
i = 0
For Each strComputer In ArrComputer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
For Each Service In ArrServices
If i > 0 Then
str = str & " or DisplayName = '" & Service & "'"
Else
str = "DisplayName = '" & Service & "'"
i = i + 1
End If
Next
Set colItems = objWMIService.ExecQuery("Select DisplayName, State from Win32_Service where " & str)
For Each objItem in colItems
Wscript.echo "DisplayName: " & objItem.DisplayName
Wscript.echo "State: " & objItem.State & VbCrLf
Next
Next