Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active December 3, 2024 19:34
Show Gist options
  • Save gitfvb/e809ded17baa378a81e9fa0dbafca281 to your computer and use it in GitHub Desktop.
Save gitfvb/e809ded17baa378a81e9fa0dbafca281 to your computer and use it in GitHub Desktop.
Use of Powershell performance indicators with get-counter

Documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.diagnostics/get-counter?view=powershell-5.1

Show all available counters, be careful, this seems to be language dependent

get-counter -listset * | Out-GridView
get-counter -listset "*Netzwerk*" | fl
get-counter -listset "*Netzwerk*" | % { $_.Counter }

Show all available processes

get-process | Out-GridView -PassThru

Get all counters of a teams process every ten seconds

$procCounter = get-counter -listset "Prozess" | % { $_.Counter }
get-counter $procCounter.replace("*", "ms-teams") -Continuous -SampleInterval 10

This is the most advanced example to create a flat table with the process counters every 10 seconds

$processName = "ms-teams"
$removeColumnPrefix = "\\desktop-cjokt0e\prozess(ms-teams)\"
get-counter $procCounter.replace("*", $processName) -Continuous -SampleInterval 10 | % { $o = [ordered]@{ "timestamp"=$_.Timestamp.ToString("yyyy-MM-ddTHH:mm:ssZ") }; $_.CounterSamples | % { $o.add( $_.Path.replace($removeColumnPrefix ,""), $_.CookedValue ) }; [pscustomobject]$o }

If you want to write it directly to a csv file, use it like this one

$processName = "ms-teams"
$removeColumnPrefix = "\\desktop-cjokt0e\prozess(ms-teams)\"
get-counter $procCounter.replace("*", $processName) -Continuous -SampleInterval 10 | % { $o = [ordered]@{ "timestamp"=$_.Timestamp.ToString("yyyy-MM-ddTHH:mm:ssZ") }; $_.CounterSamples | % { $o.add( $_.Path.replace($removeColumnPrefix ,""), $_.CookedValue ) }; [pscustomobject]$o } | Export-Csv -Path ".\teams-log.csv" -Encoding UTF8 -NoTypeInformation -Append

To run this job in the background (e.g. if you are running a remote rdp session), you can put this in a scriptblock and run it separately

$sc = [scriptblock]{
    $processName = "ms-teams"
    $removeColumnPrefix = "\\desktop-cjokt0e\prozess(ms-teams)\"
    $logfile = ".\teams-log.csv"
    get-counter $procCounter.replace("*", $processName) -Continuous -SampleInterval 10 | % {
        $o = [ordered]@{
            "timestamp"=$_.Timestamp.ToString("yyyy-MM-ddTHH:mm:ssZ")
        }
        $_.CounterSamples | % {
            $o.add( $_.Path.replace($removeColumnPrefix ,""), $_.CookedValue )
        }
        [pscustomobject]$o
    } | Export-Csv -Path $logfile -Encoding UTF8 -NoTypeInformation -Append
}
Start-Job -ScriptBlock $sc
@gitfvb
Copy link
Author

gitfvb commented Dec 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment