Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Get a specific process counter with id process

I want to get specific counters for processes that I have process id's for. However I can't think of a way to use where-object to match the process for the counter. Like

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

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

like image 517
Ken J Avatar asked Jun 16 '12 22:06

Ken J


People also ask

How do I find the process ID in powershell?

To find the PID of a process, type `Get-Process`. Indicates that the UserName value of the Process object is returned with results of the command. Specifies one or more process objects. Enter a variable that contains the objects, or type a command or expression that gets the objects.

How do I get performance counters in powershell?

To view the performance counter output from the job, use the Receive-Job cmdlet. Start-Job uses the ScriptBlock parameter to run a Get-Counter command. Get-Counter uses the Counter parameter to specify the counter path \LogicalDisk(_Total)\% Free Space .

What is Cookedvalue in powershell?

The raw values and second values are the raw ingredients used by the performance counter, and the "cooked value" is the result of "cooking" those ingredients into something for human consumption.

How do I view a process in Powershell?

The Get-Process cmdlet gets the processes on a local or remote computer. Without parameters, this cmdlet gets all of the processes on the local computer. You can also specify a particular process by process name or process ID (PID) or pass a process object through the pipeline to this cmdlet.


2 Answers

It seems a bit convoluted 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
like image 143
Andrew A. Avatar answered Oct 13 '22 12:10

Andrew A.


You can get counters for a process name so first get the process name by using its Id and then embed the process name in the counter. For example:

$id = # your process id
$proc = (Get-Process -Id $id).Name
Get-Counter -Counter "\Process($proc)\% Processor Time"
like image 23
Shay Levy Avatar answered Oct 13 '22 10:10

Shay Levy