Windows Vista Forums

Monitor service
  1. #1


    Joe Blow Guest

    Monitor service

    I have a process where I have to stop a service and it dependencies, make a
    registry change, then restart the services.

    All of this is working as desired, but I'm adding some error checking into
    it to make it a little more bulletproof. So, I'm checking the state of a
    service. If it is running, I want to send a stop command (no problem
    there), after I send a stop command, I want to wait until the state =
    stopped before moving along to the next one. This is where I'm running into
    a snag. The problem is in the Function ServiceStatus. Basically, what I'm
    trying to do is requery the status to see if it's stopped yet, if it is not
    stopped, wait a second, then check again. Keep doing this until the
    service.state = stopped. The error is in Function ServiceState in the For
    Each line, I get error=0x80041017 source=null. I tried to emulate the
    function from the original query, but can't quite put my finger on what's
    wrong...

    Thanks for any advice!

    The relevant code is:

    Set objWMIService = GetObject("winmgmts:" &
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    StopService "'EnterpriseVaultAdminService'"
    Sub StopService(strService)
    Set colServiceList = objWMIService.ExecQuery("Associators of
    {Win32_Service.Name=" & strService & "} Where
    AssocClass=Win32_DependentService Role=Antecedent")
    For Each objService in colServiceList
    If objService.State = "Running" Then
    errReturn = objService.StopService()
    LogEntry objDocument, objService.Name & " sent stop command = " &
    errReturn & ":" & ErrorResult(errReturn,"Stop")
    Do While ServiceStatus(objService.Name) <> "Stopped"
    WScript.Sleep 1000
    Loop
    End If
    LogEntry objDocument, objService.Name & " = " & objService.State
    Next
    WScript.Sleep 15000
    Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service
    where Name=" & strService)
    For Each objService in colServiceList
    Do
    errReturn = objService.StopService()
    LogEntry objDocument, objService.Name & " sent stop command = " &
    errReturn & ":" & ErrorResult(errReturn,"Stop")
    WScript.Sleep 5000
    Loop Until (errReturn = 0) Or (errReturn = 5) Or (errReturn = 10)
    WScript.Sleep 15000
    Next
    End Sub
    Function ServiceStatus(ServiceName)
    Dim colService2
    Dim objService2
    Set colService2 = objWMIService.ExecQuery ("Select * from Win32_Service
    where Name=" & ServiceName)
    For Each objService2 in colService2
    ServiceStatus = objService2.State
    Next
    End Function





      My System SpecsSystem Spec

  2. #2


    Pegasus \(MVP\) Guest

    Re: Monitor service


    "Joe Blow" <joe@xxxxxx> wrote in message
    news:eStfIjbdJHA.1336@xxxxxx

    >I have a process where I have to stop a service and it dependencies, make a
    >registry change, then restart the services.
    >
    > All of this is working as desired, but I'm adding some error checking into
    > it to make it a little more bulletproof. So, I'm checking the state of a
    > service. If it is running, I want to send a stop command (no problem
    > there), after I send a stop command, I want to wait until the state =
    > stopped before moving along to the next one. This is where I'm running
    > into a snag. The problem is in the Function ServiceStatus. Basically,
    > what I'm trying to do is requery the status to see if it's stopped yet, if
    > it is not stopped, wait a second, then check again. Keep doing this until
    > the service.state = stopped. The error is in Function ServiceState in the
    > For Each line, I get error=0x80041017 source=null. I tried to emulate the
    > function from the original query, but can't quite put my finger on what's
    > wrong...
    >
    > Thanks for any advice!
    >
    > The relevant code is:
    >
    > Set objWMIService = GetObject("winmgmts:" &
    > "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    > StopService "'EnterpriseVaultAdminService'"
    > Sub StopService(strService)
    > Set colServiceList = objWMIService.ExecQuery("Associators of
    > {Win32_Service.Name=" & strService & "} Where
    > AssocClass=Win32_DependentService Role=Antecedent")
    > For Each objService in colServiceList
    > If objService.State = "Running" Then
    > errReturn = objService.StopService()
    > LogEntry objDocument, objService.Name & " sent stop command = " &
    > errReturn & ":" & ErrorResult(errReturn,"Stop")
    > Do While ServiceStatus(objService.Name) <> "Stopped"
    > WScript.Sleep 1000
    > Loop
    > End If
    > LogEntry objDocument, objService.Name & " = " & objService.State
    > Next
    > WScript.Sleep 15000
    > Set colServiceList = objWMIService.ExecQuery ("Select * from
    > Win32_Service where Name=" & strService)
    > For Each objService in colServiceList
    > Do
    > errReturn = objService.StopService()
    > LogEntry objDocument, objService.Name & " sent stop command = " &
    > errReturn & ":" & ErrorResult(errReturn,"Stop")
    > WScript.Sleep 5000
    > Loop Until (errReturn = 0) Or (errReturn = 5) Or (errReturn = 10)
    > WScript.Sleep 15000
    > Next
    > End Sub
    > Function ServiceStatus(ServiceName)
    > Dim colService2
    > Dim objService2
    > Set colService2 = objWMIService.ExecQuery ("Select * from Win32_Service
    > where Name=" & ServiceName)
    > For Each objService2 in colService2
    > ServiceStatus = objService2.State
    > Next
    > End Function
    You can either do this with a verbose script or with a tight batch file that
    uses the inbuilt and very powerful sc.exe tool, e.g. like so:
    01. @echo off
    02. set count=1
    03. set PC=MyPC
    04. set Service=W32Time
    05.
    06. :Again
    07. echo Attemp #%count%
    08. sc \\%PC% query "%Service%" | find /i "Stopped" > nul && goto :eof
    09. sc \\%PC% stop "%Service%
    10. ping localhost -n 10 > nul
    11. set /a count=%Count% + 1
    12. if %Count% LSS 11 goto Again



      My System SpecsSystem Spec

  3. #3


    Joe Blow Guest

    Re: Monitor service


    "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
    news:OPl$O$bdJHA.5648@xxxxxx

    >
    >
    > You can either do this with a verbose script or with a tight batch file
    > that uses the inbuilt and very powerful sc.exe tool, e.g. like so:
    > 01. @echo off
    > 02. set count=1
    > 03. set PC=MyPC
    > 04. set Service=W32Time
    > 05.
    > 06. :Again
    > 07. echo Attemp #%count%
    > 08. sc \\%PC% query "%Service%" | find /i "Stopped" > nul && goto :eof
    > 09. sc \\%PC% stop "%Service%
    > 10. ping localhost -n 10 > nul
    > 11. set /a count=%Count% + 1
    > 12. if %Count% LSS 11 goto Again
    >
    Please explain "verbose script". I would like to do this via VBS, mostly
    for academic reasons at this point, it's a real situation, and I want to do
    it in VBS for the learning I can get out of it.

    Is there a reason why my function isn't working? I'm quite stumped.

    Thanks,

    Joe



      My System SpecsSystem Spec

  4. #4


    Pegasus \(MVP\) Guest

    Re: Monitor service


    "Joe Blow" <joe@xxxxxx> wrote in message
    news:eFXv3IcdJHA.1860@xxxxxx

    >
    > "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
    > news:OPl$O$bdJHA.5648@xxxxxx

    >>
    >>
    >> You can either do this with a verbose script or with a tight batch file
    >> that uses the inbuilt and very powerful sc.exe tool, e.g. like so:
    >> 01. @echo off
    >> 02. set count=1
    >> 03. set PC=MyPC
    >> 04. set Service=W32Time
    >> 05.
    >> 06. :Again
    >> 07. echo Attemp #%count%
    >> 08. sc \\%PC% query "%Service%" | find /i "Stopped" > nul && goto :eof
    >> 09. sc \\%PC% stop "%Service%
    >> 10. ping localhost -n 10 > nul
    >> 11. set /a count=%Count% + 1
    >> 12. if %Count% LSS 11 goto Again
    >>
    > Please explain "verbose script". I would like to do this via VBS, mostly
    > for academic reasons at this point, it's a real situation, and I want to
    > do it in VBS for the learning I can get out of it.
    >
    > Is there a reason why my function isn't working? I'm quite stumped.
    >
    > Thanks,
    >
    > Joe
    "Verbose" means that your VB Script has some 1550 bytes of code and is not
    straightforward to understand, which may explain why you're stumped. The
    batch file solution has 271 bytes of code, 6 times less. Its meaning is more
    or less self-explanatory. I tend to be pragmatic: When I have two solutions
    for a given problem then I tend to select the simpler of the two, especially
    when I'm pressed for time (as you are at the moment, as stated in your
    previous thread).

    I can't tell you off the cuff why your function isn't working - I'd have to
    spend some time analysing it in detail.



      My System SpecsSystem Spec

  5. #5


    Joe Blow Guest

    Re: Monitor service


    "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
    news:uWVPEVcdJHA.2112@xxxxxx

    >
    >
    > "Verbose" means that your VB Script has some 1550 bytes of code and is not
    > straightforward to understand, which may explain why you're stumped. The
    > batch file solution has 271 bytes of code, 6 times less. Its meaning is
    > more or less self-explanatory. I tend to be pragmatic: When I have two
    > solutions for a given problem then I tend to select the simpler of the
    > two, especially when I'm pressed for time (as you are at the moment, as
    > stated in your previous thread).
    >
    > I can't tell you off the cuff why your function isn't working - I'd have
    > to spend some time analysing it in detail.
    Fair enough.

    I am not familiar with the SC command, does it stop all service dependencies
    like the script I'm using does?

    I apologize for giving the impression that I am pressed for time on this. I
    am not, the comment I made was because this is a real situation I am working
    through and the StdOut and ErrOut are not relevant to this project. By
    "when I have time" means when I have no other projects on my plate and can
    devote time to an academic exercise instead of a pragmatic one..

    Thanks,

    j



      My System SpecsSystem Spec

  6. #6


    Pegasus \(MVP\) Guest

    Re: Monitor service


    "Joe Blow" <joe@xxxxxx> wrote in message
    news:O%23XYFccdJHA.5412@xxxxxx

    >
    > "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
    > news:uWVPEVcdJHA.2112@xxxxxx

    >>
    >>
    >> "Verbose" means that your VB Script has some 1550 bytes of code and is
    >> not straightforward to understand, which may explain why you're stumped.
    >> The batch file solution has 271 bytes of code, 6 times less. Its meaning
    >> is more or less self-explanatory. I tend to be pragmatic: When I have two
    >> solutions for a given problem then I tend to select the simpler of the
    >> two, especially when I'm pressed for time (as you are at the moment, as
    >> stated in your previous thread).
    >>
    >> I can't tell you off the cuff why your function isn't working - I'd have
    >> to spend some time analysing it in detail.
    >
    > Fair enough.
    >
    > I am not familiar with the SC command, does it stop all service
    > dependencies like the script I'm using does?
    >
    > I apologize for giving the impression that I am pressed for time on this.
    > I am not, the comment I made was because this is a real situation I am
    > working through and the StdOut and ErrOut are not relevant to this
    > project. By "when I have time" means when I have no other projects on my
    > plate and can devote time to an academic exercise instead of a pragmatic
    > one..
    >
    > Thanks,
    >
    > j
    It's now 11pm where I live. I might have some free time tomorrow to have a
    look at this but it won't be until 5pm, 18 hours from now. If you need a
    reply sooner then you have two options:
    - Wait until someone else picks up this thread and works out your
    VB Script problem, or
    - Open a Command Prompt, type sc.exe /?, then play with the various
    subcommands shown. I would in particular check some service that
    has dependencies - this will take five minutes at most, seeing that
    I already gave you the syntax in my batch file.



      My System SpecsSystem Spec

  7. #7


    Joe Blow Guest

    Re: Monitor service


    "Pegasus (MVP)" <I.can@xxxxxx> wrote in message
    news:%233iwCqcdJHA.3856@xxxxxx

    >
    >
    > It's now 11pm where I live. I might have some free time tomorrow to have a
    > look at this but it won't be until 5pm, 18 hours from now. If you need a
    > reply sooner then you have two options:
    > - Wait until someone else picks up this thread and works out your
    > VB Script problem, or
    > - Open a Command Prompt, type sc.exe /?, then play with the various
    > subcommands shown. I would in particular check some service that
    > has dependencies - this will take five minutes at most, seeing that
    > I already gave you the syntax in my batch file.
    Have a good night. I appreciate your help up to this point.



      My System SpecsSystem Spec

  8. #8


    gimme_this_gimme_that Guest

    Re: Monitor service

    Hi Joe.

    Sorry. Without the entire script all I can do is read and comment. Not
    debug.

    Comments ....

    I'm puzzled why the For Each statement is in ServiceStatus instead of
    StopService.

    As an experienced VBScripter you probably won't find the next bit of
    advice useful - but here goes.

    Your script looks sort of funky with those sleep statements in it.

    It looks like you are only a small set of the functionality of those
    objects and that you're using sleep statements instead of
    functionality built into those objects.

    Objects often have either events (which you can attach code to) or
    properties which you can query in a Do loop.

    Sometimes you can get lucky and find documentation for objects at
    Microsoft's developer network.

    Usually you're on your own.

    If you're using a editor like vbsedit you can use that editor's
    autocomplete functionality to get a list of Subs and Functions.

    Other than that -

    The first step is to identify what object you have. For example, in
    your case, what is a objService2 object?

    So if you were writing the code in VBA instead of VBScript how would
    objService2 be Dimmed?

    The next step is to go into Excel, go into the VBA Editor. Check every
    library that that object might be a member of.

    Go into Excel's Object Browser, and then search for that object or for
    Functions or Subs you know are associated with that object.

    This is useful mostly for finding events - but often getting a list of
    all the functionality available to you in one spot is useful.

    Events are designated in the ObjectBrowser with a lightening bolt.

    For an example of how to attach code to Events see this thread:

    http://groups.google.com/group/micro...d1ceec7f752247



      My System SpecsSystem Spec

  9. #9


    Joe Blow Guest

    Re: Monitor service

    Inline comments:


    <gimme_this_gimme_that@xxxxxx> wrote in message
    news:c6ea3437-b8cf-43f8-8081-32de670ebb29@xxxxxx

    > Hi Joe.
    >
    > Sorry. Without the entire script all I can do is read and comment. Not
    > debug.
    >
    > Comments ....
    >
    > I'm puzzled why the For Each statement is in ServiceStatus instead of
    > StopService.
    >
    For Each is in both...

    The ServiceStatus Function is supposed to parse the appropriate dependent
    services and respond back if it's running, stopped, stop pending, etc.

    > As an experienced VBScripter you probably won't find the next bit of
    > advice useful - but here goes.
    >
    > Your script looks sort of funky with those sleep statements in it.
    Agreed, I don't like them either, but until I figure out how to monitor when
    a service is actually stopped, that's all I could come up with

    >
    > It looks like you are only a small set of the functionality of those
    > objects and that you're using sleep statements instead of
    > functionality built into those objects.
    Yep, that's what I'm trying to figure out

    >
    > Objects often have either events (which you can attach code to) or
    > properties which you can query in a Do loop.
    >
    > Sometimes you can get lucky and find documentation for objects at
    > Microsoft's developer network.
    >
    > Usually you're on your own.
    >
    > If you're using a editor like vbsedit you can use that editor's
    > autocomplete functionality to get a list of Subs and Functions.
    >
    Am using VBSEdit

    > Other than that -
    >
    > The first step is to identify what object you have. For example, in
    > your case, what is a objService2 object?
    >
    > So if you were writing the code in VBA instead of VBScript how would
    > objService2 be Dimmed?
    >
    > The next step is to go into Excel, go into the VBA Editor. Check every
    > library that that object might be a member of.
    >
    > Go into Excel's Object Browser, and then search for that object or for
    > Functions or Subs you know are associated with that object.
    >
    > This is useful mostly for finding events - but often getting a list of
    > all the functionality available to you in one spot is useful.
    >
    > Events are designated in the ObjectBrowser with a lightening bolt.
    >
    > For an example of how to attach code to Events see this thread:
    >
    > http://groups.google.com/group/micro...d1ceec7f752247
    >
    >
    I'll check your search for that, unfortunately I'm out of the office the
    rest of this week and likely won't be able to check back in ...

    Entire script is here, it's over 200 lines and I didn't want to post it,
    deal with line wrap, etc.

    http://tinyurl.com/9q2cl2

    j




      My System SpecsSystem Spec

  10. #10


    Joe Blow Guest

    Re: Monitor service

    I got it figured out!

    First problem was in the Function ServiceStatus, it was a typo. I got the
    function to work in a separate vbs project, then copy/pasted it into my
    script and it worked.

    The second problem was why it wasn't waiting for the service to stop before
    it went on to the next one. That's because my loop was check "this", wait,
    exit, and should be check "this", if it's not what you want then check it
    again.

    Working code is below, I apologize if I didn't get the word wrap just right:

    Set objWMIService = GetObject("winmgmts:" & _
    "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")

    StopService "'EnterpriseVaultAdminService'"

    Sub StopService(strService)
    Set colServiceList = objWMIService.ExecQuery_
    ("Associators of {Win32_Service.Name=" & strService & _
    "} Where AssocClass=Win32_DependentService Role=Antecedent")
    For Each objService in colServiceList
    If objService.State = "Running" Then
    errReturn = objService.StopService()
    Do While LCase(ServiceStatus(objService.Name)) <> "stopped"
    ServiceStatus(objService.Name)
    Loop
    End If
    Next
    Set colServiceList = objWMIService.ExecQuery_
    ("Select * from Win32_Service where Name = " & strService)
    For Each objService in colServiceList
    errReturn = objService.StopService()
    Do While LCase(ServiceStatus(objService.Name)) <> "stopped"
    ServiceStatus(objService.Name)
    Loop
    Next
    End Sub

    Function ServiceStatus(ServiceName)
    Dim objWMIService2, colRunningServices2, objService2
    Set objWMIService2 = GetObject("winmgmts:" & _
    "{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\cimv2")
    Set colRunningServices2 = objWMIService2.ExecQuery _
    ("Select * from Win32_Service Where name = '" & ServiceName & "'")
    For Each objService2 in colRunningServices2
    ServiceStatus = objService2.State
    Next
    End Function



      My System SpecsSystem Spec

Monitor service problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
single monitor stuck in dual monitor setting oldwing Graphic cards 19 13 Apr 2010
Peformance Monitor does not monitor/collect data Bryan L Server General 1 15 Sep 2009
Monitor a WCF service from operation point of view Kurt Biesemans Indigo 0 06 Mar 2008
Closing notebook monitor switches desktop to connected 2nd monitor kayko2000 Vista General 1 27 Jun 2007
4 screens on one monitor and monitor won't wake up from sleep mode rundyman Vista hardware & devices 0 21 May 2007