Home > Back-end >  Format object like SQL Table in PowerShell
Format object like SQL Table in PowerShell

Time:01-31

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.

CodePudding user response:

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
        }
    }
}
  • Related