1

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
2
  • 3
    Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body | select -expand data | select id,status | export-csv filename.csv -notype Commented Nov 11, 2021 at 19:12
  • Thanks ! Seems to work! If you include this as an answer, I'll mark it as solved! Commented Nov 12, 2021 at 3:46

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.