PowerShell 7.3 - scenarios When Using Select-Object with Calculated Properties for Nested Objects
I'm relatively new to this, so bear with me. I'm collaborating on a project where I've looked through the documentation and I'm still confused about I'm relatively new to this, so bear with me. I'm trying to format a list of users and their roles from a JSON API response in PowerShell 7.3, but I'm running into issues when using `Select-Object` with calculated properties to extract nested object values. The API response looks like this: ```json [ {"id": 1, "name": "Alice", "roles": [{"role": "Admin"}, {"role": "User"}]}, {"id": 2, "name": "Bob", "roles": [{"role": "User"}]} ] ``` I'm using the following code to parse the JSON and select the role names along with user names: ```powershell $response = Invoke-RestMethod -Uri "https://api.example.com/users" $response | Select-Object -Property name, @{Name='Roles';Expression={ $_.roles | ForEach-Object { $_.role } }} ``` However, I'm getting the following behavior message: ``` Method invocation failed because [System.Object[]] does not contain a method named 'role'. ``` I've tried modifying the calculated property to access the roles in different ways, like using `Select-Object` inside the `ForEach-Object`, but I keep running into issues. It seems like the roles are being treated as an array, but I'm not sure how to properly extract the role names into a single string for each user. How can I fix this? Is there a better approach? This is part of a larger API I'm building. Any help would be greatly appreciated! Any feedback is welcome! The project is a CLI tool built with Powershell. Any help would be greatly appreciated!