Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check Sevice Status 4

Status
Not open for further replies.

grabrail

Technical User
Aug 15, 2001
269
GB
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
 
you can use WMI and Win32_Service class, should find loads of examples on the site. i find ADSI more readable

Set objComputer = GetObject("WinNT://computername,computer")
objComputer.Filter = Array("Service")

For Each aService In objComputer
Wscript.Echo aService.Name & "=" & aService.Status
Next
 
Hi

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

 
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


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
 
the second will throw an error if the service doesnt exist, so you can be defensive with the iteration (i.e. first example) or you can wrap in On Error Resume Next and trap the Err.Number

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
 
Sorry one last question what is //computername, computer mean
 
the "//" is just standard moniker string syntax and is always needed

"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
 
the "//" is just standard moniker string syntax and is always needed

"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
 
Ok I have the following script defined which lists all the services and there status on the target computer

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
 
total waste of time me posting on this subject it would appear.


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.
 
I do appreciate all the help you are giving me, I am just looking at a few options at the moment.

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
 
try a

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
 
Superb

Thank you again for all your help

Cheers
 
This should work. Simply seperate the computer names you want to query by a comma where it says Array(".")
"." 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.

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
 
unfortunately dm4ever i dont think you could have coded a service query using WMI in a more unefficient manor. for each service you which to query you create a new connection to WMI on a machine, this is not how you should have coded it.
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!!
 
Thanks mrmovie. I see your point. How about this then? One query per machine.

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
 
yeap much better, in theory using a dictionary object (hashed array) with a "If dicServices.Exists(objItem.DisplayName)" will be even quicker than the "For Each Service In ArrServices" iteration as well

:)

For error handlin i would also recommend

.....
Set objWMIService = Nothing
Set colItems = Nothing
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top