I'm currently use the following Powershell script as an API call using REST
to retrieve table data from a vendor, which appears to be in JSON
format:
$uri = "https://www.SomeWebsite.com/api/orders"
$headers = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer <MyTokenID>'
'Accept'= 'application/json'
}
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body
The script works, and output as such in Powershell:
data
----
{@{type=appointments; id=1234; attributes=; links=; relationships=}, @{type=appointments; id=1235; attributes=; links=; relationships=}, @{type=appointments; i...
I need the ability to export this as a CSV file.
How can I incorporate anexport-CSV
CMDLET (something like below) to have it work with the above syntax? (Preferably ALL Columns/Headers)
Select ID, Status | Export-Csv -Path "filename.csv" -NoTypeInformation
CodePudding user response:
Your Invoke-RestMethod
outputs an object with a data
property. The data
property contains the objects you want to reference. So you must expand that property first.
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body |
Select-Object -ExpandProperty data |
Select-Object id,status |
Export-Csv filename.csv -NoTypeInformation