通过 Powershell 进行内存和 CPU 监控

Memory and CPU monitoring via Powershell

我正在尝试使用 powershell 监控 cpu 和特定进程的内存使用情况。这个想法是安排它并让它以特定的时间间隔获取值,然后将它们吐出到 .csv.

到目前为止,我已经 运行 分为 2 个案例:

  1. 进程只有一个进程 ID。 代码工作正常。
  2. 该流程有几个子流程,每个子流程都有自己的 ID。 我希望它在最终输出中添加值。我对每个子流程的单个值(例如每个 chrome 选项卡)不感兴趣,但对所有子流程的累积值不感兴趣。

这是期望的输出:(如果流程有子流程,计数应该反映出来)

Enter the name of the process to monitor: xmrig

21048
------SAMPLES------

TimeStamp    : 2021-04-11 22:32:09
InstanceName : xmrig
CookedValue  : 2289,4108563869    
Count        : 1
Sum          : 15671296

TimeStamp           InstanceName CPU % Count SumMB
---------           ------------ ----- ----- -----
2021-04-11 22:32:09 xmrig        95,39     1 14,95

他的是代码:

clear

$ProcessName = Read-Host -Prompt 'Enter the name of the process to monitor'
#Write-host $ProcessName
try{
    $ProcessId = Get-Process $ProcessName -ErrorAction Stop | select -expand id
}
catch
{
write-host ("No such process found, try again.") -ForegroundColor Red
$ProcessName = Read-Host -Prompt 'Enter the name of the process to monitor'
}

 $AllProcessIds = Get-Process $ProcessName | select -expand id
Write-Host ""
#Write-Output ("Process "+ $ProcessName + " has ID: "+ $ProcessId )
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$CPUSamples =@()
$MEMSamples =@()
foreach ($ProcessId in $AllProcessIds){
    Write-Host $ProcessId -ForegroundColor red  
    $CPUSamples += (Get-Counter "\Process($ProcessName*)\% Processor Time").CounterSamples | Select-Object *
    $MEMSamples += Get-Process $ProcessName | Measure-Object Workingset -sum -average  
} 

<# Write-Host "------MEM SAMPLES------"
$MEMSamples
Write-Host "------CPU SAMPLES------"
$CPUSamples #>

$Samples = New-object psobject;
$Samples | Add-Member NoteProperty "TimeStamp" $CPUSamples.Timestamp;
$Samples | Add-Member NoteProperty "InstanceName" $CPUSamples.InstanceName;
$Samples | Add-Member NoteProperty "CookedValue" $CPUSamples.CookedValue;
$Samples | Add-Member NoteProperty "Count" $MEMSamples.Count;
$Samples | Add-Member NoteProperty "Sum" $MEMSamples.Sum;


Write-Host "------SAMPLES------"
$Samples

$Samples.CookedValue | ForEach-object -begin {$CookedValueSum =0} -process {$CookedValueSum+=$_} -end{}

#$Samples| Select-Object TimeStamp, InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}},Count, @{Name="SumMB";Expression = {[math]::round($_.Sum/1MB,2)}} | Format-Table -AutoSize

$Samples| Select-Object TimeStamp, InstanceName, @{Name="CPU %";Expression={[Decimal]::Round(($_.CookedValueSum / $CpuCores), 2)}},Count, @{Name="SumMB";Expression = {[math]::round($_.Sum/1MB,2)}} | Format-Table -AutoSize

关于如何解决这个问题有什么想法吗?有没有更简单的方法或者我是否使事情过于复杂? 非常感谢任何帮助!

通过阅读您的 post,我不确定您的问题是什么。是不是你想达到这样的目的?

$Name = "svchost"
$processes = Get-Process $Name
$cpu_cores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors

[PSCustomObject]@{
    Timestamp = (Get-Date)
    InstanceName = $Name
    Count = $processes.Count
    Memory = ($Processes.WorkingSet | Measure-Object -Sum).Sum
    CPU = "$([Math]::Round(($processes.cpu | Measure-Object -Sum).Sum / $cpu_cores, 2))%"
} | ft
Timestamp             InstanceName Count     Memory CPU  
---------             ------------ -----     ------ ---  
4/12/2021 12:27:53 AM svchost         97 2110627840 4.54%

这个轻微的重构怎么样。

Clear-Host

$ProcessName = 'idle'

try
{
    $ProcessId = Get-Process $ProcessName -ErrorAction Stop | 
    Select-Object -ExpandProperty id
}
catch
{
    write-host ("No such process found, try again.") -ForegroundColor Red
    $ProcessName = Read-Host -Prompt 'Enter the name of the process to monitor'
}

 $AllProcessIds = Get-Process $ProcessName | 
 Select-Object -ExcludeProperty id

$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors

$CPUSamples = @()
$MEMSamples = @()

foreach ($ProcessId in $AllProcessIds)
{
    $CPUSamples += (Get-Counter "\Process($ProcessName*)\% Processor Time").CounterSamples | 
    Select-Object -Property '*'

    $MEMSamples += Get-Process $ProcessName | 
    Measure-Object Workingset -sum -average  
} 


$Samples = New-object psobject

$Samples | 
Add-Member NoteProperty 'TimeStamp' $($CPUSamples.Timestamp | 
Select-Object -First 1)

$Samples | 
Add-Member NoteProperty 'InstanceName' ($CPUSamples.InstanceName | 
Select-Object -Unique)

$Samples | 
Add-Member NoteProperty 'CookedValue' $($CPUSamples.CookedValue | 
Measure-Object -Sum).Sum

$Samples | 
Add-Member NoteProperty 'Count' $MEMSamples.Count

$Samples | 
Add-Member NoteProperty 'Sum' $($MEMSamples.Sum | 
Measure-Object -Sum).Sum


Write-Host '------SAMPLES------'
$Samples

$Samples.CookedValue | 
ForEach-object -begin {$CookedValueSum =0} -process {$CookedValueSum+=$_} -end{}

$Samples | 
Select-Object TimeStamp, 
InstanceName, 
@{
    Name       = 'CPU %'
    Expression = {[Decimal]::Round(($_.CookedValueSum / $CpuCores), 2)}
},
Count, 
@{
    Name       = 'SumMB'
    Expression = {[math]::round($_.Sum/1MB,2)}
} | 
Format-Table -AutoSize

# Results
<#
------SAMPLES------


TimeStamp    : 4/11/2021 8:00:35 PM
InstanceName : explorer
CookedValue  : 0
Count        : 1
Sum          : 167120896




TimeStamp            InstanceName CPU % Count  SumMB
---------            ------------ ----- -----  -----
4/11/2021 8:00:35 PM explorer         0     1 159.38


------SAMPLES------


TimeStamp    : 4/11/2021 8:01:05 PM
InstanceName : runtimebroker
CookedValue  : 0
Count        : 3
Sum          : 262680576




TimeStamp            InstanceName  CPU % Count  SumMB
---------            ------------  ----- -----  -----
4/11/2021 8:01:05 PM runtimebroker     0     3 250.51


------SAMPLES------


TimeStamp    : 4/11/2021 8:01:26 PM
InstanceName : svchost
CookedValue  : 0
Count        : 25
Sum          : 14374760448




TimeStamp            InstanceName CPU % Count    SumMB
---------            ------------ ----- -----    -----
4/11/2021 8:01:26 PM svchost          0    25 13708.84


------SAMPLES------


TimeStamp    : 4/11/2021 8:02:10 PM
InstanceName : idle
CookedValue  : 798.371953662604
Count        : 1
Sum          : 8192




TimeStamp            InstanceName CPU % Count SumMB
---------            ------------ ----- ----- -----
4/11/2021 8:02:10 PM idle             0     1  0.01
#>