我正在尝试在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创建调用本身?
任何指针都非常受欢迎:)
答案 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
...