print all running services + user of each service

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
onemancrew
Posts: 1
Joined: 21 Mar 2012 02:44

print all running services + user of each service

#1 Post by onemancrew » 21 Mar 2012 03:18

Hi to all

I would like to write a script which print all running services and in the same time to print for each service the username which the service is running as.

so I have managed to develop some sort of script but I have some problem with the script.

here is the script which I have developed:

Code: Select all

REM ****************** START of Script ******************
del combined.txt
for /F "usebackq tokens=2 delims=:" %%i IN (`sc queryex ^| find "SERVICE_NAME"`) DO (
@echo Service Name: %%i >> Service-name.txt
)
pause
:loop
for /F "usebackq tokens=2 delims=:" %%A IN (Service-name.txt) DO (
REM **************** IN THIS PART the PROBLEM start *************
REM ***** I am trying to remove the blank space which is coming from Service-name.txt ****
REM ***** as you can see I am using delimiter char which is : only , means colon only ****
REM ***** and as result I get a blank space before the service name ***********
REM ***** I cant use delimiter which is colon and blank space because I would like that ****
REM ***** the same script will also be eligible for service name which has more then ****
REM ***** one word within it  , for example "Crypkey License" Service Name ********
REM ***** In this part of the script I am trying to remove the blank space which exist in the ***
REM ***** beginning and the blank space which exist in the end of the service name ****
@echo %%A
pause
set B="%%A"
echo "%B%"
set B=%B:~2,-1%
echo "%B%"
pause
::set %B%
sc qc %%B | find "SERVICE_START_NAME" >> Users.txt
)
pause
@echo off   
for /F "usebackq tokens=2 delims=: " %%A IN (Users.txt) DO ( echo User: %%A >> Stripped-user.txt )
pause
for /F "usebackq tokens=3 delims=: " %%A IN (Service-name.txt) DO ( echo %%A >> Stripped-service.txt )
pause
set f1=stripped-service.txt 
set f2=Stripped-user.txt
set "sep=        "  % tab %   
(   
   for /f "delims=" %%a in (%f1%) do (       
      setlocal enabledelayedexpansion       
      set /p line=       
      echo(%%a!sep!!line! >> combined.txt   
   endlocal   
   ) 
)<%f2% 
echo %f2%
del Stripped-user.txt
del Stripped-service.txt
del Users.txt
del Service-name.txt
call combined.txt

REM ****************** END of Script *************

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: print all running services + user of each service

#2 Post by foxidrive » 21 Mar 2012 04:04

Try this:

Code: Select all

@echo off
for /f "tokens=1,*" %%a in ('sc queryex ^| find "SERVICE_NAME"') do (
for /f "tokens=3,4" %%c in ('sc qc "%%b" ^|find /i "SERVICE_START_NAME"') do (
if "%%d"=="" (echo "%%c" - "%%b") else (echo "%%c %%d" - "%%b")
)
)
pause


If the user field contains two spaces then it will truncate it at the second space. That can be expanded if it's likely to be an issue.

Post Reply