[英]How to sum CPU usage value of processes name in Powershell
I have a powershell script to check CPU Usage of process in Window. 我有一个powershell脚本来检查Window中进程的CPU使用率。
$Output=""
$Processes = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
foreach($Process in $Processes)
{
$Output += $Process.name + "=" + $Process.PercentProcessorTime + " "
}
Write-Host "${Output}"
I ran it. 我跑了 The result have many same process name.
结果具有许多相同的进程名称。 Ex:
例如:
chrome=0 chrome#1=0 chrome#2=0 chrome#3=0 chrome#4=0 chrome#5=0 chrome#6=0 chrome#7=0 chrome#8=0 chrome#9=0 PUTTY=0 chrome#10=18 chrome#11=0
I want to list all process and CPU Usage of them as in picture. 我想列出它们的所有进程和CPU使用情况,如图所示。 And same process will be sum.
和相同的过程将是总和。
I want to sum value same process as below: 我想总结以下相同的值:
$Processes = get-process | Group-Object -Property ProcessName
$Output="OK |"
foreach($Process in $Processes)
{
$Obj = New-Object psobject
$Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Process.Name
$Obj | Add-Member -MemberType NoteProperty -Name Mem -Value ($Process.Group|Measure-Object WorkingSet -Sum).Sum
$Output += $Process.Name + "=" + $($Process.Group|Measure-Object WorkingSet -Sum).Sum +" "
}
Write-Host "${Output}"
And result is sum of same process: 结果是相同过程的总和:
armsvc=1736704 ASDSvc=11309056 audiodg=18563072 bash=1323008 calc=4136960 chrome=2138599424
You are concatenating together a string (of text) rather than adding up the total. 您是将一个字符串(文本)串联在一起,而不是将总数相加。
This will give you the total process processor usage. 这将为您提供过程处理器的总使用量。 *Note: the total will be greater than 100%, because the output also includes the
_Total
and Idle
(bot 100% on my machine) *注意:总数将大于100%,因为输出还包括
_Total
和Idle
(我的机器上的机器人100%)
$Output=0
$Processes = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
foreach($Process in $Processes)
{
$Output += $Process.PercentProcessorTime
}
Write-Host "Total Processor Usage: $Output %"
Example output: 输出示例:
Total Processor Usage: 318 %
处理器总使用率:318%
This should output something similar to what you see in the task manager. 这将输出类似于您在任务管理器中看到的内容。
$Processes = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
$Processes | %{ New-Object psobject -Property `
@{ Time = $_.PercentProcessorTime;
Name = ($_.name -replace "#\d+", "" )}}`
| ?{ $_.Name -notmatch "_Total|Idle" } `
| Group-Object Name `
| %{ New-Object psobject -Property `
@{ Name = $_.Name;
Sum = ($_.Group | Measure-Object Time -Sum ).Sum }} `
| Format-Table
$Processes = (get-wmiobject Win32_PerfFormattedData_PerfProc_Process)
$procStrings = @()
$Processes | %{ New-Object psobject -Property `
@{ Time = $_.PercentProcessorTime;
Name = ($_.name -replace "#\d+", "" )}}`
| ?{ $_.Name -notmatch "_Total|Idle" } `
| Group-Object Name `
| %{
$procStrings += "$($_.Name)=$(($_.Group | Measure-Object Time -Sum ).Sum)"}
$procStrings -join " "
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.