Powershell 中所有进程的 CPU 和内存使用率百分比 [英] CPU and memory usage in percentage for all processes in Powershell

查看:699
本文介绍了Powershell 中所有进程的 CPU 和内存使用率百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 PowerShell 中查看所有正在运行的进程的 CPU 和内存利用率百分比?

Is there a way for viewing CPU and memory utilization in percentage for all running processes in PowerShell?

我尝试了以下方法:

Get-Process | %{"Process={0} CPU_Usage={1} Memory_Usage={2}" -f $_.ProcessName,[math]::Round($_.CPU),[math]::Round($_.PM/1MB)}

与此同时,我解析了这些信息并从总内存中计算了内存使用百分比,但无法获得与 CPU 相同的结果,因为通过此命令它显示进程中的 CPU 时间划分,而不是本身的利用率.

Along with it I have parsed this information and calculated memory usage percentage from total memory, but couldn't get the same for CPU since by this command it show CPU time division in processes not utilization per se.

使用以下命令获取总内存和总 CPU 利用率:

Used the following commands for total memory and total cpu utilization:

  1. 总物理内存

  1. TOTAL PHYSICAL MEMORY

Get-WmiObject Win32_OperatingSystem |%{"{0}" -f $_.totalvisiblememorysize}

总 CPU 使用率

Get-WmiObject Win32_Processor |Select-Object -expand LoadPercentage

任何帮助将不胜感激.我需要以下格式:

Any help would be appreciated. I need it in the following format:

PROCESS=process_name CPU=percent_cpu_utilization_by_process MEMORY=percent_memory_utilization_by_process

推荐答案

您可以使用 WMI 对象 Win32_PerfFormattedData_PerfProc_Process 来收集所有这些数据.

You can use the WMI object Win32_PerfFormattedData_PerfProc_Process to collect all this data.

Get-WmiObject Win32_PerfFormattedData_PerfProc_Process `
    | Where-Object { $_.name -inotmatch '_total|idle' } `
    | ForEach-Object { 
        "Process={0,-25} CPU_Usage={1,-12} Memory_Usage_(MB)={2,-16}" -f `
            $_.Name,$_.PercentProcessorTime,([math]::Round($_.WorkingSetPrivate/1Mb,2))
    }

如果您需要占总内存百分比的内存,您可以在开始时添加以下内容以获得系统中的总 RAM.

If you need the memory in a percentage of total memory you can add the following at the start to get the total RAM in the system.

$RAM= Get-WMIObject Win32_PhysicalMemory | Measure -Property capacity -Sum | %{$_.sum/1Mb} 

然后将WorkingSetPrivate计算改为如下

([math]::Round(($_.WorkingSetPrivate/1Mb)/$RAM*100,2))

注意这个 WMI 对象中的 CPU stat 被四舍五入到最近的现在,我已经计算了当前使用的内存(以兆字节为单位).有关此类的更多信息,请参阅此链接.

Note the CPU stat in this WMI object is rounded to the nearest present and I have calculated the current used memory in Megabytes. For more information on this class see this link.

这篇关于Powershell 中所有进程的 CPU 和内存使用率百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆