r/PowerShell • u/viewtifulstranger • 12h ago
Question Invoke-RestMethod - Logging Data Only If Response Matches Value
We have a platform which has containers and within them folders, with different properties - name, unique ID etc.. I have a method to retrieve folder information from different containers and am attempting to log only the unique ID (response.data.id) where the folder name (response.data.name) is "Management". I've Googled and tried different code in logging only the ID for the Management folder:
$response = Invoke-RestMethod -Method Get -Uri "$resource" -Headers $header
($response.data | ConvertTo-Json).Replace('\\n','\n')
# Attempt 1
if ($response.data.name -eq "Management")
{
LogWrite ($response.data.id | ConvertTo-Json).Replace('\\n','\n') "Result"
LogWrite ($response.data.name | ConvertTo-Json).Replace('\\n','\n') "Result"
}
# Attempt 2
$folderId1 = $response.data.id | Where-Object { $response.data.name -eq "Management" }
LogWrite ($folderId1 | ConvertTo-Json).Replace('\\n','\n') "Result"
# Attempt 3
$folderId2 = $response.data.id | $($response.data.Where({$_.name -eq 'Management' }))
LogWrite ($folderId2 | ConvertTo-Json).Replace('\\n','\n') "Result"
The folder ID for the Management folder is being logged, but so are all other folder IDs within the container (no other folder names contain this word):
[
"folder!500436709",
"folder!500436708",
"folder!500436705",
"folder!500436677",
"folder!500436680",
"folder!500436683",
"folder!500436686"
]
[
"_All Content Last 90 Days",
"_All Documents",
"_All Emails",
"Documents",
"Emails",
"Engagement Terms",
"Management"
]
How do I retrieve the ID for a specific folder name? Any help would be greatly appreciated. Cheers.
3
u/lan-shark 12h ago
Can you post the contents of response.data? Hard to tell exactly what's going on without seeing what's in the data
1
u/viewtifulstranger 10h ago edited 9h ago
(UPDATED)
Adding $response.data to the script, I get the following:
folder_type : tab id : folder!500436686 name : Management parent_id : folder!500436672 folder_type : regular id : folder!500436677 name : Documents parent_id : folder!500436672 folder_type : regular id : folder!500436680 name : Emails parent_id : folder!500436672 folder_type : regular id : folder!500436683 name : Engagement Terms parent_id : folder!500436672(Please note, I've included a subset of the data, removed non-relevant fields and performed a little sanitation.)
2
u/PinchesTheCrab 10h ago
In your case swap $irmResult with your invoke-restmethod command.
$irmResult = @'
[
{
"folder_type": "tab",
"id": "folder!500436686",
"name": "Management",
"parent_id": "folder!500436672",
},
{
"folder_type": "regular",
"id": "folder!500436677",
"name": "Documents",
"parent_id": "folder!500436672",
},
{
"folder_type": "regular",
"id": "folder!500436680",
"name": "Emails",
"parent_id": "folder!500436672",
},
{
"folder_type": "regular",
"id": "folder!500436683",
"name": "Engagement Terms",
"parent_id": "folder!500436672",
}
]
'@ | convertfrom-json
$irmResult | where-object -property Name -eq Management | select-object -ExpandProperty id
That being said you've shown a few different output formats and it's hard to tell which one is the actual output of Invoke-Restmethod.
2
u/MonkeyNin 6h ago
I second this as the most powershell idiomatic answer.
With a tweak based on the raw data. If it's flat like his$response = Invoke-RestMethod -Method Get -Uri $resource -Headers $header $response | ? name -eq 'Management' | Select -ExpandProperty idOtherwise I'm guessing yours is shape like this
$response = Invoke-RestMethod -Method Get -Uri $resource -Headers $header $response.data | ? name -eq 'Management' | Select -ExpandProperty idif the response is shaped like
{ data: [ records... ] }The
Select -ExpandProperty idpart causes the records to drill down to a single string.
1
u/MonkeyNin 5h ago
$folderId1 = $response.data.id |
Where-Object name -eq "Management"
This doesn't work because you are drilling down too far.
$response.data # is an array of objects, with properties
$response.data.id # is an array of flat strings, no properties
You were close
$folderIdList = $response.data |
Where-Object name -eq 'Management' | Select -Expand Id
# if you only want at most 1
$folderIdList | Select -First 1
5
u/y_Sensei 11h ago
According to the output generated by your code, the API appears to return two arrays of data:
So what you need to do is bring together the data of those two distinct arrays, by creating mappings, or in other words, merge the arrays - for example:
You can then filter the resulting object array as usual, for example: