Reputation: 452
I have to send a msg to a plurality of users who perform a specific process: How can I find a list of usernames, performing, for example the image "chrome.exe", and then send msg to these users. All the activities described above, must be in a bat-file Thank you in advance!
Upvotes: 0
Views: 297
Reputation: 56180
Based on the comments to xmcp's answer, I expanded the code a bit:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%x in ('tasklist /fi "imagename eq firefox.exe" /fo csv /nh /v') do (
set line=%%x
set line=!line:","="@"!
for /f "tokens=7 delims=@" %%a in (!line!) do echo %%~a
)
It replaces the field separators (","
), without touching the comma within the numbers (in some localizations) and parses the resulting string with a different separator. Downside: it slows things down (theoretically, I don't think anyone will notice it)
Upvotes: 2
Reputation: 34909
xmcp's answer shows the perfect command to retrieve processes and the names of their owning users. However, their solution fails in case of additional white-spaces appearing within the data.
To make it more safe, use the csv output format of the tasklist
command, capture its output by a for /F
loop in full lines/rows and extract the single column/cell items by a standard for
loop:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* Capture CSV-formatted output without header; `tasklist` returns these columns:
rem `"Image Name","PID","Session Name","Session#","Mem Usage","Status","User Name","CPU Time","Window Title"`: */
for /F "delims=" %%L in ('
tasklist /FI "ImageName eq Chrome.exe" /FI "Status eq Running" /V /NH /FO CSV
') do (
rem // Initialise column counter:
set /A "CNT=0"
rem /* Use standard `for` loop to enumerate columns, as this regards quoting;
rem note that the comma `,` is a standard delimiter in `cmd`: */
for %%I in (%%L) do (
rem // Store item with surrounding quotes removed:
set "ITEM=%%~I"
rem /* Store item with surrounding quotes preserved, needed for later
rem filtering out of (unquoted) message in case of no match: */
set "TEST=%%I"
rem // Increment column counter:
set /A CNT+=1
rem // Toggle delayed expansion not to lose exclamation marks:
setlocal EnableDelayedExpansion
rem /* in case no match is found, this message appears:
rem `INFO: No tasks are running which match the specified criteria.`;
rem since this contains no quotes, the following condition fails: */
if not "!ITEM!"=="!TEST!" (
rem // The 7th column holds the user name:
if !CNT! EQU 7 echo(!ITEM!
)
endlocal
)
)
endlocal
exit /B
This simply echoes the name of every user that is currently running a process named Chrome.exe
. To send messages to them, you might use the net send
command instead of echo
.
This approach does not work in case the CSV data contains global wild-card characters *
and ?
; such characters should not occur in image, session and names; they may appear in window titles though, but these appear after the user names in the tasklist
output anyway.
Upvotes: 0
Reputation: 3742
Try this:
@echo off
for /f "tokens=8" %%i in ('tasklist /fi "imagename eq chrome.exe" /fo table /nh /v') do echo %%i
Please be aware that the code might be buggy if the image name contains spaces, but I can't find a perfect solution in plain batch-file.
Explanation:
Upvotes: 1