Invoke-RestMethod includes converting the response from JSON, where Invoke-WebRequest does not, so it's a useful cmdlet to use here.
.filters in that site's JSON output is a list of strings. You can put them in a file, one per line, with:
$filters = (Invoke-RestMethod -Uri https://fsrm.experiant.ca/api/v1/combined).filters
$filters | Set-Content "d:\path\$(get-date -Format u).csv"
If it does need a column header line:
$Path = "d:\path\$(get-date -Format u).csv"
$filters = (Invoke-RestMethod -Uri https://fsrm.experiant.ca/api/v1/combined).filters
'Filter' | Set-Content $Path
$filters | Add-Content $Path
And if it does need to go through CSV, which will quote each line and have a header line, then:
$path = "d:\temp"
$ts = (Get-Date -format yyyyMMdd_HHmmss)
$json = Invoke-RestMethod -Uri "https://fsrm.experiant.ca/api/v1/combined"
$json.filters | select @{N="Value";E={$_}} | Export-Csv -NoTypeInformation -Path "$path\$ts.csv"
There's no need to |%{$_.Filters} because there's only one object converted from JSON and filters is a property of it, so access it once:
{
filters: [
]
}
And there's no need to E={$_.ToString()} because the filter values are already strings.
The way Import-CSV and Export-CSV work is:
- A row is an object
- Columns map to properties of objects
- The CSV maps to an array of objects
Select allows you to start with some objects coming in through the pipeline, and send them on down the pipeline with only some properties of your choosing.
Calculated properties allow you to add in new properties on the fly, to the objects coming through the pipeline.
In this case:
$json.filters |
what's coming through is strings.
$json.filters | select @{Name="Value";Expression={$_}}
is taking one string
"*.vbs"
and turning it into a PSCustomObject with one property:
@{Value="*.vbs"}
That now maps to a CSV row, with one column ('Value') and the column content *.vbs.
The pipeline becomes
@{Value="*.vbs"}
@{Value="*.exe"}
@{Value="*.js"}
and Export-CSV processes them into a CSV
Select-Object @{Name="Filters";Expression={$_.Filters -join ';'}}"Filters"\n"*.ext\n*.ext2\n*.ext3"(I replied the-joincharacter with ``n`)$x.filters | set-content whatever.csv