Author Posts

April 22, 2017 at 6:33 pm

I can't seem to figure out how to cast $procs.CPUUtilization as a decimal type.

Thank you in advance

April 22, 2017 at 10:28 pm

A colleague helped me work this out I believe...

$cputhreshold = 0
$computers = "somePC"
$computers |
    ForEach-Object {
    $computer = $_
    Get-Counter "\\$computer\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 |
        Select -Expand CounterSamples |
        Select -Expand CookedValue |
        Measure-Object -Average |
        where { $_.Average -gt $cputhreshold } |
        Select @{n = 'ComputerName'  ; e = {$computer}},
               @{n = 'CPUUtilization'; e = {'{0:N2}' -f $_.Average}}
} | ConvertTo-HTML -fragment

April 22, 2017 at 10:36 pm

You could do it in multiple ways.
I thought that .ToDecimal() should work pretty straight forward but it's looking for an "iformatprovider".

Anyway here are two ways that works for me (just pasting in the last portion of the code).

Where-Object {[convert]::ToDecimal($procs.CPUUtilization) -gt $cputhreshold}

Or

Where-Object {[decimal]"$($procs.CPUUtilization)" -gt $cputhreshold}