×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Check Sevice Status
4

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

RE: Check Sevice Status

3
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

RE: Check Sevice Status

(OP)
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

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


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

RE: Check Sevice Status

(OP)
Sorry one last question what is //computername, computer mean

RE: Check Sevice Status

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

RE: Check Sevice Status

(OP)
Thank you very much for all your help

RE: Check Sevice Status

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

RE: Check Sevice Status

(OP)
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

RE: Check Sevice Status

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.

RE: Check Sevice Status

(OP)
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

RE: Check Sevice Status

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

RE: Check Sevice Status

(OP)
Superb

Thank you again for all your help

Cheers

RE: Check Sevice Status

glad you have a resolution

RE: Check Sevice Status

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.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_service.asp

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

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!!

RE: Check Sevice Status

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

RE: Check Sevice Status

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

smile

For error handlin i would also recommend
   
.....
   Set objWMIService = Nothing
   Set colItems = Nothing
Next

RE: Check Sevice Status

this may be another posibility. you limit what you want returned "DisplayName" and "Status" and you only query those services you want only once on the PC.


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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close