Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

Powershell script: Powershell equivalent of Linux top

01 March,2016 by Tom Collins


One thing I like about Linux is the top command. The linux top command displays in a continious stream cpu movements. This real-time display is very useful when you're trying to identify any unusual patterns in cpu, or trying to identify the highest user.

I started thinking about you would have a similar structure using powershell. Getting the cpu information is straightforward.

 
Get-Process or ps will display the information with the headers:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName


Thats's easy. But to view any movements I need to continiously hit the refresh button. There are better things to do , particuarly if you can program the refresh sequence.

In Powershell you can set off a command and it will refresh without you needing to continiously click in the button. Replicating the same behaviour as Linux top is just a few extra keystrokes, and the effect is similar

Here is an example of Powershell scripting , that will display the top 10 processes based on CPU and refresh every 3 seconds. You can adjust the refresh rate and sort as it suits your requirements

 

while (1) {Get-Process | sort -desc cpu | select -first 10; 
sleep -seconds 3; cls; 
write-host "Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName"; 
write-host "-------  ------    -----      ----- -----   ------     -- -----------"}

A problem outlined to me by Rick Willemain in the blog post comments was the truncation of left digits in the CPU(s) column.

This is occurring due to sizing issues.  If you're experiencing this problem use this version of the code , which basically adds a pipe into the Format Table   functionality.

 

while (1) {Get-Process | sort -desc cpu | select -first 10 | format-table -autosize; 
sleep -seconds 3; cls; 
write-host "Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName"; 
write-host "-------  ------    -----      ----- -----   ------     -- -----------"}

 

 

 

Read More on Powershell cpu

Number of physical processors with Powershell – number of cores ...

How to find CurrentClockSpeed and MaxClockSpeed using Powershell Get-WmiObject win32_processor


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Powershell script: Powershell equivalent of Linux top


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer