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

$procCounter = get-counter -listset "Prozess" | % { $_.Counter }
$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

$procCounter = get-counter -listset "Prozess" | % { $_.Counter }
$processName = "ms-teams"
$counter = $procCounter.replace("*", $processName)
$removeColumnPrefix = "\\desktop-cjokt0e\prozess(ms-teams)\" # this could also be done more dynamically
$logfile = "C:\Users\Florian\teams-log.csv" # better use an absolute path as the job is not starting at your users home directory

# The script loop the fill the logfile
$sc = [scriptblock]{
    param ($counter, $removeColumnPrefix, $logfile)
    Get-Counter $counter -Continuous -SampleInterval 10 | ForEach-Object {

        $o = [ordered]@{
            "timestamp"=$_.Timestamp.ToString("yyyy-MM-ddTHH:mm:ssZ")
        }

        $_.CounterSamples | ForEach-Object {
            $o.add( $_.Path.replace($removeColumnPrefix ,""), $_.CookedValue )
        }

        [pscustomobject]$o

    } | Export-Csv -Path $logfile -Encoding UTF8 -NoTypeInformation -Append
}

# Start a new background job
Start-Job -ScriptBlock $sc -ArgumentList $counter, $removeColumnPrefix, $logfile

# View the current status of jobs
Get-Job

# Stop the job with a specific id like
Stop-Job -Id 5
@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