Created
February 25, 2014 14:27
-
-
Save jpluimers/9209811 to your computer and use it in GitHub Desktop.
List the Windows Services (no need for an Administrative UAC token)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:: find all services by SERVICE_NAME, then list STATE, TYPE, DISPLAY_NAME, and "" (this is on the line below STATE) | |
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION | |
:: the space in " all" is required by the sc command; see "sc /?" for explanation | |
:: the ^ in "state^= all" is required as the command interpreter will eat the equals sign, which causes the errors | |
:: "[SC] EnumQueryServicesStatus OpenService FAILED 1060" | |
:: "The specified service does not exist as an installed service." | |
:: see http://blog.smartbear.com/software-quality/managing-windows-services/ | |
:: for /f "usebackq tokens=1,2,3 delims=:" %%i in (`sc query`) do ( | |
for /f "usebackq tokens=1,2,3 delims=:" %%i in (`sc query state^= all`) do ( | |
rem echo %%i %%j %%k | |
if "%%i"=="SERVICE_NAME" call :%%i %%j %%k | |
) | |
endlocal | |
goto :eof | |
:SERVICE_NAME | |
:: echo %0 %1 %2 | |
if "%2"=="" call :process-service %1 | |
if not "%2"=="" call :process-service %1$%2 | |
goto :eof | |
:process-service | |
:: echo service "%1" | |
set service_display_name= | |
set service_name= | |
set service_pid= | |
set service_properties= | |
set service_state= | |
set service_type= | |
:: `sc query` and `sc queryex` will only show DISPLAY_NAME when no SERVICE_NAME is specified | |
:: so we have to perform `sc query` for ALL services, then grab the DISPLAY_NAME for the matching SERVICE_NAME | |
for /f "usebackq tokens=1,* delims=:, " %%s in (`sc query state^= all`) do ( | |
rem if "%%s"=="STATE" if not !%1!==!! echo %%v state of %1 is %%v | |
if "%%s"=="SERVICE_NAME" set service_name=%%t | |
if "%%s"=="DISPLAY_NAME" if "!service_name!"=="%1" set service_display_name=%%t | |
set first_char=%%s | |
set first_char=!first_char:~0,1! | |
if "!first_char!"=="(" if "!service_name!"=="%1" set service_properties=%%s, %%t | |
rem echo "!first_char!", !service_properties!, %%s, %%t | |
) | |
set service_name= | |
:: find all services by SERVICE_NAME, then list STATE, TYPE, DISPLAY_NAME, and "" (this is on the line below STATE) | |
for /f "usebackq tokens=1,2,3,4 delims=:, " %%s in (`sc queryex %1`) do ( | |
rem if "%%s"=="STATE" if not !%1!==!! echo %%v state of %1 is %%v | |
if "%%s"=="PID" set service_pid=%%t | |
if "%%s"=="SERVICE_NAME" set service_name=%%t | |
if "%%s"=="STATE" set service_state=%%u | |
if "%%s"=="STATE" set service_state=%%u | |
if "%%s"=="TYPE" set service_type=%%u | |
if "%%s"=="STATE" set service_state=%%u | |
rem echo "%%s", "%%t", "%%u", "%%v" | |
) | |
echo %service_pid%, %service_state%, %service_type%, %service_name%, %service_properties%, %service_display_name% | |
goto :eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment