Categories

10409

Process information

Recent forum question got me thinking about retrieving process information. In this case it was about a processes Working Set

 

Standard response is

PS> Get-Process powershell | select *working*


MaxWorkingSet    : 1413120
MinWorkingSet    : 204800
PeakWorkingSet   : 50548736
PeakWorkingSet64 : 50548736
WorkingSet       : 49737728
WorkingSet64     : 49737728

 

We could go WMI

 

PS> Get-WmiObject -Class Win32_Process -Filter "Name='Powershell.exe'" | select *working* | fl


MaximumWorkingSetSize : 1380
MinimumWorkingSetSize : 200
PeakWorkingSetSize    : 49364
WorkingSetSize        : 49811456

 

The results are the same but the units have changed.

 

Our final choice is to go directly for the performance counters

 

PS> Get-Counter -Counter "\Process(powershell)\Working Set Peak", "\Process(powershell)\Working Set", "\Process(powershell)\Working Set - Private"

Timestamp                 CounterSamples
---------                 --------------
11/09/2011 15:32:10       \\rslaptop01\process(powershell)\working set peak :
                          50548736

                          \\rslaptop01\process(powershell)\working set :
                          47411200

                          \\rslaptop01\process(powershell)\working set - private :
                          19300352

Processor % usage

In this post - http://msmvps.com/blogs/richardsiddaway/archive/2011/08/07/top-processes-by-resource.aspx – I looked at wrapping the Get-Process cmdlet in a function so that we could always get the top N sorted by a number of criteria.

I was asked if this showed % processor use. The answer is no. The CPU property show total CPU time used by a process since the machine started but as processes come and go it doesn’t show the whole story.

If you want to view the %processor usage of a process we can use Get-Counter.

PS> get-counter -counter "\Process(System)\% Processor Time"

Timestamp                 CounterSamples
---------                 --------------
11/08/2011 20:24:44       \\rslaptop01\process(system)\% processor time :
                          0

but we have lots of processes running so we need to find them all

Get-Counter -ListSet Process

shows the process performance object. If we expand the PathsWithInstances property

Get-Counter -ListSet Process | select -ExpandProperty PathsWithInstances

we get a bit more than we need – so we add a filter

Get-Counter -ListSet Process |
select -ExpandProperty PathsWithInstances |
where {$_ -like "*% Processor Time"}

which gives us all of the process % usage counters.

We might think to use them like this

Get-Counter -ListSet Process |             
select -ExpandProperty PathsWithInstances |             
where {$_ -like "*% Processor Time"} |            
foreach {            
Get-Counter -Counter $_             
            
}


But it accesses one counter at a time and there will be a few seconds difference on the time stamp of the first and last process. We could live with that but it would be better to get all the counters at the same time



PS> get-counter -counter "\Process(System)\% Processor Time", "\Process(Idle)\% Processor Time"



Timestamp                 CounterSamples
---------                 --------------
11/08/2011 20:31:08       \\rslaptop01\process(system)\% processor time :
                          1.53847140039448



                          \\rslaptop01\process(idle)\% processor time :
                          186.155039447732



It would be good if we could sort these in terms of usage



get-counter -counter "\Process(System)\% Processor Time", `

"\Process(Idle)\% Processor Time" |


select -ExpandProperty  countersamples |


select Instancename, Cookedvalue |


sort Cookedvalue -Descending | ft -a



InstanceName      CookedValue
------------      -----------
idle         196.924339250493
system                      0



All we have to do is create a list of all of the processes



function get-cpuuser {            
            
$procnames = Get-Counter -ListSet Process |             
select -ExpandProperty PathsWithInstances |             
where {$_ -like "*% Processor Time"}             
            
Get-Counter -Counter $procnames |            
select -ExpandProperty  Countersamples |             
select Instancename, Cookedvalue |             
sort Cookedvalue -Descending            
}


I get values of more than 100 sometimes – this is because I have multiple cores in the system

Start-Process

In this post - http://richardsiddaway.spaces.live.com/default.aspx?_c01_BlogPart=blogentry&_c=BlogPart&handle=cns!43CFA46A74CF3E96!2262 -  I showed different ways of starting a process.

Now that a post CTP3 version of PowerShell v2 is available in Windows 7 I can share this.  Thanks to however brought it to my attention.

If you are in PowerShell and need to kick off IE then just use

Start-Process www.microsoft.com

or better still one of these :-)

Start-Process "http://richardsiddaway.spaces.live.com"
Start-Process http://msmvps.com/blogs/RichardSiddaway/Default.aspx

Its all the little things like this that add up to PowerShell being so useful

Technorati Tags: ,,