使用 wmic 命令查找前 10 个进程
Posted
技术标签:
【中文标题】使用 wmic 命令查找前 10 个进程【英文标题】:Find top 10 processes with wmic command 【发布时间】:2014-10-21 13:27:43 【问题描述】:我知道我可以执行“wmic process list brief”来显示进程列表。有没有办法只查看使用最多内存的前 10 个进程?
【问题讨论】:
是的,有(起点here on this post)但是...您可以删除批处理文件并切换到 PowerShell 吗? 如果可能的话,我想把它全部保存下来。 【参考方案1】:@echo off
setlocal EnableDelayedExpansion
(for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
set "size= %%b"
echo !size:~-10! %%a
)) > wmic.txt
set i=0
for /F "skip=1 delims=" %%a in ('sort /R wmic.txt') do (
echo %%a
set /A i+=1
if !i! equ 10 goto :end
)
:end
del wmic.txt
输出示例:
96931840 iexplore.exe
82161664 explorer.exe
42319872 svchost.exe
33656832 svchost.exe
31469568 dwm.exe
26943488 iexplore.exe
25690112 SearchIndexer.exe
18550784 svchost.exe
17002496 taskhostex.exe
16343040 svchost.exe
【讨论】:
【参考方案2】:在 DosTips 上,我发布了 JSORT.BAT - a hybrid JScript/batch utility,它可以从给定的行位置开始对标准输入输入进行数字排序,并将结果写入标准输出。该实用程序是纯脚本,可以在 XP 以后的任何 Windows 机器上运行。
我决定扩展 JSORT.BAT 以支持两个新选项:/S n
以便您可以跳过(保留)标头,/C n
以便您仅获取前 n 条记录。
然后直接使用wmic process list brief
的输出变得非常简单。唯一的技巧是我必须计算 WorkingSetSize 列的行位置,因为它取决于列出的文件名的最长长度。
@echo off
setlocal
:: Get full proc list in unicode
wmic process list brief >procList.tmp
:: Convert unicode to ANSII
type procList.tmp >procList.tmp2
:: Read the header line
<procList.tmp2 set /p "header="
:: Identify position of WorkingSetSize column
for /f "delims=W" %%A in ("%header%") do echo %%A>skipSize.tmp
for %%A in (skipSize.tmp) do set /a pos=%%~zA-1
:: Print the header, followed by the top 10 sorted by WorkingSetSize
type procList.tmp2 | jsort2 /n /r /p %pos% /s 1 /c 10
:: Delete the temp files
del procList.tmp procList.tmp2 skipSize.tmp
这是编写此答案时的 JSORT.BAT 代码。按照上面的 JSORT.BAT 链接获取最新版本。
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::************ Documentation ***********
::JSORT.BAT version 2.1
:::
:::JSORT [/Option [Value]]...
:::
::: Sort lines of text from stdin and write the result to stdout.
::: JSORT uses an ascending, case sensitive text sort by default.
:::
::: Options:
:::
::: /I - Ignore case
:::
::: /C n - Number of sorted lines to print. Skipped lines are always printed
::: and do not contribute to the count. Default is -1 (all lines).
:::
::: /N - Sort consecutive digits as numbers instead of text. The numbers
::: may be embedded within alpha text. JSort supports numbers up to
::: 20 digits long.
:::
::: /P n - Begin sorting at character position n. Lines that have fewer than
::: n characters are treated as equivalent values, and collate before
::: all other lines. The default value is 1 (first character).
:::
::: /R - Sort the lines in Reverse (descending) order.
:::
::: /S n - Number of lines to skip - default is 0.
::: Skipped lines are not sorted (remain in place)
:::
::: /V - Display the version of JSORT.BAT.
:::
::: /? - Display this help
:::
:::JSORT.BAT was written by Dave Benham and originally posted at
:::http://www.dostips.com/forum/viewtopic.php?f=3&t=5595
:::
::************ Batch portion ***********
@echo off
setlocal enableDelayedExpansion
:: Define options
set "options= /?: /i: /c:-1 /n: /p:1 /r: /s:0 /v:"
:: Set default option values
for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B"
:: Get options
:loop
if not "%~1"=="" (
set "test=!options:* %~1:=! "
if "!test!"=="!options! " (
>&2 echo Error: Invalid option %~1
exit /b 1
) else if "!test:~0,1!"==" " (
set "%~1=1"
) else (
set "%~1=%~2"
shift /1
)
shift /1
goto :loop
)
:: Display help
if defined /? (
for /f "delims=: tokens=*" %%A in ('findstr "^:::" "%~f0"') do echo(%%A
exit /b 0
)
:: Display version
if defined /v (
for /f "delims=: tokens=*" %%A in ('findstr /bc:"::JSORT.BAT version" "%~f0"') do echo %%A
exit /b 0
)
:: Transform and validate options
set /a "case=0%/i%, num=0%/n%, pos=%/p%-1, order=1-2*0%/r%, 1/^!(0x80000000&pos)" 2>nul || (
>&2 echo Error: Invalid /P value.
exit /b 1
)
:: Perform the sort
cscript //E:JScript //nologo "%~f0" %case% %num% %pos% %order% %/s% %/c%
exit /b 0
************* JScript portion **********/
var array=new Array(),
nocase =WScript.Arguments.Item(0),
numeric=WScript.Arguments.Item(1),
pos =WScript.Arguments.Item(2),
order =WScript.Arguments.Item(3),
skip =WScript.Arguments.Item(4),
count =WScript.Arguments.Item(5);
while (!WScript.StdIn.AtEndOfStream)
if (skip > 0)
WScript.Echo(WScript.StdIn.ReadLine());
skip-=1
else
var expanded="", num="", raw=WScript.StdIn.ReadLine(), upper=((nocase==1)?raw.toUpperCase():raw);
for( var i=pos; i<raw.length; i++ )
var c=upper.substr(i,1);
if (numeric==1 && c>="0" && c<="9")
num+=c;
else
if (num != "")
num="00000000000000000000" + num;
expanded+=num.substr(num.length-20);
num="";
expanded+=c;
if (num != "")
num="00000000000000000000" + num;
expanded+=num.substr(num.length-20);
var obj=expanded:expanded, raw:raw;
array.push(obj);
if (count<0) count=array.length;
if (count>array.length) count=array.length;
array.sort(function(a,b)return order*((a.expanded>b.expanded)-(a.expanded<b.expanded)););
for (var i=0; i<count; i++) WScript.Echo(array[i].raw);
【讨论】:
编辑 - 更正 /C 选项默认以上是关于使用 wmic 命令查找前 10 个进程的主要内容,如果未能解决你的问题,请参考以下文章
WMIC + Python 子进程/ (Windows 10)