NealWalters
NealWalters

Reputation: 18167

Putting a specific Powershell Get-Counter Statistic into a variable

I'd like to pull a specific statistics from a _Total (the CounterSamples) into a variable, in order to further parse it.

This is what I've tried. Trying to index with [1] gives error further below. Looping didn't seem to get me very far either.

cls
$statToCollect = '\Process(_Total)\IO Data Operations/sec'
Get-Counter $statToCollect
Write-Host "================" 
$saveStats = Get-Counter $statToCollect
$ctrSamples = $saveStats[1].CounterSamples
Write-Host "$ctrSamples"
Write-Host "$($saveStats)"
Write-Host "================"

$diskStats = Get-Counter $statToCollect 
$diskStatsLoopCounter = 1 
foreach ($diskStat in $diskStats)
{
    if ($diskStatsLoopCounter -eq 1) 
    {
        write-host "$($diskStat.CounterSamples)" 
    }
    $diskStatsLoopCounter = $diskStatsLoopCounter + 1 
}

Results:

Timestamp                 CounterSamples                                                     
---------                 --------------                                                     
12/29/2014 9:27:49 AM     \\mpcname\process(_total)\io data operations/sec :                
                          970.6265098029                                                     


================
Unable to index into an object of type Microsoft.PowerShell.Commands.GetCounter.PerformanceCo
unterSampleSet.
At C:\Users\neal.walters\Documents\DiskUtil.ps1:6 char:26
+ $ctrSamples = $saveStats[ <<<< 1].CounterSamples
    + CategoryInfo          : InvalidOperation: (1:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex


Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet
================
Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample

Upvotes: 0

Views: 1008

Answers (2)

Don Cruickshank
Don Cruickshank

Reputation: 5928

Using PowerShell version 4 on Windows 8.1:

Get-Counter returns a PerformanceCounterSampleSet and you can access the CounterSamples property to get an array of PerformanceCounterSample objects.

The particular property that you're interested in is CookedValue:

$statToCollect = '\Process(_Total)\IO Data Operations/sec'
$total = (Get-Counter $statToCollect).CounterSamples.CookedValue

This gets you the result as a double:

PS> $total
28.9450419770711

PS> $total.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Double                                   System.ValueType

Upvotes: 1

Matt
Matt

Reputation: 46690

In your particular case $saveStats is only the one element.

PS C:\Users\mcameron> $saveStats.Count
1

Which is why this command would have returned null output as there is not a second.

PS C:\Users\mcameron> $saveStats[1]

Since there was only the one element either of the following options would have worked for this case.

PS C:\Users\mcameron> $saveStats[0]

PS C:\Users\mcameron> $saveStats

Also as for the line Write-Host "$($saveStats)" since $saveStats is an object and not a string it would not expand the way you expect. Pretty sure this occure because the ToString() is not overloaded to handle this so just the object type is outputted. Simply having $saveStats on its own would allow powershell to format it properly using its own built-in cmdlets.

PS C:\Users\mcameron> $saveStats

Timestamp                 CounterSamples                                                                                      
---------                 --------------                                                                                      
12/29/2014 10:56:53 AM    \\c3935\process(_total)\io data operations/sec :                                                    
                          27.7291444862573

Similar issue with the line write-host "$($diskStat.CounterSamples)" which has the same response as above.

As the other commenters and posters have said you most likely want one of the properties like CookedValue which can be easily converted to a string.

write-host "$($diskStat.CounterSamples.CookedValue)"

Upvotes: 2

Related Questions