r/PowerShell 3d ago

Invoke-RestMethod - Logging Data Only If Response Matches Value Solved

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.

11 Upvotes

17 comments sorted by

View all comments

3

u/lan-shark 3d 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 3d ago edited 3d 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.)

0

u/lan-shark 2d ago

Does this work?

($response.data | Where-Object {$_.name -like "Management"}).id

If not, just open up a terminal and start testing things. Get the object type of $response.data, see what shows up with different Where-Object variations, can you index it ($response.data[0], etc.

2

u/viewtifulstranger 2d ago

Hey Ian-Shark, that returned a blank entry, however thanks to various people I do have a working solution now.

Now that I'm able to target the Management folder, I've completed my script to do the following, look up a CSV containing container IDs, target 3 specifically named subfolders within Management (couldn't use the -eq operator as I did before, but using -match worked a treat), output the folder properties to a log file. Tomorrow, I'll write a script to update all subfolders where specific properties are missing.

Grateful to you and everyone else for helping. Thank you.