windows 按进程 ID 查找进程名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27211427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:51:21  来源:igfitidea点击:

Find Process Name by its Process ID

windowsbatch-filecommand-linecmdoperating-system

提问by Oz Molaim

Suppose I know the process ID. I want to find the process name by its ID, using windows batch script. How can I do this?

假设我知道进程 ID。我想使用 Windows 批处理脚本通过其 ID 查找进程名称。我怎样才能做到这一点?

回答by MC ND

The basic one, ask tasklist to filter its output and only show the indicated process id information

基本的一种,让 tasklist 过滤它的输出并且只显示指示的进程 id 信息

tasklist /fi "pid eq 4444" 

To only get the process name, the line must be splitted

要仅获取进程名称,必须拆分该行

for /f "delims=," %%a in ('
    tasklist /fi "pid eq 4444" /nh /fo:csv
') do echo %%~a

In this case, the list of processes is retrieved without headers (/nh) in csv format (/fo:csv). The commas are used as token delimiters and the first token in the line is the image name

在这种情况下,/nh以 csv 格式 ( /fo:csv)检索不带标题 ( )的进程列表。逗号用作标记分隔符,行中的第一个标记是图像名称

note: In some windows versions (one of them, my case, is the spanish windows xp version), the pid filter in the tasklist does not work. In this case, the filter over the list of processes must be done out of the command

注意:在某些 Windows 版本中(其中之一,我的情况是西班牙 Windows xp 版本),任务列表中的 pid 过滤器不起作用。在这种情况下,必须在命令之外完成对进程列表的过滤

for /f "delims=," %%a in ('
    tasklist /fo:csv /nh ^| findstr /b /r /c:"[^,]*,\"4444\","
') do echo %%~a

This will generate the task list and filter it searching for the process id in the second column of the csv output.

这将生成任务列表并过滤它,在 csv 输出的第二列中搜索进程 ID。

edited: alternatively, you can suppose what has been made by the team that translated the OS to spanish. I don't know what can happen in other locales.

编辑:或者,您可以假设将操作系统翻译成西班牙语的团队做了什么。我不知道在其他地区会发生什么。

tasklist /fi "idp eq 4444" 

回答by Craig

Using only "native" Windows utilities, try the following, where "516" is the process ID that you want the image name for:

仅使用“本机”Windows 实用程序,尝试以下操作,其中“516”是您想要映像名称的进程 ID:

for /f "delims=," %a in ( 'tasklist /fi "PID eq 516" /nh /fo:csv' ) do ( echo %~a )
for /f %a in ( 'tasklist /fi "PID eq 516" ^| findstr "516"' ) do ( echo %a )

Or you could use wmic (the Windows Management Instrumentation Command-line tool) and get the full path to the executable:

或者您可以使用 wmic(Windows Management Instrumentation 命令行工具)并获取可执行文件的完整路径:

wmic process where processId=516 get name
wmic process where processId=516 get ExecutablePath

Or you could download Microsoft PsTools, or specifically download just the pslist utility, and use PsList:

或者您可以下载 Microsoft PsTools,或者专门下载 pslist 实用程序,然后使用 PsList:

for /f %a in ( 'pslist 516 ^| findstr "516"' ) do ( echo %a )

回答by Magoo

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a pid=1600
FOR /f "skip=3delims=" %%a IN ('tasklist') DO (
 SET "found=%%a"
 SET /a foundpid=!found:~26,8!
 IF %pid%==!foundpid! echo found %pid%=!found:~0,24%!
)

GOTO :EOF

...set PID to suit your circumstance.

...设置PID以适合您的情况。