CPU % used by a process or just idle

Topics: Developer Forum, User Forum
Sep 1, 2007 at 10:13 PM
(C#/.NET novice)

I'd like to contribute a cmdlet that will print out the CPU % used by a process (or just idle). What are your thoughts? I'm thinking that without any 'time parameters', we'd print out the % used (or idle) for just 1 second. The option would be there to pick a longer time, but the problem would be that the shell would be blocked while there's a sleep going on.

I'm also thinking about values like 'since boot', 'since process started', and whether there's a way to use PSEventing in this too... Being able to watch a process and even record its min and max values...

Sep 10, 2007 at 12:00 AM
Sounds interesting. Could you give a few examples of usage (even if you have to make up the output)?
Sep 12, 2007 at 6:29 PM
Edited Sep 12, 2007 at 7:10 PM
Lee Holmes already did this ... http://www.leeholmes.com/blog/AccessingPerformanceCountersInPowerShell.aspx (not saying I wouldn't like to have this in PSCX -- I want a windows version of 'top').

You can add this to your types but according to MSDN you have to call .NextValue() and then wait a second tand call it again to get relevant CPU use info ...

      $percent = new-object System.Diagnostics.PerformanceCounter `
            "Process","% Processor Time",$this.Processname
      $null=$percent.NextValue(); sleep 1;
      return $percent.NextValue();

The only way this is really useful is if you create and maintain a cache of them ... similar to how Lee did it.