Performance Counter API call error in Powershell

Mark Jenster picture Mark Jenster · Oct 22, 2012 · Viewed 8.6k times · Source

I want to get specific informations about performance from Perfmon via Powershell and write it to a .csv file, using a script ran by another service. I know Powershell can give some interesting error message, thus I want to hear if any of you could discipher what exactly I'm doing wrong.

The code I'm trying to make work seems like this:

$date = (Get-Date).ToShortDateString()
Get-Counter
    $gc = @("\Memory\% Committed Bytes In Use",
            "\Memory\Available MBytes",
            "\Network Interface(*)\Current Bandwith",
            "\Network Interface(*)\Packets Recieved/sec",
            "\Network Interface(*)\Packets Sent/sec",
            "PhysicalDisk(_Total)\Disk Write Bytes/sec",
            "PhysicalDisk(_Total)\Disk Read Bytes/sec",
            "Processor(_Total)\% Processor Time",
            "Processor(_Total)\% Idle Time")
Get-Counter -counter $gc -SampleInterval 2 -MaxSamples 8 | export-counter -path C:\PerfMon2-$date.csv -FileFormat csv

with an error like this:

Get-Counter : Internal performance counter API call failed. Error: c0000bb9.
At C:\Users\jenstmar\Desktop\Nuværende Projekter\Perfmon.ps1:13 char:12
+ Get-Counter <<<<  -counter $gc -SampleInterval 2 -MaxSamples 1 | export-counter -path C:\PerfMon2-$date.csv -FileFormat cs
v
    + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
    + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

Yet, if I choose performance counter categories instead of specifics, it works. An example would be this:

$date = (Get-Date).ToShortDateString()
    Get-counter 
        $gc = (Get-Counter -listset "Network Interface").paths 
        $gc += (Get-Counter -listset "Processor").paths 
        $gc += (Get-Counter -ListSet "Processor Information").paths
        $gc += (Get-Counter -listSet "memory").paths
        $gc += (Get-Counter -listSet "PhysicalDisk").paths
    get-counter -counter $gc -SampleInterval 2 -MaxSamples 8 | export-counter -path C:\PerfMon-$date.csv -FileFormat csv

Answer

Andrey Marchuk picture Andrey Marchuk · Oct 22, 2012

Here's the code that works:

Get-Counter
$gc =   '\Memory\% Committed Bytes In Use',
        '\Memory\Available MBytes',
        '\Network Interface(`*)\Current Bandwith',
        '\Network Interface(`*)\Packets Recieved/sec',
        '\Network Interface(`*)\Packets Sent/sec',
        "\PhysicalDisk(_Total)\Disk Write Bytes/sec"
  "\PhysicalDisk(_Total)\Disk Read Bytes/sec",
        "\Processor(_Total)\% Processor Time",
        "\Processor(_Total)\% Idle Time"
Get-Counter -counter $gc -SampleInterval 2 -MaxSamples 8

I had to escape * symbol