Ale*_*noy 5 windows cpu powershell performance monitor
我希望能够每5秒向顶级CPU消费者输出一个日志文件.这样我就可以在测试期间看到谁使用了大部分cpu.
我发现这个答案很常见:
$cpu = Get-Counter -ComputerName localhost "\Process(*)\% Processor Time" `
| Select-Object -ExpandProperty countersamples `
| where {$_.InstanceName -ne 'idle' } `
| where {$_.InstanceName -ne '_total' }`
| Select-Object -Property instancename, cookedvalue `
| Sort-Object -Property cookedvalue -Descending `
| Select-Object -First 5 `
| ft @{L='Date';E={Get-Date}}, InstanceName, @{L='CPU';E={(($_.Cookedvalue/100)/$NumberOfLogicalProcessors).toString('P')}} -HideTableHeaders `
| Format-Table -Auto | Out-String
Run Code Online (Sandbox Code Playgroud)
我有2个问题:
有时我得到:
获取计数器:其中一个性能计数器样本中的数据无效.查看每个PerformanceCounterSample对象的Status属性,以确保它包含有效数据.
我想获得完整的进程名称,而不是
java 25% idea64 0.8% ...
我将尝试使用以下脚本立即回答您的两个问题:
Get-Counter "\Process(*)\% Processor Time" -ErrorAction SilentlyContinue `
| select -ExpandProperty CounterSamples `
| where {$_.Status -eq 0 -and $_.instancename -notin "_total", "idle"} `
| sort CookedValue -Descending `
| select TimeStamp,
@{N="Name";E={
$friendlyName = $_.InstanceName
try {
$procId = [System.Diagnostics.Process]::GetProcessesByName($_.InstanceName)[0].Id
$proc = Get-WmiObject -Query "SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId=$procId"
$procPath = ($proc | where { $_.ExecutablePath } | select -First 1).ExecutablePath
$friendlyName = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($procPath).FileDescription
} catch { }
$friendlyName
}},
@{N="CPU";E={($_.CookedValue/100/$env:NUMBER_OF_PROCESSORS).ToString("P")}} -First 5 `
| ft -a -HideTableHeaders
Run Code Online (Sandbox Code Playgroud)
这导致下表:
24.07.2016 21:00:53 Microsoft Edge Content Process 9,68%
24.07.2016 21:00:53 system 0,77%
24.07.2016 21:00:53 Microsoft Edge 0,39%
24.07.2016 21:00:53 runtimebroker 0,39%
24.07.2016 21:00:53 Host Process for Windows Services 0,39%
Run Code Online (Sandbox Code Playgroud)
获取计数器:其中一个性能计数器样本中的数据无效.查看每个PerformanceCounterSample对象的Status属性,以确保它包含有效数据.
这与Windows环境中的进程管理有关.在执行查询时,可能会出现一些进程,其中一些可能会消失(即wmiprvse进程负责执行wmi查询).某些进程可能需要您拥有更多权限.这一切都在获取过程信息时导致错误.可以使用-ErrorAction SilentlyContinueswitch 安全地跳过它,并使用Status -eq 0表达式进行过滤.
GetVersionInfo方法的可执行文件更好.如果这些信息可用,那么FileDescription物业商店就有价值.如果它不可用,则使用非友好的进程名称.| 归档时间: |
|
| 查看次数: |
2652 次 |
| 最近记录: |