No announcement yet.

print all running services + user of each service

  • Filter
  • Time
  • Show
Clear All
new posts

  • print all running services + user of each service

    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:

    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
    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
    set B="%%A"
    echo "%B%"
    set B=%B:~2,-1%
    echo "%B%"
    ::set %B%
    sc qc %%B | find "SERVICE_START_NAME" >> Users.txt
    @echo off   
    for /F "usebackq tokens=2 delims=: " %%A IN (Users.txt) DO ( echo User: %%A >> Stripped-user.txt )
    for /F "usebackq tokens=3 delims=: " %%A IN (Service-name.txt) DO ( echo %%A >> Stripped-service.txt )
    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    
    echo %f2%
    del Stripped-user.txt
    del Stripped-service.txt
    del Users.txt
    del Service-name.txt
    call combined.txt
    REM ****************** END of Script *************
    Last edited by Rems; 21st March 2012, 20:52. Reason: Added [CODE] tags
    The rules DO apply to me.

  • #2
    Re: print all running services + user of each service

    First thing I noticed are the single quotes that are used in:
    (`sc queryex ^| find "SERVICE_NAME"`)

    Change these to straight single quote signs:
    ('sc queryex ^| find /i "SERVICE_NAME"')

    If that didn't solve it, please explain what the problems exactly are.


    This posting is provided "AS IS" with no warranties, and confers no rights.


    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts