Wednesday, April 04, 2012

Service Control - Windows Service

What is Service Control?

The SC command duplicates some aspects of the NET command but adds the ability to create a service.
You can use Service Control(SC.exe) to Create, Start, Stop, Query or Delete any Windows SERVICE. The command options for SC are case sensitive.
Syntax: SC [\\server] [command] [service_name] [Options]

Key's:
  1. server: The machine where the service is running
  2. service_name: The KeyName of the service, this is often but not always the same as the DisplayName shown in Control Panel, Services.
  3. You can get the KeyName by running: SC GetKeyName <DisplayName>
Commands:
  1. query  [qryOpt] - Show status
  2. queryEx [qryOpt] - Show extended info - pid, flags
  3. GetDisplayName - Show the DisplayName
  4. GetKeyName - Show the ServiceKeyName
  5. EnumDepend - Show Dependencies
  6. qc - Show config-dependencies, full path etc
  7. start - START a service.
  8. stop - STOP a service
  9. pause - PAUSE a service.
  10. continue - CONTINUE a service.
  11. create - Create a service. (add it to the registry)
  12. config - permanently change the service configuration
  13. delete - Delete a service (from the registry)
  14. control - Send a control to a service
  15. interrogate - Send an INTERROGATE control request to a service
  16. Qdescription - Query the description of a service
  17. description - Change the description of a service
  18. Qfailure - Query the actions taken by a service upon failure
  19. failure - Change the actions taken by a service upon failure
  20. sdShow - Display a service's security descriptor using SDDL
  21. SdSet - Sets a service's security descriptor using SDDL
qryOpt Command:
  1. type= driver|service|all - Query specific types of service
  2. state= active|inactive|all - Query services in a particular state only
  3. bufsize= bytes
  4. ri= resume_index_number (default=0)
  5. group= groupname - Query services in a particular group
Misc commands that don't require a service name:
  1. SC  QueryLock - Query the LockStatus for the ServiceManager Database, this will show if a service request is running
  2. SC  Lock - Lock the Service Database
  3. SC  BOOT -Values are {ok | bad} Indicates whether to save the last restart configuration as the last-known-good restart configuration
Options:
The CREATE and CONFIG commands allow additional options to be set, see the build-in help: 'SC create' and 'SC config'.

Note: The qryOpt options above are case sensitive - they must be entered in lower case, also the position of spaces and = must be written exactly as shown.

Ex:SC query will display if a service is running, giving output like this:
       SERVICE_NAME: messenger
       TYPE: 20  WIN32_SHARE_PROCESS
       STATE: 4  RUNNING (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
       WIN32_EXIT_CODE: 0  (0x0)
       SERVICE_EXIT_CODE: 0  (0x0)
       CHECKPOINT: 0x0
       WAIT_HINT: 0x0

To retrieve specific information from SC's output, pipe into FIND or FindStr:
  1. C:\> SC query messenger | FIND "STATE" | FIND "STOPPED"
  2. C:\> SC query messenger | FIND "STATE" | FIND "RUNNING"
The statements above will return an %ERRORLEVEL% = 1 if the text is not found
IF errorlevel 1 GOTO: my_subroutine

NET START command can be used in a similar way to check if a service is running:
  1. NET START | FIND "Service name" > nul
  2. IF errorlevel 1 ECHO The service is not running
The service control manager will normally wait up to 30 seconds to allow a service to start.
You can modify this time (30,000 milliseconds) in the registry: 
  1. HKLM\SYSTEM\CurrentControlSet\Control
  2. ServicesPipeTimeout (REG_DWORD)
Some options only take effect at the point when the service is started.
Ex: The SC config command allows the executable of a service to be changed. When the service next starts up it will run the new executable. Config changes requires the current user to have “permission to configure the service”.

Examples:
  1. SC GetKeyName "task scheduler"
  2. SC GetDisplayName schedule
  3. SC start schedule
  4. SC QUERY schedule
  5. SC QUERY type= driver
  6. SC QUERY state= all |findstr "DISPLAY_NAME STATE" >svc_installed.txt
  7. SC \\myServer CONFIG myService obj= LocalSystem password= mypassword
  8. SC CONFIG MyService binPath=c:\myprogram.exe obj=".\LocalSystem" password="" 
Imp:Watch out for extra spaces:
SC QUERY state= all Works
SC QUERY sTate =all Fails!

No comments:

Post a Comment