Powershell Get a specific process counter with an id process

I want to get specific counters for processes for which I have a process identifier. However, I cannot think of a way to use the where-object to match the counter process. how

Where Gc '\process(*)\id process -eq 456 gc '\process($name)\working set' 

So use the process id to get the name and get the working set (or something like that).

+4
source share
4 answers

You can get counters for a process name, first get the process name using its identifier, and then insert the process name into the counter. For instance:

 $id = # your process id $proc = (Get-Process -Id $id).Name Get-Counter -Counter "\Process($proc)\% Processor Time" 
+3
source

It seems a bit confusing to get the correct performance counter path for a process with multiple instances of the same process name:

 $proc_id=6580 $proc_path=((Get-Counter "\Process(*)\ID Process").CounterSamples | ? {$_.RawValue -eq $proc_id}).Path Get-Counter ($proc_path -replace "\\id process$","\% Processor Time") Timestamp CounterSamples --------- -------------- 11/20/2014 5:39:15 PM \\myhost\process(conhost#2)\% processor time : 0 
+5
source

If you want a solution that also includes a process with multiple instance IDs, you can use:

 $p = $((Get-Counter '\processus(*)\id de processus' -ErrorAction SilentlyContinue).CounterSamples | % {[regex]$a = "^.*\($([regex]::Escape($_.InstanceName))(.*)\).*$";[PSCustomObject]@{InstanceName=$_.InstanceName;PID=$_.CookedValue;InstanceId=$a.Matches($($_.Path)).groups[1].value}}) $id = # your process id $p1 = $p | where {$_.PID -eq $id} Get-Counter -Counter "\Process($($p1.InstanceName+$p1.InstanceId))\% Processor Time" # In french # Get-Counter -Counter "\Processus($($p1.InstanceName+$p1.InstanceId))\% temps processeur" 
+2
source

This is easy with the get-process cmdlet. Just filter the output for the process id that you want using the where-object , then select the options you are interested in:

 get-process | where-object{ $_.id -eq 456 } | select name,workingset 
+1
source

All Articles