Cyclemonkey
Cyclemonkey

Reputation: 97

Append multiple rows to a csv in Powershell

I'm trying to create a script that retrieves data by running multiple queries via an API every day and then appends the values to multiple new rows in a CSV file. How do I append all the values to multiple new rows in the CSV file.

#Category 1 Queries

$C1_Range1 = 'API query...'
$C1_Range2 = 'API query...'
$C1_Range3 = 'API query...'

#Category 2 Queries

$C2_Range1 = 'API query...'
$C2_Range2 = 'API query...'
$C2_Range3 = 'API query...'

...

Export-Csv 'C:\path\to\your.csv' -NoType -Append

I'm trying to avoid exporting to CSV after every query. Is it possible to build a table in powershell that can be appended to the CSV in one go?

Here is an example of my desired CSV output

DATE, CATEGORY, RANGE, VALUE        #ColumnName
...
09/10/2019, CAT1, RANGE3, 34567     #Existing Values
09/10/2019, CAT2, RANGE1, 12345
09/10/2019, CAT2, RANGE2, 98776
09/09/2019, CAT2, RANGE3, 45654
10/10/2019, CAT1, RANGE1, 12345 
10/10/2019, CAT1, RANGE2, 23456
10/10/2019, CAT1, RANGE3, 34567
10/10/2019, CAT2, RANGE1, 98765
10/10/2019, CAT2, RANGE2, 87654
10/10/2019, CAT2, RANGE3, 34567     #I want to append all the new queries to the bottom

How would I go about adding new rows to the hashtable before I append them, for example...

'$NewRow = Date + Category + Range + Query 1
Add to bottom of $table

$NewRow = Date + Category + Range + Query 1
Add to bottom of $table

$NewRow = Date + Category + Range + Query 1
Add to bottom of $table'

...
And then I can append to the CSV

Your help will be much appreciated! Thanks you

Upvotes: 1

Views: 1133

Answers (1)

Alex_P
Alex_P

Reputation: 2952

If you can combine queries in a collection you can loop through them and create every entry in the form of a PSCustomObject. This is one example in another question: PSObject array in PowerShell.

$coll = @(
   Range1,
   Range2,
   Range3,
   ...
)

$table = foreach ($item in $coll)
{
   [PSCustomObject]@{
      'Date' = Get-Date
      'Category' = $item.Category
      'Range' = $item.Name
      'Value' = $item.Value
   }
}

The final report you can export to CSV.

Upvotes: 1

Related Questions