how to cast to decimal in where clause

This topic contains 2 replies, has 2 voices, and was last updated by  Fredrik Kacsmarck 1 year, 1 month ago.

  • Author
  • #69273

    Kevin Blumenfeld

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

    Thank you in advance

  • #69276

    Kevin Blumenfeld

    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
  • #69279

    Fredrik Kacsmarck

    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}


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

You must be logged in to reply to this topic.