Dan Thomson at myITforum.com

Pacifying the call of an undying passion

Syndication

News


    If they don't find you handsome, maybe they'll find you handy (Red Green).
    Proud member of the myITforum Network

Links: Helpful forums

Links: Interesting blogs

Links: User Groups

Stuff I do

Restarting Services via WMI

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:

  • Check to see if the WinMgmt service was specified. If yes, the code exits.  This code depends on WMI afterall and it is possible to use this code to stop WMI, but not start it.
  • Use a WMI ping routine to check if the specified system is Online or Offline.
  • Connect to WMI on the specified system.
  • Check to see if the specified service exists. If no, the code exits.
  • Check to see if the specified service can accept a Stop control command. If no, the code exits.
  • Determines the current state of the specified service.
  • If the service state is "Start Pending, Continue Pending, Stop Pending, Pause Pending", the code will wait for that service to enter a normalized state "Running, Paused, Stopped".
  • If the service state is "Running or Paused", the code will:
    • Check to see if there are any services that depend on the specified service (dependent services). If yes, those dependent services will be stopped.
    • Stop the specified service.
    • Check to see if the specified service is dependent on any services (antecedent services). If yes, those services are stopped.
    • Goto Stop check below.
  • If the service state is "Stopped", the code will:
    • Check to see if the specified service is dependent on any services. If yes, those services are started.
    • Starts the specified service.
    • Check to see if there are any services that depend on the specified service. If yes, those dependent services will be started. An exception here is that the code will skip starting any services which have their StartMode set to "Disabled" or "Manual"

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:

  • WMI_Service_Restart  (The main procedure that gets things going)
  • WMI_Service_Exists (Determines if a service exists)
  • WMI_Service_CanAcceptCmd (Determines if a service can accept a Stop or Pause control command) 
  • WMI_Service_State_Get (Gets the current State of a service)
  • WMI_Service_State_Set (Sends a control command to a service. IOW: Sets a service to a new State)
  • WMI_Service_State_WaitOnChange (Waits for a service to enter a specified State)
  • WMI_Services_Error_Lookup (Looks through a list of WMI error codes to output error messages)
  • WMI_Error_Display (Uses SWbemLastError to lookup the last WMI error that occurred)
  • WriteOut (Outputs to screen and, if an object reference exists for objFile_Log, outputs to a file)
  • WMI_Ping (Uses WMI to ping a system to determine if it is Online or Offline)

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

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

Published Saturday, December 23, 2006 11:43 PM by dthomson
Filed under: ,

Comments

No Comments