Counters with zero value filter

Welcome Forums General PowerShell Q&A Counters with zero value filter

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

 
Participant
1 year, 3 months ago.

  • Author
    Posts
  • #78249

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 1
    Rank: Member

    Thanks.
    Trying to implement it right now...

The topic ‘Counters with zero value filter’ is closed to new replies.