【发布时间】:2016-11-01 17:15:41
【问题描述】:
我正在尝试使用以下代码获取 CPU 使用率最高的进程以及运行它们的用户:
$ProcessList = gwmi Win32_PerfFormattedData_PerfProc_Process -ComputerName $ComputerName -Credential $cred | select IDProcess,Name,PercentProcessorTime | where { $_.Name -ne "_Total" -and $_.Name -ne "Idle"} | sort PercentProcessorTime -Descending | select -First 3
$TopProcess = @()
ForEach ($Process in $ProcessList) {
$row = new-object PSObject -Property @{
Id = $Process.IDProcess
Name = $Process.Name
User = (gwmi Win32_Process -ComputerName $ComputerName -Credential $cred | where {$_.ProcessId -eq $Process.IDProcess}).GetOwner().User
CPU = $Process.PercentProcessorTime
Description = (Get-Process -Id $Process.IDProcess).Description
}
$TopProcess += $row
}
但是得到
Get-Process : 找不到进程标识符为 20060 的进程。 在 C:\Users\Jasons1\CPUUsage.ps1:13 char:20 + 描述 = (Get-Process -Id $Process.IDProcess).Description + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (20060:Int32) [Get-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand
第一次。
【问题讨论】:
标签: powershell