Skip to content

Instantly share code, notes, and snippets.

@coryasilva
Created December 30, 2014 04:46
Show Gist options
  • Save coryasilva/7525287a10c2e9341f25 to your computer and use it in GitHub Desktop.
Save coryasilva/7525287a10c2e9341f25 to your computer and use it in GitHub Desktop.
Windows Service Control Batch File
@echo off
REM %1 = action
REM %2 = service
REM Set vars
set status=""
set message=""
set hint="C:\>servicecontrol.bat [ START | STOP ] [<service_name>]"
set example=servicecontrol.bat start "My Service Name"
REM Check for valid action parameter
if not [%1]==[stop] if not [%1]==[start] goto :invalidaction
REM Null check for service parameter
if [%2]==[] goto :nullservice
REM Try net command
net %1 %2
if %errorlevel%==0 (
set status=SUCCESS
set message=%2 %1 @ %date% %time%
) else (
set status=ERROR
set message="net" command returned an errorlevel of %errorlevel%
)
goto :quit
REM Error and Validation Handling
:nullservice
set status=ERROR
set message=I expected a service name, but it was not defined.
set hint=Use quotes for services with spaces in the name.
set example="Cool Service Name"
goto :quit
:invalidaction
set status=ERROR
set message=I expected a valid action.
goto :quit
REM Command results and feedback
:quit
echo .....................servicecontrol.bat.........................
echo .
echo . HINT: %hint%
echo . EXAMPLE: %example%
echo .
echo . COMMAND: %0 %1 %2
echo . STATUS: %status%
echo . MESSAGE: %message%
echo .
echo .................................................................
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment