xhr489
xhr489

Reputation: 2309

Format object like SQL Table in PowerShell

Say I have collected these counters with Perfmon:

$MemCounters = (Get-Counter -ListSet Memory).Paths
$hello = Get-Counter -Counter $MemCounters -MaxSamples 2

Now I would like to display the result where I both can see the Counter values but also the timestamp for when the counter is from:

$hello.CounterSamples  
$hello.CounterSamples.Timestamp 

I would like column 1 to be $hello.CounterSamples.Timestamp and column 2 to be $hello.CounterSamples so that I later can import the data to SQL.

Upvotes: 0

Views: 38

Answers (1)

Olaf
Olaf

Reputation: 5232

Since there are several counter samples per timestamp you have to extract them with a nested loop ...

$hello |
ForEach-Object {
    $CounterSampleList = $_.CounterSamples
    foreach ($CounterSample in $CounterSampleList) {
        [PSCustomObject]@{
            TimeStamp                = $_.TimeStamp
            CounterSamplePath        = $CounterSample.Path
            CounterSampleCookedValue = $CounterSample.CookedValue
        }
    }
}

Upvotes: 1

Related Questions