Find top 10 processes with wmic command

China☆狼群 提交于 2019-12-11 22:58:45

问题


I know I can do 'wmic process list brief' to display a list of processes. Is there a way to view only the top 10 processes using the most memory?


回答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

Output example:

  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:


Over at DosTips I posted JSORT.BAT - a hybrid JScript/batch utility that can numerically sort stdin input, starting at a given line position, and write the result to stdout. The utility is pure script that will run on any Windows machine from XP onward.

I decided to extend JSORT.BAT to support two new options: /S n so you can skip (preserve) the header, and /C n so you can get just the top n records.

It then becomes very simple to work directly with the output of wmic process list brief. The only trick is I had to compute the line position of the WorkingSetSize column, as it varies depending on the longest length of the listed file names.

@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

Here is the JSORT.BAT code at the time this answer was written. Follow the JSORT.BAT link above for the most up-to-date version.

@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);


来源:https://stackoverflow.com/questions/26487895/find-top-10-processes-with-wmic-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!