I recently had a conversation with someone who was feeling WMI was failing them in being able to properly restart services and they decided to rely on shelling out to an external executable to perform this task.
I have spent a little bit of time coming up with some code that should demonstrate the proper methods for restarting services via WMI.
There are a lot of comments in the code, so the code ended up being fairly large and is in excess of 1000 lines, but it works pretty well in my testing. It is so big mainly due to the technique of keeping it fairly modular by way of using many procedures and functions to perform tasks.
The code will basically do the following:
It can be specified that the code should A) Not wait, B) Wait for a specified amount of time, or C) Wait indefinitely for each service to enter the newly specified state (Started, Stopped, Paused). This wait ensures that the code does not proceed until the service is ready.
There are a number of procedures and functions that make up the code. They are:
Here is some sample output (with most blank lines removed) by calling the procedure as follows:
Call WMI_Service_Restart(".", "", "", "spooler", 300)Call WMI_Service_Restart(".", "", "", "Symantec AntiVirus", 300)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Starting management of the spooler service on .. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Connecting to WMI on . ----------------------------------------------------------------------------- Checking if the spooler service exists on .. ----------------------------------------------------------------------------- Query WMI for the spooler service. The spooler service exists. ----------------------------------------------------------------------------- Checking if the spooler service can accept a Stop control command. ----------------------------------------------------------------------------- Connecting to the spooler service. The spooler can accept a Stop command. ----------------------------------------------------------------------------- Checking the state of the spooler service. ----------------------------------------------------------------------------- Connecting to the spooler service. The service state is: Running ----------------------------------------------------------------------------- Stopping services that depend on the spooler service. ----------------------------------------------------------------------------- Fax service. Connecting to the Fax service. Sending service command: Stop WMI error lookup: Code: 0 Description: The request was accepted. *Wait 300 seconds for the Fax service to enter a Stopped state. Connecting to the Fax service. The service state is: Stopped ----------------------------------------------------------------------------- Stopping the spooler service. ----------------------------------------------------------------------------- Connecting to the spooler service. Sending service command: Stop WMI error lookup: Code: 0 Description: The request was accepted. *Wait 300 seconds for the spooler service to enter a Stopped state. Connecting to the spooler service. The service state is: Stopped ----------------------------------------------------------------------------- Starting services that the spooler service depends on. ----------------------------------------------------------------------------- RpcSs service. Connecting to the RpcSs service. Sending service command: Start WMI error lookup: Code: 10 Description: The service is already running. ----------------------------------------------------------------------------- Starting the spooler service. ----------------------------------------------------------------------------- Connecting to the spooler service. Sending service command: Start WMI error lookup: Code: 0 Description: The request was accepted. *Wait 300 seconds for the spooler service to enter a Running state. Connecting to the spooler service. The service state is: Running ----------------------------------------------------------------------------- Starting services that depend on the spooler service. ----------------------------------------------------------------------------- Fax service. Connecting to the Fax service. Sending service command: Start WMI error lookup: Code: 0 Description: The request was accepted. *Wait 300 seconds for the Fax service to enter a Running state. Connecting to the Fax service. The service state is: Start Pending Connecting to the Fax service. The service state is: Start Pending Connecting to the Fax service. The service state is: Stopped Connecting to the Fax service. The service state is: Start Pending Connecting to the Fax service. The service state is: Start Pending Connecting to the Fax service. The service state is: Running ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Done managing the spooler service. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Starting management of the Symantec AntiVirus service on .. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Connecting to WMI on . ----------------------------------------------------------------------------- Checking if the Symantec AntiVirus service exists on .. ----------------------------------------------------------------------------- Query WMI for the Symantec AntiVirus service. The Symantec AntiVirus service does not exist. Exiting
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Starting management of the spooler service on ..
Connecting to WMI on .
-----------------------------------------------------------------------------
Checking if the spooler service exists on ..
Query WMI for the spooler service.
The spooler service exists.
Checking if the spooler service can accept a Stop control command.
Connecting to the spooler service.
The spooler can accept a Stop command.
Checking the state of the spooler service.
The service state is: Running
Stopping services that depend on the spooler service.
Fax service.
Connecting to the Fax service.
Sending service command: Stop
WMI error lookup:
Code: 0
Description: The request was accepted.
*Wait 300 seconds for the Fax service to enter a Stopped state.
The service state is: Stopped
Stopping the spooler service.
*Wait 300 seconds for the spooler service to enter a Stopped state.
Starting services that the spooler service depends on.
RpcSs service.
Connecting to the RpcSs service.
Sending service command: Start
Code: 10
Description: The service is already running.
Starting the spooler service.
*Wait 300 seconds for the spooler service to enter a Running state.
Starting services that depend on the spooler service.
*Wait 300 seconds for the Fax service to enter a Running state.
The service state is: Start Pending
Done managing the spooler service.
Starting management of the Symantec AntiVirus service on ..
Checking if the Symantec AntiVirus service exists on ..
Query WMI for the Symantec AntiVirus service.
The Symantec AntiVirus service does not exist.
Exiting
The only thing I can think of that the code might need is to make it so that it checks all levels of dependent and antecedent services and performs the specified actions on them all. Currently the code only looks one level for dependent and antecedent services that are associated with the user specified service.
The code is attached to this post (save it with a .vbs extension).
I hope you find it useful.
Dan
Posted Saturday, December 23, 2006 11:43 PM by dthomson | with no comments