转换计数器数据

Transform counter data

我有以下脚本:

$SqlServerCounterPrefix = '\MSSQL$MSSQL_2008'
$Data = get-counter ($SqlServerCounterPrefix + ":Buffer Manager\Buffer cache hit ratio"), 
                    ($SqlServerCounterPrefix + ":Buffer Manager\Page life expectancy"), 
                    ($SqlServerCounterPrefix + ":Access Methods\Page splits/sec")
#$Data
$TransformedData = $Data.CounterSamples | Select-Object -Property Path, CookedValue
$TransformedData | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $Data.Timestamp
$TransformedData | Format-table -auto

结果如下:

Path                                                                 CookedValue Timestamp          
----                                                                 ----------- ---------          
\sw-pa-win10\mssql$mssql_2008:buffer manager\buffer cache hit ratio         100 2016-10-25 10:18:19
\sw-pa-win10\mssql$mssql_2008:buffer manager\page life expectancy           123 2016-10-25 10:18:19
\sw-pa-win10\mssql$mssql_2008:access methods\page splits/sec                  0 2016-10-25 10:18:19

我如何将它变成以下内容:

Timestamp              buffer cache hit ratio    page life expectancy    page splits/sec
---------              ----------------------    --------------------    ---------------
2016-10-25 10:18:19    100                       123                     0

注意:我不擅长 powershell,所以如果您有任何关于改进原始脚本的建议,欢迎评论!

我设法找到了解决方案:

$SqlServerCounterPrefix = '\MSSQL$MSSQL_2008'
$Data = get-counter ($SqlServerCounterPrefix + ":Buffer Manager\Buffer cache hit ratio"), 
                    ($SqlServerCounterPrefix + ":Buffer Manager\Page life expectancy"), 
                    ($SqlServerCounterPrefix + ":Access Methods\Page splits/sec")
$TransformedData = $Data.CounterSamples | Select-Object -Property Path, CookedValue

$object = New-Object psobject

$object | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $Data.Timestamp
foreach ($row in $TransformedData)
{
    $path = $row.Path
    $name = $path.Substring($path.LastIndexOf("\") + 1)
    $object | Add-Member -NotePropertyName $name -NotePropertyValue $row.CookedValue
}
$object