Cpu Load for specific process with details.

I found good script which shows what is value of cpu Load for specific process.

Here is the code:

"# Option A: This is if you just have the name of the process; partial name OK
$ProcessName = “java”

# Option B: This is for if you just have the PID; it will get the name for you
#$ProcessPID = “6860”

#$ProcessName = (Get-Process -Id $ProcessPID).Name
$CpuCores = (Get-WMIObject Win32_ComputerSystem).NumberOfLogicalProcessors
$Samples = (Get-Counter “\Process($Processname*)\% Processor Time”).CounterSamples

$Samples | Select `
InstanceName,
@{Name=”CPU %”;Expression={[Decimal]::Round(($_.CookedValue / $CpuCores), 2)}}"

It shows me all java process on computer with cpuloads of that process:

"InstanceName                                                                         CPU %
------------                                                                         -----
java                                                                                     0
java                                                                                  1,92
java                                                                                 29,23
java                                                                                  4,23"

What if I want to add more columns to that report.

I need that report shows:

Instance name, CPU %, process PID, and most important cmdline of that process.

Is that possible using a script above, with some kind of corrections  ?

Greetings

Alex

 

Parents
  • Ok.

    I earlier tried something like that, but command:

    $proc = (Get-Process -Id $NamedProcess.ProcessId).Name

    gives us Name of process svchost (in your script) java (in mine).

    Next command:

    Get-Counter -Counter "\Process($proc*)

    means that we get the counter for first process starting with svchost or java and as a result we have an error.

    Why in your results we get not unique PID's ?

    For correct query PID's should be unique because of:

    "ForEach($NamedProcess in $ProcessData)
    {
        $proc = (Get-Process -Id $NamedProcess.ProcessId).Name"

    Only PID's are unique in Get-Process but PID is useless in Get-Counter.

    This is a problem and I don't know how to solve it :(

     

Reply Children
No Data