I'm accessing some JSON data using Invoke-RestMethod and I want to iterate through it, performing another Invoke-RestMethod per id and using the apiHost value for each call. A simplified example of the data is below:
"items": [
{
"id": "1234",
"apiHost": "thishost.com"
},
{
"id": "5678",
"apiHost": "anotherhost.com"
},
Here is the function as i have it so far. The Write-Host line (for troubleshooting this) displays ALL the apiHost values per id and explains why the next lines don't work. How do I refer to the relevant apiHost per iteration through the id so that the next lines will work?
function Get-ID {$headers = @{
'Authorization' = $auth_string
'Org' = $organization
}
$response = Invoke-RestMethod -Method Get -Headers $headers -Uri $a_url
foreach($id in $response.items.id) {
$itemheaders = @{
'Authorization' = $auth_string
'Dept-ID' = $id
}
write-host $response.items.apiHost
$apihost = $response.items.apiHost + '/here/there'
$outfile = 'c:\temp\' + $id + '_details.json'
$response = Invoke-RestMethod -Method Get -Headers $itemheaders -Uri $apihost -outfile $outfile
}
}
This post seems to suggest what I'm doing is correct, but I think I'm missing something obvious.