views:

141

answers:

1

I have written a query in Powershell interrogating a F5 BIG-IP box through it's iControl API to bring back CPU usage etc.

Using this code (see below) I can return the data back into a CSV format which is fine. However the $csvdata variable contains all the data. I need to be able to take this variable and for each line split each column of data into a seperate variable.

The output currently looks like this: timestamp,"Utilization" 1276181160,2.3282800000e+00

Any advice would be most welcome

$SystemStats = (Get-F5.iControl).SystemStatistics 

### Allocate a new Query Object and add the inputs needed
$Query = New-Object -TypeName iControl.SystemStatisticsPerformanceStatisticQuery
$Query.object_name = $i
$Query.start_time = $startTime
$Query.end_time = 0
$Query.interval = $interval
$Query.maximum_rows = 0 

### Make method call passing in an array of size one with the specified query
$ReportData = $SystemStats.get_performance_graph_csv_statistics( (,$Query) )


### Allocate a new encoder and turn the byte array into a string
$ASCII = New-Object -TypeName System.Text.ASCIIEncoding
$csvdata = $ASCII.GetString($ReportData[0].statistic_data)
+1  A: 

Use the ConvertFrom-Csv cmdlet. If the CSV data has a header then:

$csvdata = $ASCII.GetString($ReportData[0].statistic_data) | ConvertFrom-Csv

If there aren't headers in the original text, you can use the Header parameter on ConvertFrom-Csv to specify them e.g.:

$csvdata = ... | ConvertFrom-Csv -Header Timestamp,Cat,Number,NotherNumber

Now $csvdata will be objects each with the properties Timestamp, Cat, Number and NotherNumber. Output each to a line should be simple:

$csvdata | Foreach {$_.Timestamp; $_.Cat; $_.Number; $_.NotherNumber}
Keith Hill
That doesn't work as i am not importing an external csv file but just piping the data direct from the $reportdata variable into the $csvdata variable to turn it from a byte array into csv format
Andrew
@Andrew: Use `ConvertFrom-Csv` in that case. I guess that's what Keith wanted to write, anyway :-)
Joey
Sorry. I don't use CSV much personally so yeah, ConvertFrom-Csv is the proper cmdlet. I've updated the answer to reflect that.
Keith Hill
thanks, worked a treat
Andrew