Remote control SQL Server service


Control the SQL Server services on a remote server by using SC.

sc \\remote-server-name config MSSQLSERVER start= disabled

sc \\remote-server-name stop   MSSQLSERVER 4:3:14 "MS Patching 20160123"

[SC] ControlServiceEx FAILED 1051:

A stop control has been sent to a service that other running services are dependent on.

E:\DBA> sc \\remote-server-name enumdepend MSSQLSERVER
[SC] EnumDependentServices: entriesread = 1

SERVICE_NAME: SQLSERVERAGENT
DISPLAY_NAME: SQL Server Agent (MSSQLSERVER)
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0


:: STOP (and START) commands are asynchronous - might want to use START /WAIT to process them
:: e.g.
:: START /WAIT sc \\remote-server-name stop  SQLSERVERAGENT 4:3:14 "MS Patching 20160123"


E:\DBA>sc \\remote-server-name stop  SQLSERVERAGENT 4:3:14 "MS Patching 20160123"

SERVICE_NAME: SQLSERVERAGENT
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x1
        WAIT_HINT          : 0x3a98
        PID                : 1860
        FLAGS              :

E:\DBA>sc \\remote-server-name stop   MSSQLSERVER 4:3:14 "MS Patching 20160123"

SERVICE_NAME: MSSQLSERVER
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 3  STOP_PENDING
                                (STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x1
        WAIT_HINT          : 0xea60
        PID                : 1328
        FLAGS              :

E:\DBA>sc \\remote-server-name start  MSSQLSERVER

SERVICE_NAME: MSSQLSERVER
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x7d0
        PID                : 2576
        FLAGS              :

E:\DBA>sc \\remote-server-name start SQLSERVERAGENT

SERVICE_NAME: SQLSERVERAGENT
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 2  START_PENDING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x1
        WAIT_HINT          : 0x7530
        PID                : 4596
        FLAGS              :

Full SC help:

SC.EXE

DESCRIPTION:
        Sends a STOP control request to a service.
USAGE:
        sc <server> stop [service name] <reason> <comment>
        <reason> = Optional reason code number for service stop
                   formed with the following elements in the format:

                        Flag:Major reason:Minor reason

                        [E.g., 1:2:8 means Hardware: Disk (Unplanned)]

                        Flag                       Major reason
                   ------------------       ---------------------------
                   1    -   Unplanned       1       -   Other
                   2    -   Custom          2       -   Hardware
                   4    -   Planned         3       -   Operating System
                                            4       -   Software
                                            5       -   Application
                                            64-255  -   Custom

                            Minor reason
                   -----------------------------------
                   1            -   Other
                   2            -   Maintenance
                   3            -   Installation
                   4            -   Upgrade
                   5            -   Reconfiguration
                   6            -   Hung
                   7            -   Unstable
                   8            -   Disk
                   9            -   Network Card
                   10           -   Environment
                   11           -   Hardware Driver
                   12           -   Other Driver
                   13           -   Service Pack
                   14           -   Software Update
                   15           -   Security Fix
                   16           -   Security
                   17           -   Network Connectivity
                   18           -   WMI
                   19           -   Service Pack Uninstall
                   20           -   Software Update Uninstall
                   22           -   Security Fix Uninstall
                   23           -   MMC
                   256-65535    -   Custom

       <comment> = Optional comment for the reason above (127 characters maximum)

	   Reason	4:3:14	Planed : Operating System : Software Update
	   Comment	"MS Patching 20160123"

DESCRIPTION:
        Starts a service running.
USAGE:
        sc <server> start [service name] <arg1> <arg2> ...


DESCRIPTION:
        Modifies a service entry in the registry and Service Database.
USAGE:
        sc <server> config [service name] <option1> <option2>...

OPTIONS:
NOTE: The option name includes the equal sign.
      A space is required between the equal sign and the value.
 type= <own|share|interact|kernel|filesys|rec|adapt>
 start= <boot|system|auto|demand|disabled|delayed-auto>
 error= <normal|severe|critical|ignore>
 binPath= <BinaryPathName>
 group= <LoadOrderGroup>
 tag= <yes|no>
 depend= <Dependencies(separated by / (forward slash))>
 obj= <AccountName|ObjectName>
 DisplayName= <display name>
 password= <password>
Comment on this article using form below. Requires email login only for authentication. HTML forbidden, Markdown only.