Say i have a list of service all starting with the name "MyServiceFactory -". Not all of them will be started, just a handful and it varies upon service usage. I'm looking for help writing a batch program that only stops services that are running and starts these services (not all services and not restart). Any help is appreciated
This should work (or at least give you a start):
@echo off
setlocal
if "%~1"=="" goto usage
set tmpFile=templist.txt
set tmpAnsi=templist_ansi.txt
wmic /locale:MS_409 service where "caption like '%~1' and state='Running'" get caption /format:csv >%tmpFile%
REM this is required to convert from Unicode(UCS-2) to ANSI
type %tmpFile%>%tmpAnsi%
Echo ---------------Stopping services----------------------
Echo.
for /f "tokens=2 skip=2 delims=," %%i in (%tmpAnsi%) do (
wmic /locale:MS_409 service where caption="%%i" call stopservice
)
Echo --------------Starting services-----------------------
Echo.
for /f "tokens=2 skip=2 delims=," %%i in (%tmpAnsi%) do (
wmic /locale:MS_409 service where caption="%%i" call startservice
)
goto end
:usage
Echo.
Echo Usage is:
Echo %~n0 pattern_to_check
Echo.
Echo Pattern:
Echo [ ] Any one character within the specified range ([a=f]) or set ([abcdef]).
Echo ^^ Any one character not within the range ([^a=f]) or set ([^abcdef].)
Echo %% Any string of 0 (zero) or more characters
Echo _ (underscore) Any one character. Any literal underscore
Echo used in the query string must be escaped by placing it inside []
Echo.
Echo If pattern contains spaces, it must be enclosed in double quotes
:end
Assuming you name your batch file batch.bat, you would call it batch.bat "MyServiceFactory -%"
.