从 powershell 脚本格式化 csv 中的输出数据
Formatting the output data in csv from powershell script
我正在尝试获取 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,则在后续行中)。
第二次查询counter时,如何放到新的column中?
例如,我希望 csv 看起来像这样,可能具有与 CookedValue2 或任何其他名称不同的列名称。
The expect outlook of csv file
任何帮助将不胜感激
试试这个:
$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"
我正在尝试获取 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,则在后续行中)。 第二次查询counter时,如何放到新的column中? 例如,我希望 csv 看起来像这样,可能具有与 CookedValue2 或任何其他名称不同的列名称。 The expect outlook of csv file 任何帮助将不胜感激
试试这个:
$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"