我试图获取perfmon计数器并将数据导出到csv文件。
$pg_co = "\Processor(_total)\% processor time"
$pg_Da = Get-Counter $pg_co
$pg_Da.counterSamples | Select-Object TimeStamp, CookedValue, RawValue | Export-CSV "D:\Qemu\test1.csv" -Append -NoTypeInformation
$pg_co = "\Memory\Available MBytes"
$pg_Da = Get-Counter $pg_co
$pg_Da.counterSamples | Select-Object TimeStamp, CookedValue, RawValue | Export-CSV "D:\Qemu\test1.csv" -Append -NoTypeInformation
但是我无法将这些值放在单独的列中。数据被覆盖(如果我包含-Append,则在后续行中)。 当我第二次查询计数器时,如何将其放入新列? 例如,我希望csv看起来像这样,可能使用不同的列名作为CookedValue2或任何东西。 The expect outlook of csv file 任何帮助将不胜感激
答案 0 :(得分:1)
试试这个:
$pg_co1 ="\Processor(_total)\% processor time"
$pg_Da1 = Get-Counter $pg_co1
$pg_co2 ="\Memory\Available MBytes"
$pg_Da2 = Get-Counter $pg_co
$csv = New-Object -TypeName PSObject
Add-Member -InputObject $csv -MemberType NoteProperty -Name "TimeStamp" -Value $pg_Da1.Timestamp
Add-Member -InputObject $csv -MemberType NoteProperty -Name "CookiedValue1" -Value $pg_Da1.CounterSamples[0].CookedValue
Add-Member -InputObject $csv -MemberType NoteProperty -Name "CookiedValue2" -Value $pg_Da2.CounterSamples[0].CookedValue
Add-Member -InputObject $csv -MemberType NoteProperty -Name "RawValue1" -Value $pg_Da1.CounterSamples[0].RawValue
Add-Member -InputObject $csv -MemberType NoteProperty -Name "RawValue2" -Value $pg_Da2.CounterSamples[0].RawValue
$csv | Export-Csv "D:\Qemu\test1.csv" -NoTypeInformation -Append
它使这种csv:
"TimeStamp","CookiedValue1","CookiedValue2","RawValue1","RawValue2"
"27.03.2017 16:33:21","3,91259176894325","0,782341394244668","450438906250","450448828125"