We want to report CPU usage % by PID. We have a Powershell script in place; however it reports Instance Name, not PID. The Powershell script we have is:
$Samples = (Get-Counter “\Process($Processname*)% Processor Time” -ErrorAction SilentlyContinue).CounterSamples
$Samples | Select InstanceName, @{Name=“CPU”;Expression={[Decimal]::Round(($_.CookedValue / 2), 2)}}
Not from this command. You can fudge it as long as the process you’re after is the only instance of the same name, but as soon as you get more than one process with the same name (easy example would be Chrome) you have no way of determining ID from the data that this counter reports.
I would be looking to see if you can use Get-Counter to in some way pull this data by PID instead, and pull PIDs from Get-Process – OR just try to get this data via Get-Process, which always returns the IDs anyway.