Skip to content

Instantly share code, notes, and snippets.

@aplocher
Last active Aug 29, 2015
Embed
What would you like to do?
Helper bat file to start / stop SQL server instances. Will require user intervention (pause) when error occurs. Supports colorized output if cmdcolor.exe is available in the same dir or in the environment PATH.
@echo off
rem ---------------------------------------------
rem - SQL SERVICE HELPER v1.1.20140624
rem - ADAM PLOCHER / BITCOLLECTORS
rem - https://github.com/aplocher
rem -
rem - Description:
rem - Helper bat file to start / stop SQL server instances. Will require
rem - user intervention (pause) when error occurs. Supports colorized output
rem - if cmdcolor.exe is available in the same dir or in the environment PATH.
rem -
rem - Optional component cmdcolor.exe
rem - This component simplifies colorization and can be downloaded from
rem - https://github.com/jeremejevs/cmdcolor
rem -
rem - Usage:
rem - sqlsrvhelper.bat /start|/stop [instance_name]
rem -
rem - Example:
rem - sqlsrvhelper.bat /start
rem - sqlsrvhelper.bat /stop
rem - or:
rem - sqlsrvhelper.bat /start SQL08R2
rem - sqlsrvhelper.bat /stop SQL08R2
rem ---------------------------------------------
pushd %~dp0
rem -- HANDLE PARAMETERS
set action=%1
set instancename=%2
set servicename=%3
rem -- INIT
set supportscolor=0
set isservicerunning=0
if /i [%instancename%]==[] set instancename=MSSQLSERVER
if /i [%servicename%]==[] (
if /i [%instancename%]==[MSSQLSERVER] (
set servicename=%instancename%
) else (
set servicename=MSSQL$%instancename%
)
)
rem -- CHECK IF SQL SERVICE IS RUNNING
net start | find " SQL Server (%instancename%)" >nul 2>&1
if %ERRORLEVEL% NEQ 1 set isservicerunning=1
rem -- CHECK IF COLORIZED TXT SUPPORTED
where cmdcolor.exe >nul 2>&1
if %ERRORLEVEL% NEQ 1 set supportscolor=1
rem -- IS START OR STOP ACTION? --
if /i [%action%]==[/stop] goto:stopaction
if /i [%action%]==[/start] goto:startaction
goto:errorparameters
:startaction
echo Attempting to START MSSQL (Instance: %instancename%)
if %isservicerunning% NEQ 1 goto:startservice
call:displayWarning " - Service already running"
goto:success
:startservice
echo Service NOT already running. Starting now...
net start %servicename%
if %ERRORLEVEL% EQU 0 goto:success
call:displayError " - Error occurred, please review the errors. Pausing ..."
pause
goto:done
:stopaction
echo Attempting to STOP MSSQL (Instance: %instancename%)
if %isservicerunning% EQU 1 goto:stopservice
call:displayWarning " - Service not running"
goto:success
:stopservice
echo Service running. Stopping now...
net stop %servicename%
if %ERRORLEVEL% EQU 0 goto:success
call:displayError " - Error occurred, please review the errors. Pausing ..."
pause
goto:done
:errorparameters
echo -----------------------------------------------------------
call:displayInfo "sqlsrvhelper.bat"
call:displayInfo "by Adam Plocher @ Bit Collectors"
echo.
echo USAGE: sqlsrvhelper.bat /start^|/stop [instance_name]
echo.
echo EXAMPLES:
echo.
echo Start the default instance (MSSQLSERVER):
call:displayInfo " sqlsrvhelper.bat /start"
echo.
echo Stop a SQL Server instance called SQL08R2:
call:displayInfo " sqlsrvhelper.bat /stop SQL08R2"
if not [%supportscolor%]==[1] (
echo.
echo WARNING: cmdcolor.exe not found!
echo Color text output is supported by downloading the light-weight
echo cmdcolor.exe helper from: github.com/jeremejevs/cmdcolor
)
echo -----------------------------------------------------------
echo.
goto:success
:success
rem -- CLOSE AFTER 1 SECOND(S) --
timeout /t 1 /nobreak >nul 2>&1
goto:done
:done
popd
goto:eof
:displayError
if [%supportscolor%]==[1] (
echo \033[91m%~1| cmdcolor
goto:eof
)
echo *ERROR* %~1
goto:eof
:displayWarning
if [%supportscolor%]==[1] (
echo \033[93m%~1| cmdcolor
goto:eof
)
echo *WARNING* %~1
goto:eof
:displayInfo
setlocal EnableDelayedExpansion
if [%supportscolor%]==[1] (
echo \033[97m%~1| cmdcolor
goto:eof
)
echo %~1
goto:eof

SQL SERVICE HELPER (sqlsrvhelper.bat)

Adam Plocher

https://gist.github.com/aplocher/

https://github.com/aplocher

USAGE:

sqlsrvhelper.bat /start|/stop [instance_name]

EXAMPLE:

Starting and stopping the default instance:

sqlsrvhelper.bat /start
sqlsrvhelper.bat /stop

Starting and stopping an instance named SQL08R2

sqlsrvhelper.bat /start SQL08R2
sqlsrvhelper.bat /stop SQL08R2

WHY NOT JUST USE 'NET START' OR 'NET STOP'?

I wanted a solution I could easily implement that would pause upon error, but the window would disappear without user intervention when the service was started or it was already running (or stopped). It also needed to be portable and optionally support colorized text output in the command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment