How Do You Shut Down Dependent Services? 
Author Message
 How Do You Shut Down Dependent Services?

I have the need to be able to shut down services using VBScript. I know
to use the StopService method of Win32_Service, but what do I do when it
errors out because there are dependent services still running?

Terry Poston

"The Miracle of Modern Technology" is that we get anything done using
it...



Sat, 16 Jul 2005 20:44:17 GMT  
 How Do You Shut Down Dependent Services?

Quote:

> I have the need to be able to shut down services using VBScript. I know
> to use the StopService method of Win32_Service, but what do I do when it
> errors out because there are dependent services still running?

Hi

This is very easy to do with Net stop by adding the /y switch:

net stop /y "lanmanserver"

With a vbscript:

sService = "LanManServer"

Set oShell = CreateObject("WScript.Shell")
oShell.Run "net stop /y " & Chr(34) & sService & Chr(34), 0, True

If you want to do it WMI, here is how:

sService = "LanManServer"

' "." for local computer
sNode = "."

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

' get all dependent services
Set cServiceList = oWMI.ExecQuery("Associators of " _
     & "{Win32_Service.Name='" & sService & "'} Where " _
     & "AssocClass=Win32_DependentService Role=Antecedent" )

' stop all dependent services
For Each oService in cServiceList
  If LCase(oService.State) <> LCase("Stopped") Then
    oService.StopService
    ' create a wait loop waiting for the service to be stopped.
    ' exit after 10 seconds if stop status is not obtained
    i = 0
    Do
      WScript.Sleep 500
      Set cResults = oWMI.ExecQuery("Select state from Win32_Service " _
        & "Where name='" & oService.Name & "'")
      For Each oService2 in cResults
        sServiceState = oService2.State
      Next
      i = i + 1
    Loop While (LCase(sServiceState) <> LCase("Stopped")) And (i < 20)
  End If
Next

' stop the main service
Set cServiceList = oWMI.ExecQuery _
     ("Select * from Win32_Service where Name='" & sService & "'")

For Each oService in cServiceList
  If LCase(oService.State) <> LCase("Stopped") Then
    oService.StopService
  End If
Next

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and a ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter



Sun, 17 Jul 2005 09:13:40 GMT  
 How Do You Shut Down Dependent Services?
Thanks! This is the first thing I've found that even attempts to answer
my question......

Terry Poston

Quote:


>> I have the need to be able to shut down services using VBScript. I know
>> to use the StopService method of Win32_Service, but what do I do when
>> it errors out because there are dependent services still running?

> Hi

> This is very easy to do with Net stop by adding the /y switch:

> net stop /y "lanmanserver"

> With a vbscript:

> sService = "LanManServer"

> Set oShell = CreateObject("WScript.Shell") oShell.Run "net stop /y " &
> Chr(34) & sService & Chr(34), 0, True

> If you want to do it WMI, here is how:

> sService = "LanManServer"

> ' "." for local computer
> sNode = "."

> Set oWMI = GetObject("winmgmts:" _
>      & "{impersonationLevel=impersonate}!\\" & sNode _ & "\root\cimv2")

> ' get all dependent services
> Set cServiceList = oWMI.ExecQuery("Associators of " _
>      & "{Win32_Service.Name='" & sService & "'} Where " _ &
>      "AssocClass=Win32_DependentService Role=Antecedent" )

> ' stop all dependent services
> For Each oService in cServiceList
>   If LCase(oService.State) <> LCase("Stopped") Then
>     oService.StopService
>     ' create a wait loop waiting for the service to be stopped. ' exit
>     after 10 seconds if stop status is not obtained i = 0 Do
>       WScript.Sleep 500
>       Set cResults = oWMI.ExecQuery("Select state from Win32_Service " _
>         & "Where name='" & oService.Name & "'")
>       For Each oService2 in cResults
>         sServiceState = oService2.State
>       Next
>       i = i + 1
>     Loop While (LCase(sServiceState) <> LCase("Stopped")) And (i < 20)
>   End If
> Next

> ' stop the main service
> Set cServiceList = oWMI.ExecQuery _
>      ("Select * from Win32_Service where Name='" & sService & "'")

> For Each oService in cServiceList
>   If LCase(oService.State) <> LCase("Stopped") Then
>     oService.StopService
>   End If
> Next

> --
> torgeir
> Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration
> scripting examples and a ONLINE version of the 1328 page Scripting
> Guide: http://www.microsoft.com/technet/scriptcenter



Mon, 18 Jul 2005 00:47:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using WMI to obtain dependent services (restarting services and all dependent services)

2. Shut down, how to intercept shut down?

3. Script to shut down Win2K service?

4. ASP service appears to shut down all the time

5. Shutting down WinNT Services

6. Trying to shut down Services on Remote Server on network

7. Shutting down WindowsNT 3.51 Services

8. shutting down, rebooting, power down local host???

9. Shutting down and powering down Win 2000

10. How to do complex drop down dependent lists in datagrid

11. Dependent drop down lists runtime error 5825

12. Shutting down W2k

 

 
Powered by phpBB® Forum Software