开发者

Creating PerformanceCounterCategory in Powershell

开发者 https://www.devze.com 2023-01-14 20:30 出处:网络
I\'m trying to run the following script in powershell: $counters = @() $counters = $counters + [Diagnostics.CounterCreationData]::(\"Hit counter\", \"Number of total hits\", [Diagnostics.Performance

I'm trying to run the following script in 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);

When I execute this, I get an error saying $counterCollection is null. I'm afraid I'm not yet familiar enough with powershell to sort out开发者_如何学Go where this is going wrong - is it the array I'm building the Collection from? Or the CounterCreationDataCollection creation call itself?

Any pointers are more than welcome :)


You're mixing in static accessor syntax :: with the constructor call. Try this instead:

$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
...
0

精彩评论

暂无评论...
验证码 换一张
取 消