C/C++教程

powershell提取前10个process的内存和cpu占比

本文主要是介绍powershell提取前10个process的内存和cpu占比,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

$Processes = @()
Get-WmiObject -Class Win32_PerfFormattedData_PerfProc_Process |
    Where-Object {$_.name -notin "_Total","Idle","Memory Compression"} | ForEach-Object{
        if($_.Name -match '^.*(?=#\d+)'){
            $name = $Matches.Values
        }
        else{
            $name = $_.Name
        }
        $cpu = $_.PercentProcessorTime/$Env:NUMBER_OF_PROCESSORS
        $ws = $_.WorkingSet
        if($name -notin $Processes.ProcessName){
            $Processes += [pscustomobject]@{
                "ProcessName" = $name
                "WS" = $ws
                "CPU" = $cpu
            }
        }
        else{
            $Processes | Where-Object {$_.ProcessName -eq $name} | ForEach-Object{
                $_.WS += $ws
                $_.CPU += $cpu
            }
        }
 }

 $Processes | Sort-Object -Property WS -Descending | Select-Object -Property ProcessName,WS -First 10 | Out-Host
$Processes | Sort-Object -Property CPU -Descending | Select-Object -Property ProcessName,CPU -First 10 | Out-Host

这篇关于powershell提取前10个process的内存和cpu占比的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!