Sample and display CPU core usage

Show the average usage for each CPU over a specified period of time._x000A_Can help to spot overloaded or over specified vCPU allocations for computers although the SBA should be run a number of times, particularly when under peak usage/load._x000A_Arguments:_x000A_ Capture Period – the number of seconds to monitor CPU usage for

Version: 1.5.10
Created: 2018-09-29T19:25:10.817
Modified: 2018-11-21T14:02:46.283
Creator: Guy Leech
Downloads: 235
Tags:
The Script Copy Script Copied to clipboard

<# Get each CPU's usage over a period and show least loaded @guyrleech 2018 Modification History: 20/11/18 GRL Always dispplay two decimal places so pad with zeroes if necessary #>

[int]$samplePeriod = $args[0]

[hashtable]$perCpuStats = @{}
[datetime]$first = 0
[bool]$gotFirst = $false
[datetime]$last = 0

Get-Counter -Counter ‘Processor(*)% Processor Time’ -SampleInterval 1 -MaxSamples $samplePeriod | ForEach-Object

{
    if( ! $gotFirst )
    {
        $first = $_.TimeStamp
        $gotFirst = $true
    }
    $last = $_.TimeStamp
    $_.CounterSamples | ForEach-Object

{
$thisCPU = $perCpuStats[ $_.InstanceName ]
if( $thisCpu )
{
$null = $thisCpu.Add( $_.CookedValue )
}
else
{
$perCpuStats.Add( $_.InstanceName , [System.Collections.ArrayList]@( $_.CookedValue ) )
}
}
}

“Average CPU utilisation over $samplePeriod seconds”

if( $perCpuStats -and $perCpuStats.Count )
{
[array]$results = @( $perCpuStats.GetEnumerator() | ForEach-Object `
{
if( $_.Key -ne ‘_total’ )
{
[pscustomobject][ordered]@{
‘CPU’ = $_.Key -as [int]
‘Average CPU Utilisation %’ = ( $_.Value | Measure-Object -Average | Select -ExpandProperty Average ).ToString(“0.00”)
##’Minimum CPU Utilisation %’ = [math]::Round( ( $_.Value | Measure-Object -Minimum | Select -ExpandProperty Minimum ) , 2 )
##’Maximum CPU Utilisation %’ = [math]::Round( ( $_.Value | Measure-Object -Maximum | Select -ExpandProperty Maximum ) , 2 )
}
}
})
$results | Sort CPU | Format-Table -AutoSize
}
else
{
Write-Error “Failed to retrieve any CPU performance data”
}

START YOUR TRIAL

Get Your Download Link

Gain access to ControlUp from your PC. Register and get a link to start your Free Trial.