Collect each service's display name and status - windows

I want to get list of services with their display name and their status.
This is what I have tried:
for /f "tokens=2" %s in ('SC query state^= all ^| find "DISPLAY_NAME"') do #(for /f "tokens=4" %t in ('SC query %s ^| find "STATE"') do #echo %s is %t)
But this returns only limited services such as disk, etc.

This is a perfect task for the built-in WMI command line executable, WMIC.exe.
From the cmd.exe prompt:
For /F "Skip=1 Delims=" %A In ('"WMIC Service Get DisplayName, Name, State"') Do #For /F "Delims=" %B In ("%A") Do #Echo(%B
From a batch file:
#For /F "Skip=1 Delims=" %%A In ('"WMIC Service Get DisplayName, Name, State"'
) Do #For /F "Delims=" %%B In ("%%A") Do #Echo(%%B
#Pause

Try getting help from powershell directly in your batch file, like this for example (save as .bat and run it). In the example i type it to the screen and give you ALL services, RUNNING ONLY, and STOPPED ONLY but you could do basically what you want with the content of that txt file (search it with a loop, save a part to a variable, etc.).
#echo off&cls
pushd %~dp0
echo.
echo List ALL services
pause
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "powershell Get-Service | Out-File services.txt"
type services.txt
echo.
echo List services that are RUNNING only
pause
find "Running" services.txt
echo.
echo List services that are STOPPED only
pause
find "Stopped" services.txt
pause
:cleanup
del /f services.txt

Related

Take username and Products on batch file

Can I take the username and list products installed in the same txt file?
I thought of something like:
for /f "usebackq tokens=* delims=" %%a in (`wmic product get name`) do (
set "result=%%a"
)
echo %username%/%result% > test.txt
But no success.
I need this to put this on an Excel file, with another program, and I need this structure, username/programs.
(
for /f "usebackq tokens=* delims=" %%a in (`wmic product get name`) do (
echo %username%/%%a
)
)> test.txt
You can absolutely do that, you just need to keep in mind that for loops process one line at a time, so you need to output the value while you're still in the loop.
for /f "delims=" %%A in ('wmic product get name') do (
echo %%A >>test.txt
)
You don't need to set a variable for this since you aren't doing any string manipulation with it, but if you did, you would have to use delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('wmic product get name') do (
set "line=%%A"
echo !line! >>test.txt
)
You can also skip the for loop altogether and just redirect the output of the wmic command straight to the text file:
wmic product get name >>test.txt
You could probably use powershell from cmd or your batch-file, to get this information as a single line in CSV file in the format:
"UserName","Program Name 1","Program Name 2", "Program Name 3", etc…
Example command:
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -Command "'\"' + (#($([System.Security.Principal.WindowsIdentity]::GetCurrent().Name.Split('\\')[-1])) + (Get-CimInstance -Class Win32_Product -Filter 'Name Is Not Null').Name -Join '\",\"') + '\"'" 1> "UserProducts.csv"

Issues when getting a list of user profiles using WMIC

I was using the following batch command to retrieve all local user profiles (including domain users too) :
for /f "delims=" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*" 2^>nul ^| %SystemRoot%\System32\findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (
The problem is that this command has its limits: it doesn't really check if the users in question do actually have an account.
The user Compo provided me a methodology for retrieving the profile names, using WMIC.
So I ended up writing the following command:
#For /F "tokens=* skip=1" %%I In ('%__AppDir__%wbem\WMIC.exe UserAccount Get Name ^|%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"') do (
The problem is: it ignores my exclusion file (which contains one user per line) and it ends up a profile without any name.
Any idea How I can solve these issues ?
#echo off
setlocal
set "bin=%~dp0"
for /f "tokens=* skip=1" %%I in ('
%__AppDir__%wbem\WMIC.exe UserAccount where Disabled^="FALSE" get Name ^|
%__AppDir__%WindowsPowerShell\v1.0\powershell -noprofile -command "$input.trim()" ^|
%__AppDir__%findstr.exe /i /l /x /v /g:"%bin%\exclude_users.txt"
') do echo "%%~I"
The wmic output is piped to powershell to be trimmed and then piped to findstr.
The wmic command will exclude disabled accounts by use of the where clause.
Change the setting of bin as needed.
If you want a solution which still uses WMIC, and your exclusion list, then the following should do as you require.
#For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where "LocalAccount='TRUE'" Assoc:List /ResultRole:Name 2^>NUL')Do #Echo %%G|%__AppDir__%findstr.exe /VXLIG:"%~dp0exclude_users.txt"
You can split that over multiple lines for easier reading too:
#For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
Where "LocalAccount='TRUE'" Assoc:List /ResultRole:Name 2^>NUL'
)Do #Echo %%G|%__AppDir__%findstr.exe /VXLIG:"%~dp0exclude_users.txt"

wmic show last logon name from file comp_names.txt

Have a script in .bat for view last logon name from list:
#echo off
setlocal enabledelayedexpansion
set cnt=0
for /f "usebackq tokens=*" %%A IN ("m_names.txt") DO (
echo [%%A]
wmic /node: %%A COMPUTERSYSTEM GET dnshostname,USERNAME /all
)
In m_names.txt :
lazar2
counter683
machine
localm
etc
May be another way to call wmic service ?

Save the result of a command in variable, Windows batch

I am trying to write a batch script that saves the result of a command in a variable. so I can use it later.
For example I am tryin to run this on the script:
sc queryex "Service" |find /i "pid"
but I want to save this result in a variable.
set PIDRS=sc queryex "Themes" |find /i "pid"
ECHO "%PIDRS%
Any Ideas?
for /f "tokens=* delims=" %%# in ('sc queryex "Themes" ^|find /i "pid"') do set "PIDRS=%%#"
echo %PIDRS%
This will set the entire line to PIDRS
here's how to get only the pid:
#echo off
set "rspid="
for /f "skip=9 tokens=2 delims=:" %%# in ('sc queryex "Themes"') do (
if not defined rspid set /a rspid=%%#
)
the second does not use additional FIND which in theory should make it faster.

Unreliable results using quotes with variables in windows batch for loop

I'm running a for loop to go through a list of services to stop, but I'm having trouble with the quotes, and rather unusual results as well.
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo "%%a"
My results are with this:
"Service Name
"Service Name
If I switch to:
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo ""%%a""
The results are:
""Service Name
""Service Name
And, if I switch to:
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do echo ^"%%a^"
The results:
"Service Name
"Service Name
Obviously something is happening that I don't understand, because I simply cannot get it to encapsulate my results in quotes so I can run commands such as net stop or sc delete.
try this:
for /f "tokens=* delims= " %%a in ('wmic service where ^(displayname like "%%tsm%%"^) get name ^| findstr "TSM"') do (
for /f "delims=" %%i in ("%%a") do echo "%%i"
)
this is due to the "end of line" sign of wmic (CRLFCR).

Resources