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!
我必须向执行特定进程的多个用户发送一个msg:如何找到用户名列表,执行例如图像“chrome.exe”,然后将msg发送给这些用户。上面描述的所有活动,都必须在bat文件中提前谢谢!
3 个解决方案
#1
2
Based on the comments to xmcp's answer, I expanded the code a bit:
基于对xmcp的回答的评论,我稍微扩展了代码:
@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. Donwside: it slows things down (theroetical, I don't think anyone will notice it)
它替换字段分隔符(“,”),而不触及数字中的逗号(在某些本地化中),并使用不同的分隔符分析结果字符串。 Donwside:它减慢了事情(理论上,我认为没有人会注意到它)
#2
1
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:
#3
0
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.
xmcp的答案显示了检索进程及其拥有用户名称的完美命令。但是,如果数据中出现额外的空格,则其解决方案将失败。
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:
为了使其更安全,使用tasklist命令的csv输出格式,通过完整行/行中的for / F循环捕获其输出,并通过标准for循环提取单个列/单元项:
@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
.
这简单地回应了当前运行名为Chrome.exe的进程的每个用户的名称。要向它们发送消息,您可以使用net send命令而不是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.
如果CSV数据包含全局通配符*和?,则此方法不起作用;这些字符不应出现在图像,会话和名称中;它们可能出现在窗口标题中,但这些出现在任务列表输出中的用户名之后。
#1
2
Based on the comments to xmcp's answer, I expanded the code a bit:
基于对xmcp的回答的评论,我稍微扩展了代码:
@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. Donwside: it slows things down (theroetical, I don't think anyone will notice it)
它替换字段分隔符(“,”),而不触及数字中的逗号(在某些本地化中),并使用不同的分隔符分析结果字符串。 Donwside:它减慢了事情(理论上,我认为没有人会注意到它)
#2
1
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:
#3
0
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.
xmcp的答案显示了检索进程及其拥有用户名称的完美命令。但是,如果数据中出现额外的空格,则其解决方案将失败。
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:
为了使其更安全,使用tasklist命令的csv输出格式,通过完整行/行中的for / F循环捕获其输出,并通过标准for循环提取单个列/单元项:
@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
.
这简单地回应了当前运行名为Chrome.exe的进程的每个用户的名称。要向它们发送消息,您可以使用net send命令而不是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.
如果CSV数据包含全局通配符*和?,则此方法不起作用;这些字符不应出现在图像,会话和名称中;它们可能出现在窗口标题中,但这些出现在任务列表输出中的用户名之后。