how to cast to decimal in where clause

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

  • Author
    Posts
  • #69273
    Profile photo of Adminicrater
    Adminicrater
    Participant

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

    Thank you in advance

  • #69276
    Profile photo of Adminicrater
    Adminicrater
    Participant

    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
    Profile photo of Fredrik Kacsmarck
    Fredrik Kacsmarck
    Participant

    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}
    

You must be logged in to reply to this topic.