6

I want a batch file to check if the service "MyServiceName" is running. If the service is running, I want the batch file to disable it and then display a message. If it isn't running, and is disabled, I want the batch file to display a message and then to exit. Thanks for the help.

Gary Allen
  • 557
  • 1
  • 4
  • 11

3 Answers3

11
sc query MyServiceName| find "RUNNING" >nul 2>&1 && echo service is runnung
sc query MyServiceName| find "RUNNING" >nul 2>&1 || echo service is not runnung

To stop service:

net stop MyServiceName
npocmaka
  • 51,748
  • 17
  • 123
  • 166
2

If tried to come up with a little script that makes use of the SC-command, which seems to have some limitations though (and I couldn't test it):

@echo off
setlocal enabledelayedexpansion
:: Change this to your service name
set service=MyServiceName
:: Get state of service ("RUNNING"?)
for /f "tokens=1,3 delims=: " %%a in ('sc query %service%') do (
  if "%%a"=="STATE" set state=%%b
)
:: Get start type of service ("AUTO_START" or "DEMAND_START")
for /f "tokens=1,3 delims=: " %%a in ('sc qc %service%') do (
  if "%%a"=="START_TYPE" set start=%%b
)
:: If running: stop, disable and print message
if "%state%"=="RUNNING" (
  sc stop %service%
  sc config %service% start= disabled
  echo Service "%service%" was stopped and disabled.
  exit /b
)
:: If not running and start-type is manual, print message
if "%start%"=="DEMAND_START" (
  echo Start type of service %service% is manual.
  exit /b
)
:: If start=="" assume Service was not found, ergo is disabled(?)
if "%state%"=="" (
  echo Service "%service%" could not be found, it might be disabled.
  exit /b
)

I don't know if this gives the behavior you wanted. It seems like SC does not list services, that are disabled. But since you don't want to do anything if it's disabled, my code simply prints a message if the service wasn't found.

However, you can hopefully use my code as a framework/toolbox for your purposes.

EDIT:

Given npocmaka's answer, you could probably change the for-sections to something like:

sc query %service%| find "RUNNING" >nul 2>&1 && set running=true
marsze
  • 11,092
  • 5
  • 33
  • 50
  • `sc query state= all` (that space after the equals sign is importart) will get all services. – mojo Jun 25 '13 at 14:04
  • Ok, I see, thx. However, when you look for a specific name `SC` will return `STATE=STOPPED` anyway, so you might probably test `"%state%"=="STOPPED"` as well. – marsze Jun 25 '13 at 14:08
1

This script takes the service name as the first (and only) parameter, or you can hardcode it into the SVC_NAME assignment. The output of the sc commands is thrown away. I don't know if you actually want to see it or not.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET SVC_NAME=MyServiceName
IF NOT "%~1"=="" SET "SVC_NAME=%~1"

SET SVC_STARTUP=
FOR /F "skip=1" %%s IN ('wmic path Win32_Service where Name^="%SVC_NAME%" get StartMode') DO (
    IF "!SVC_STARTUP!"=="" SET "SVC_STARTUP=%%~s"
)

CALL :"%SVC_STARTUP%" "%SVC_NAME%"
CALL :StopService "%SVC_NAME%"
GOTO :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:"Boot"
:"System"
:"Auto"
:"Manual"
@ECHO Disabling service '%~1'.
sc.exe config "%~1" start= disabled > NUL
IF NOT ERRORLEVEL 1 @ECHO Service '%~1' disabled.
EXIT /B

:"Disabled"
@ECHO Service '%~1' already disabled.
EXIT /B


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:StopService
SETLOCAL
SET SVC_STATE=
FOR /F "skip=1" %%s IN ('wmic path Win32_Service where Name^="%~1" get State') DO (
   IF "!SVC_STATE!"=="" SET "SVC_STATE=%%~s"
)
CALL :"%SVC_STATE%" "%~1"
EXIT /B


:"Running"
:"Start Pending"
:"Continue Pending"
:"Pause Pending"
:"Paused"
:"Unknown"
@ECHO Stopping service '%~1'.
sc.exe stop "%~1" > NUL
IF NOT ERRORLEVEL 1 @ECHO Service '%~1' stopped.

EXIT /B

:"Stop Pending"
:"Stopped"
@ECHO Service '%~1' is already stopping/stopped.
EXIT /B
mojo
  • 3,745
  • 14
  • 20