Counters with zero value filter

This topic contains 3 replies, has 2 voices, and was last updated by  Pavel 3 months, 2 weeks ago.

  • Author
    Posts
  • #78249

    Pavel
    Participant

    Hi,
    I am still dealing with counters.
    Just found out that to run for long time with counters saved locally on could be space consuming indeed.
    Trying to filter from saving information about counters with zero value.
    I though about following:

    (Get-counter -Counter ((get-counter -listset Processor).Paths  | Where-Object {
                                                        $_ -like "*" 
                                                        } ) -SampleInterval 2 -MaxSamples 1) 
                                                        | Where-Object {$_.CounterSamples.CookedValue -gt 0}

    For some reason it is not working and I am still getting those with zero value.
    It is strange, because when I am running same test on counters values only:

    (Get-counter -Counter ((get-counter -listset Processor).Paths  | Where-Object {
                                                        $_ -like "*" 
                                                        } ) -SampleInterval 2 -MaxSamples 1).CounterSamples.CookedValue 
                                                        | Where-Object {$_ -gt 0}

    It is working as I intended.

    What I did wrong here?

    Thanks.

  • #78256

    Liam Fisher
    Participant

    Hi Pavel,

    Get-Counter only seems to return one object, there is no way to filter this with a Where-Object (that I know of, or can think of at this time). Here's some code that will generate a list of results using the filter:

    $Paths = (get-counter -listset Processor).Paths
    $Results = @()
    Get-counter -Counter $Paths -SampleInterval 2 -MaxSamples 1 | ForEach-Object {
        $Results += [pscustomobject]@{
            Timestamp = $_.Timestamp
            CounterSamples = $_.CounterSamples | Where-Object {
                $_.CookedValue -gt 0
            }
        }
    }
    
  • #78259

    Liam Fisher
    Participant

    Alternatively, if you don't care about the Timestamp you can achieve it with the following:

    $Paths = (get-counter -listset Processor).Paths
    (Get-Counter -Counter $Paths -SampleInterval 2 -MaxSamples 1).CounterSamples | Where-Object {
        $_.CookedValue -gt 0
    }
    
  • #78262

    Pavel
    Participant

    Thanks.
    Trying to implement it right now...

You must be logged in to reply to this topic.