如何查找运行特定流程的用户列表

时间:2016-08-22 12:04:19

标签: shell batch-file

我必须向执行特定过程的多个用户发送一个消息: 如何查找用户名列表,例如执行图像" chrome.exe",然后将msg发送给这些用户。 上述所有活动必须在bat文件中 提前谢谢!

3 个答案:

答案 0 :(得分:2)

根据对xmcp的回答的评论,我稍微扩展了代码:

SELECT part_id
     , AVG(est_time) last_2_avg
  FROM 
     ( SELECT x.*
            , CASE WHEN @part_id = part_id 
                   THEN CASE WHEN @batch_id = batch_id THEN @i:=@i ELSE @i:=@i+1 END
                   ELSE @i:=1
                   END i
            , @part_id := part_id
            , @batch_id:= batch_id
         FROM test x
            , (SELECT @part_id := null, @batch_id:=null, @i:=1) vars
        ORDER
           BY part_id
            , batch_id DESC
     ) a
 WHERE a.i <= 2
 GROUP 
    BY part_id;

+---------+------------+
| part_id | last_2_avg |
+---------+------------+
|       1 |    27.2500 |
|       2 |    22.5000 |
|       3 |    16.6667 |
|       4 |    47.5000 |
+---------+------------+

它替换字段分隔符(@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 ) ),而不触及数字中的逗号(在某些本地化中),并使用不同的分隔符分析结果字符串。 Donwside:它减慢了事情的速度(理论上,我认为没有人会注意到它)

答案 1 :(得分:1)

试试这个:

@echo off
for /f "tokens=8" %%i in ('tasklist /fi "imagename eq chrome.exe" /fo table /nh /v') do echo %%i

请注意,如果图像名称包含空格,代码可能有问题,但我无法在普通批处理文件中找到完美的解决方案。

说明:

screenshot

答案 2 :(得分:0)

xmcp&#39; s answer显示了检索进程及其拥有用户名称的完美命令。但是,如果数据中出现额外的空格,则其解决方案将失败。

为了使其更安全,请使用tasklist命令的输出格式,以整行/行的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

这简单地回显了当前正在运行名为Chrome.exe的进程的每个用户的名称。要向他们发送消息,您可以使用net send命令代替echo

如果CSV数据包含全局通配符*?,则此方法不起作用;这些字符不应出现在图像,会话和名称中;它们可能会出现在窗口标题中,但它们会出现在tasklist输出中的用户名之后。