0

I'm trying to fetch the perfmon counters and exporting the data to csv file.

$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

But I'm unable to put these values in individual columns. The data gets overwritten(or in subsequent rows if I include -Append). When I query the counter for the second time, how can I put it in a new column? For example, I would want the csv to look like this, maybe with a different column name as CookedValue2 or anything. The expect outlook of csv file Any help would be greatly appreciated

San852
  • 19
  • 8
  • This looks like it would work just fine. Did the csv file already contain information before this code was run? Or do you mean you want to transpose the data so rows appear as columns? I am not sure what you are seeing and how it is wrong. Some examples would be useful here. Because if you want to transpose it that is not a CSV anymore – Matt Mar 27 '17 at 10:42
  • For those who want to help you but aren't familiar with perfmon (e.g. linux users) it would be gret if you'd include data examples. – Clijsters Mar 27 '17 at 10:45
  • I just included a picture, of what I am expecting, https://i.stack.imgur.com/MElpx.png. – San852 Mar 27 '17 at 10:53
  • @Matt I don't want to transpose it completely, the first column is fine, however would it be possible to just add the next counter values in the next column, as shown in the pic? – San852 Mar 27 '17 at 10:56
  • @Clijsters I have included a pic, if that would be of any help. – San852 Mar 27 '17 at 11:00

1 Answers1

1

Try this:

$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

it make this kind of csv:

"TimeStamp","CookiedValue1","CookiedValue2","RawValue1","RawValue2"
"27.03.2017 16:33:21","3,91259176894325","0,782341394244668","450438906250","450448828125"
Mihail Kuznesov
  • 555
  • 2
  • 13