Get-Utilization of System Resources
Once i was working in client environment where i notice lot of alerts received for CPU & Physical memory usage and started working on those alerts as usual by taking RDP session of each server then opening task manager to have a look at performance tab, one idea was running in my mind to have a script in place to check the utilization through some command like get-utilization.
I found number of alerts were fake or maybe it was CPU spike for 5-10 minutes but it were mine alerts therefore it was mine responsibility to check each server, then one day i got time and decided to create one script to analyse system resources remotely rather than login to each server, yes there is an alternate using windows performance tools but it takes so much of time to add remote server counters and monitor.
Here is a custom function written in below script which can be executed with powershell profile and it will be ready whenever power shell is open you have to run command get-utilization ServerName
This script will check the utilization of computer/server on the basis of some counters including CPU, Total Memory, Used Memory, Free Memory, Commited Bytes & Disk Time.
For Remote Computer
Get-Utilization RemoteComputer
#——————————————–
function get-utilization {
if (!$args) {
$p = Get-Counter -counter “\Processor(_total)\% Processor Time”
$CS = gwmi win32_computersystem
$TotMemory = $cs.totalphysicalmemory/(1024*1024)
$MemoryAvailable = get-counter -counter “\Memory\Available MBytes”
$freeMemory = $($MemoryAvailable.countersamples).cookedvalue
$UsedMemory = $TotMemory – $freeMemory
$UsedMemoryper = $UsedMemory * 100/ $TotMemory
$FreeMemoryPer = 100 – $UsedMemoryper
$DiskTime =$((get-counter -counter “\physicaldisk(_total)\% disk time”).countersamples).cookedvalue
$CommitedBytes = $((get-counter -counter “\memory\% committed bytes in use”).countersamples).cookedvalue
$TotMemoryGB = $cs.totalphysicalmemory / 1GB
}
else
{
$p = Get-Counter -counter “\Processor(_total)\% Processor Time” -ComputerName $args
$CS = gwmi win32_computersystem -ComputerName $args
$TotMemory = $cs.totalphysicalmemory/(1024*1024)
$MemoryAvailable = get-counter -counter “\Memory\Available MBytes” -ComputerName $args
$freeMemory = $($MemoryAvailable.countersamples).cookedvalue
$UsedMemory = $TotMemory – $freeMemory
$UsedMemoryper = $UsedMemory * 100/ $TotMemory
$FreeMemoryPer = 100 – $UsedMemoryper
$DiskTime = $((get-counter -counter “\physicaldisk(_total)\% disk time” -ComputerName $args ).countersamples).cookedvalue
$CommitedBytes = $((get-counter -counter “\memory\% committed bytes in use” -ComputerName $args ).countersamples).cookedvalue
$TotMemoryGB = $cs.totalphysicalmemory / 1GB
}
$CusTable = new-object -TypeName psobject -Property @{
“CPU %” = “{0:N3}” -f $($p.countersamples).cookedvalue
“Commited Bytes %” = “{0:N2}” -f $CommitedBytes
“Disk Time %” = “{0:N3}” -f $DiskTime
“Used Memory %” = “{0:N2}” -f $UsedMemoryper
“Free Memory %” = “{0:N2}” -f $FreeMemoryPer
“Total Memory” = “{0:N2}” -f $TotMemoryGB
}
$CusTable | select “CPU %”, “Total Memory”, “Commited Bytes %”, “Used Memory %”, “Free Memory %”, “Disk Time %” |Format-Table -AutoSize
}
#——————————————–
Script Output