zoukankan      html  css  js  c++  java
  • Stopping and Starting Dependent Services

    http://technet.microsoft.com/en-us/library/ee198770.aspx

    Stopping and Starting Dependent Services

    Microsoft® Windows® 2000 Scripting Guide

    Service dependencies are especially important when you try to stop services. To stop an antecedent service, you must first stop the dependent services. For example, if you attempt to stop the IIS Admin Service without first stopping dependent services such as FTP, SMTP, and World Wide Web Publishing Service, you receive an error message, and all the services continue to run.

    Dependencies also affect the order in which services start. To start a dependent service, the antecedent service must start first. If you are starting a dependent service such as FTP, the antecedent service (IIS Admin) automatically starts first. Only after IIS Admin starts does the FTP service start.

    However, starting the antecedent service first does not cause a dependent service to start. If you start the IIS Admin Service, that service itself starts, but its dependent services (such as FTP) do not automatically start at the same time.

    In other words, stopping and restarting an antecedent service sometimes involves stopping and restarting a number of dependent services. You could do this manually - coding all the dependencies within your script. The difficulty with this approach is twofold. First, you must determine all the dependencies and manually add them to the script. Second, if those dependencies ever change (because of a service or operating system upgrade), your script must be modified to reflect these changes.

    Alternatively, you can use WMI to enumerate the dependencies, and then stop and restart the appropriate services in the appropriate order. By using WMI, you avoid the problems of hard-coding service dependencies: you do not have to determine these dependencies beforehand, and you do not have to be concerned that changes to the operating system affect these dependencies. Instead, WMI determines the appropriate dependencies each time the script runs.

    Scripting Steps

    The scripts for stopping and starting dependent services perform similar steps but in the opposite order.

    Stopping dependent services

    Listing 15.16 contains a script that stops the IIS Admin Service and all its dependents. To carry out this task, the script must perform the following steps:

    1. Create a variable to specify the computer name.

    2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

    3. Use the ExecQuery method to query the Win32_Service class.

      This query must use an Associators of query and specify the following information:

      • The instance of the service on which the query is performed (Win32_Service.Name = 'IISAdmin').

      • The name of the Association class (AssocClass = Win32_DependentService). If the class name is not specified, the query returns all associated classes and their instances.

      • The role played by the IISAdmin Service. In this case, IISAdmin is Antecedent to the services to be returned by the query.

      The query returns a collection consisting of all the services dependent on the IIS Admin Service.

    4. For each service in the collection, use the StopService method to stop the service.

    5. After a stop control has been sent to each dependent service, pause for 60 seconds (60,000 milliseconds) to give the SCM time to stop each service.

    6. Use a the ExecQuery method to retrieve the instance of the IISAdmin Service.

    7. Use the StopService method to stop the IISAdmin Service.

    Listing 15.16 Stopping a Service and Its Dependents

      
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colServiceList = objWMIService.ExecQuery _
     ("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
     & "AssocClass=Win32_DependentService Role=Antecedent" )
    For Each objService in colServiceList
     errReturn = objService.StopService()
    Next
    Wscript.Sleep 60000
    Set colServiceList = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
    For Each objService in colServiceList
     errReturn = objService.StopService()
    Next
    

    Starting dependent services

    To start a service and all its dependents, simply reverse the process for stopping a service and its dependents: start the antecedent service, obtain a list of dependent services, and start each one. Listing 15.17 contains a script that starts the IIS Admin Service and all its dependents.

    Listing 15.17 Starting Dependent Services

      
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colServiceList = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
    For Each objService in colServiceList
     errReturn = objService.StartService()
    Next
    Wscript.Sleep 60000
    Set colServiceList = objWMIService.ExecQuery _
     ("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
     & "AssocClass=Win32_DependentService Role=Antecedent" )
    For Each objService in colServiceList
     objService.StartService()
    Next
    
  • 相关阅读:
    PHPCMS的自增长标签
    discuz!X2头像无法显示解决方法
    屏蔽“您目前使用的Discuz!程序有新版本发布,请及时升级!”提示
    python +Django 搭建web开发环境初步,显示当前时间
    Java之美[从菜鸟到高手演变]之设计模式四
    Java之美[从菜鸟到高手演变]之设计模式三
    Java之美[从菜鸟到高手演变]之设计模式二
    Java开发中的23种设计模式详解
    Cglib动态代理
    JSP 对象的作用范围
  • 原文地址:https://www.cnblogs.com/shineqiujuan/p/1990942.html
Copyright © 2011-2022 走看看