Jason Stallard
Jason Stallard

Reputation: 348

Cannot find the process identifier on remote computer using Powershell

I am trying to get the highest CPU usage processes and the users running them with the following code:

$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
}

but get

Get-Process : Cannot find a process with the process identifier 20060. At C:\Users\Jasons1\CPUUsage.ps1:13 char:20 + Description = (Get-Process -Id $Process.IDProcess).Description + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (20060:Int32) [Get-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand

for -First times.

Upvotes: 0

Views: 1485

Answers (1)

Esperento57
Esperento57

Reputation: 17462

  Description = (Get-Process -Id $Process.IDProcess).Description

by

  Description = (gwmi Win32_Process -ComputerName $ComputerName -Credential $cred | where {$_.ProcessId -eq $Process.IDProcess}).Description 

Upvotes: 1

Related Questions