Question

I need to monitor the CPU and the memory usage in windows servers when executing performance tests on them but I need them to be via script.

In Unix systems, I have the scripts and, for example, to monitor the cpu, I use this line:

cpuPerc=$(top -n 1 -b|grep Cpu|awk '{print $2}'|cut -d"%" -f1"")

And then, I send it to a file or to terminal with an echo.

Thanks.

Was it helpful?

Solution

If you want to do it with powershell you can see an simple example here : http://technet.microsoft.com/en-us/magazine/ee872428.aspx

If you are on a localised windows (not english), you will have to get the correct counter name before. For example in my french windows version it will be :

Get-counter -ListSet *

this will display all the available counterset. I can see there is a set named "processeur" wich is the french word for CPU. So now i can get the available counter for this set like this:

Get-counter -ListSet "processeur" |select -expand counter

it gives me the result :

PS>Get-counter -ListSet "processeur" |select  -expand counter 
\Processeur(*)\% temps processeur                             
\Processeur(*)\% temps utilisateur                            
\Processeur(*)\% temps privilégié                             
\Processeur(*)\Interruptions/s                                
\Processeur(*)\% temps DPC                                    
\Processeur(*)\% temps d'interruption                         
\Processeur(*)\DPC mis en file d'attente/s                    
\Processeur(*)\Taux DPC                                       
\Processeur(*)\% d'inactivité                                 
\Processeur(*)\% durée C1                                     
\Processeur(*)\% durée C2                                     
\Processeur(*)\% durée C3                                     
\Processeur(*)\Transitions C1/s                               
\Processeur(*)\Transitions C2/s                               
\Processeur(*)\Transitions C3/s                               

Now I can use one or more of these counter like this :

  • get all the counters : Get-counter -ListSet "processeur" |select -expand counter | foreach{ get-counter $_ -SampleInterval 2 -MaxSamples 10}
  • get inactivity counter : Get-counter -ListSet "processeur" |select -expand counter |where {$_ -match "inactivité"} | foreach{ get-counter $_ -SampleInterval 2 -MaxSamples 10}

At least, you can pipe the result to export-counter in order to save it to a file (CSV, TSV, or BLG). Hope that's help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top