views:

35

answers:

1

Hello,

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 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 :)

A: 

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
...
Keith Hill
That did the trick. thanks!
b34r