Friday, March 4, 2011

Managing Windows services with Service Control (SC)

How many times you wanted to restart a Windows service and didn't remember the command line to do it. Here's a little memento for Service Control (SC) to use in these circunstamces.

Let's start with the command that will output the shortname from the display name:
SC GETKEYNAME "print spooler"
[SC] GetServiceKeyName SUCCESS  Name = Spooler
The other way around, to get the display name from the short service name type:
[SC] GetServiceDisplayName SUCCESS  Name = Print Spooler
If you don't know the display name nor the short name for a service, just run the following command to get a list of all the existing services with their state:
...then check out the name of the service you are interested in. Once you know the exact service name, you can perform reconfiguration as follows.

To change the startup mode for a service (automatic, manual or disabled) use:
SC CONFIG ServiceName = [auto|demand|disabled]
This command will directly modify the DWORD key named 'Start' in the registry under HKLM\SYSTEM\CurrentControlSet\Services\ServiceName.

For Win32 services the possible values in the registry are:
  • 2 for automatic
  • 3 for manual
  • 4 for disabled
For example, the Printer Spooler service status is registered under HKLM\SYSTEM\CurrentControlSet\services\Spooler. If the status is started, the value of Start key is 2. If the status is stopped, the value of Start key is 3.

Now, to start a service:
SC START ServiceName
And to stop it:
SC STOP ServiceName
Other useful switches you might want to know for Service Control are:
  • query: Queries the status for a service, or enumerates the status for types of services.
  • queryex: Queries the extended status for a service, or enumerates the status for types of services.
  • start: Starts a service.
  • pause: Sends a PAUSE control request to a service.
  • interrogate: Sends an INTERROGATE control request to a service.
  • continue: Sends a CONTINUE control request to a service.
  • stop: Sends a STOP request to a service.
  • config: Changes the configuration of a service (persistant).
  • description: Changes the description of a service.
  • failure: Changes the actions taken by a service upon failure.
  • sidtype: Changes the service SID type of a service.
  • qc: Queries the configuration information for a service.
  • qdescription: Queries the description for a service.
  • qfailure: Queries the actions taken by a service upon failure.
  • qsidtype: Queries the service SID type of a service.
  • delete: Deletes a service (from the registry).
  • create: Creates a service. (adds it to the registry).
  • control: Sends a control to a service.
  • sdshow: Displays a service's security descriptor.
  • sdset: Sets a service's security descriptor.
  • showsid: Displays the service SID string corresponding to an arbitrary name.
  • GetDisplayName: Gets the DisplayName for a service.
  • GetKeyName: Gets the ServiceKeyName for a service.
  • EnumDepend: Enumerates Service Dependencies.
I hope this article was helpful!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...