Rem: Stopping an NT Service
Using the Net Stop command or sc.exe isn’t the only way you can stop NT services. Alternatively, you can use Active Directory Service Interfaces (ADSI) or Windows Management Instrumentation (WMI) to stop, start, and manage services.
June 24, 2001
I need to programmatically stop several Windows NT services. Do you know of an alternative to using the Net Stop command or the sc.exe utility?
You can use Microsoft Active Directory Service Interfaces (ADSI) or Windows Management Instrumentation (WMI) to programmatically stop, start, and manage services. Listing 4 shows VBScript code that uses ADSI to stop an NT service (i.e., Network Associates McShield); Listing 5 shows VBScript code that uses WMI to stop the same service.
To stop a service with ADSI, you need to use the IADsServiceOperations interface, which you access by binding to the service you want to stop on the target computer. As Listing 4 shows, you begin by setting the strComputer variable to the name of the target computer and the strService variable to the name of the NT service you want to stop. Be sure that you use the service's actual name (i.e., the name that NT uses internally for the service) rather than the service's display name (i.e., the name that appears in the Control Panel Services applet). The two names aren't always the same. Next, you use the strComputer and strService variables to create the WinNT provider path that you use with VBScript's GetObject function. The GetObject function uses this path to obtain an instance of the Service object that represents the NT service. After you assign that instance to the oService variable, you use the IADsServiceOperations interface's Stop property to stop the service. To learn more about using ADSI to manage services, see the ADSI IADsService and IADsServiceOperations interface docmentation at http://msdn.microsoft.com/library/psdk/adsi/if_pers_64v9.htm and http://msdn.microsoft.com/library/psdk/adsi/if_dyna_94s3.htm, respectively.
To stop a service with WMI, you use WMI's Win32_Service class. As Listing 5 shows, the WMI code is similar to the ADSI code. You begin by setting the strComputer and strService variables to the target computer and service you want to stop, respectively. Once again, make sure you use the service's actual name. Next, you use strComputer and strService variables to create the WMI moniker (i.e., the WinMgmts: path) that you use with the GetObject function. The GetObject function uses the moniker to obtain an instance of the Win32_Service class. After you assign that instance to the oService variable, you use the Win32_Service class's StopService method to stop the service. To learn more about using WMI to manage services, see the WMI Win32_Service class documentation at http://msdn.microsoft.com/library/psdk/wmisdk/clasops_4dyd.htm.
Although the VBScript code samples in Listing 4 and Listing 5 effectively illustrate how you can use ADSI and WMI to stop an NT service, the code is simplistic in that it doesn't handle errors or factor in NT service dependencies. When you create your service-management solution, you need to consider service dependencies specific to your environment. You also might want to add error-handling code. For information about handling errors in VBScript code, see Alistair G. Lowe-Norris, "Scripting Solutions with WSH and COM: Trapping and Handling Errors in Your Scripts," May 2001.
About the Author
You May Also Like