我目前使用以下 Powershell 腳本作為 API 呼叫REST,用于從供應商處檢索表資料,其JSON格式如下:
$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
該腳本有效,并在 Powershell 中輸出:
data
----
{@{type=appointments; id=1234; attributes=; links=; relationships=}, @{type=appointments; id=1235; attributes=; links=; relationships=}, @{type=appointments; i...
我需要能夠將其匯出為 CSV 檔案。如何合并export-CSVCMDLET(如下所示)以使其與上述語法一起使用?(最好是所有列/標題)
Select ID, Status | Export-Csv -Path "filename.csv" -NoTypeInformation
uj5u.com熱心網友回復:
您Invoke-RestMethod輸出一個具有data屬性的物件。該data屬性包含您要參考的物件。因此,您必須首先擴展該屬性。
Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -Body $body |
Select-Object -ExpandProperty data |
Select-Object id,status |
Export-Csv filename.csv -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/359677.html
