help with this script

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Vern Anderson Vern Anderson 3 years ago.

  • Author
    Posts
  • #14645
    Profile photo of Avi Grinberg
    Avi Grinberg
    Participant

    i want a script to capture the current CPU usage for every process above 0 CPU usage.
    i create this script but i see that the script doesn't report a right CPU usage like task manager show because it doesn't calculated the processor threads so i show 99 CPU usage. i found this code that show the CPU count [System.Environment]::ProcessorCount
    my question is how do i insert the code into the cookedvalue results that it will reflect like task manager?

    Get-Counter '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| where {$_.instancename -ne "_Total" -and $_.instancename -ne "Idle" -and $_.cookedvalue -gt 0} | Sort-Object -Property cookedvalue -Descending | ft -AutoSize

    THX

  • #14650
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Have you considered just running Get-Process? That shows CPU usage on a per-process basis.

    But there's no way to insert a value into a performance counter.

    Also, [System.Environment]::ProcessorCount shows the *number of processors in the machine*, not the CPU usage.

    So I'm confused about what you want.

  • #14651
    Profile photo of Avi Grinberg
    Avi Grinberg
    Participant

    basically i simple want to get the current CPU usage like task manager show in the processes...

  • #14652
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Get-process will do that.

  • #14653
    Profile photo of Avi Grinberg
    Avi Grinberg
    Participant

    no get-process will not show the active running process like task manager show in regard to the CPU usage.

  • #14660
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    This is about as close as I have gotten to what I think you're looking for.


    (Get-Counter "\\$ENV:COMPUTERNAME\process(*)\% processor time").CounterSamples | Where-Object {$_.InstanceName -notlike '_total'} | Sort-Object CookedValue -Descending | Format-Table @{Name="Process";Expression={$_.InstanceName}},@{Name="Percent CPU";Expression={$_.CookedValue/[int]$Env:NUMBER_OF_PROCESSORS}; Format="F2"; Alignment="Left"} -AutoSize

You must be logged in to reply to this topic.