how to cast to decimal in where clause

Welcome Forums General PowerShell Q&A how to cast to decimal in where clause

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 7 months ago.

  • Author
    Posts
  • #69273

    Participant
    Points: 22
    Rank: Member

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

    Thank you in advance

  • #69276

    Participant
    Points: 22
    Rank: Member

    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

    Participant
    Points: 151
    Helping Hand
    Rank: 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}
    

The topic ‘how to cast to decimal in where clause’ is closed to new replies.