tags:

views:

105

answers:

1

I'm trying to export just the numerical value for two performance counters to CSV. I've been able to get something built together but I still need some assistance.

    $counter1 = "\\ygex01wal\SMTP Server(_total)\Messages Received Total"
    $counter2 = "\\ygex01wal\SMTP Server(_total)\Messages Sent Total"

    $data1 = Get-Counter $counter1
    $data1cooked = $data1.countersamples | Select-Object cookedvalue
    $data2 = Get-Counter $counter2
    $data2cooked = $data2.countersamples | Select-Object cookedvalue

    $object = New-Object PSObject
    add-member -InputObject $object Noteproperty 'Received' $data1cooked
    add-member -InputObject $object Noteproperty 'Sent' $data2cooked

    $object | Export-Csv c:\csv.csv -NoTypeInformation

My csv ends up looking like:

 
   "Received","Sent"
   "@{CookedValue=2469610}","@{CookedValue=307718}"

I would prefer it to look like

 
   "Received","Sent"
   "2469610","307718"

Any suggestions on how I can get there?

+2  A: 

Add the -expandproperty parameter to the select-object cmdlet.

excerpt:

$data1 = Get-Counter $counter1
$data1cooked = $data1.countersamples | Select-Object -expandproperty cookedvalue
$data2 = Get-Counter $counter2
$data2cooked = $data2.countersamples | Select-Object -expandproperty cookedvalue

Here's a blog post about that parameter: http://powershellstation.com/2009/11/11/an-overlooked-parameter/

Mike Shepard