在Powershell中创建PerformanceCounterCategory

时间:2010-08-31 20:24:46

标签: powershell performancecounter

我正在尝试在powershell中运行以下脚本:

$counters = @()

$counters = $counters + [Diagnostics.CounterCreationData]::("Hit counter", "Number of total hits", [Diagnostics.PerformanceCounterType]::NumberOfItem32);
$counters = $counters + [Diagnostics.CounterCreationData]::("Hits per second", "Number of average hits per second", [Diagnostics.PerformanceCounterType]::RateOfCountsPerSecond32);

$counterCollection = [Diagnostics.CounterCreationDataCollection]::($counters);

[Diagnostics.PerformanceCounterCategory]::Create("HitCounters","Some help text",[Diagnostics.PerformanceCounterCategoryType]::SingleInstance, $counterCollection);

当我执行此操作时,我收到错误消息,说$ counterCollection为null。 我担心我还不熟悉PowerShell来解决这里出错的问题 - 是我正在构建Collection的数组吗?或者CounterCreationDataCollection创建调用本身?

任何指针都非常受欢迎:)

1 个答案:

答案 0 :(得分:0)

您将静态访问器语法::与构造函数调用混合在一起。试试这个:

$ccdTypeName = 'System.Diagnostics.CounterCreationDate'
$ccdcTypeName = 'System.Diagnostics.CounterCreationDataCollection'
$counters = @()
$counters += new-object $ccdTypeName "Hit counter","..."
$counters += new-object $ccdTypeName "Hits per sec","..."
$counterCollection = new-object $ccdcTypeName $counters
...