print all running services + user of each service

March 21, 2012 at 02:01:24
Specs: Windows Server 2003
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 *************

See More: print all running services + user of each service

Report •

March 21, 2012 at 06:39:20
I'd normally use something like VBScript for this, but wmic should be able to handle it easily enough.
wmic service where "Started=True" get Name, StartName

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question