Invoke-RestMethod returns folder objects in $response.data. I want to log the ID of each folder named Management, but my current filter logs every ID in the container. How should I filter the response?
How do I log only the folder ID when the name matches?
Answers
Filter the folder objects before selecting their IDs: $folderIds = $response.data | Where-Object { $_.name -eq 'Management' } | Select-Object -ExpandProperty id
Then loop over $folderIds and call LogWrite for each ID. This also handles multiple folders with that name. You don't need ConvertTo-Json just to log an ID string.
Reply to this answer
The problem is where the filtering happens. $response.data.id pulls out all the IDs first, leaving strings that no longer carry their folder names. In your Where-Object block, $response.data.name checks the whole collection rather than the current folder.
Use $_.name while piping $response.data, then access $_.id on the matching objects. The if statement has a similar issue: a match in the names can make the condition true, after which it logs all the IDs.
Reply to this answer
Check the shape of the response if the filter still comes up empty. Invoke-RestMethod normally converts JSON into PowerShell objects, but the folder array might be under data or it might be the response itself.
Inspect $response.data or use $response | ConvertTo-Json -Depth 10 to see the structure. If the folder objects are at the top level, filter $response instead of $response.data.
Reply to this answer
Add your answer
Answers are reviewed before publication.
d.816+mint:
That works for the next step too, since each matching ID needs a separate action.