Powershell中的性能计数器API调用错误

时间:2012-10-22 07:08:31

标签: powershell counter performance-monitor

我希望通过Powershell从Perfmon获取有关性能的特定信息,并使用另一个服务运行的脚本将其写入.csv文件。我知道Powershell可以提供一些有趣的错误信息,因此我想听听你们是否有人能够训练我到底做错了什么。

我正在尝试制作的代码如下:

$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

出现如下错误:

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

然而,如果我选择性能计数器类别而不是细节,它就有效。一个例子就是:

$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

1 个答案:

答案 0 :(得分:2)

以下是有效的代码:

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

我不得不逃避*符号