Search code examples
powershellpscustomobject

How can I access Powershell PSCustomObject properties?


I'm processing through Powershell script some API result processing. API data (json) come from this :

$tree = Invoke-WebRequest -Uri "xxxxxxxxmonURLxxxxxxxxxx/130333"  
$children = ($tree.Content | ConvertFrom-Json).data.12345.children

Then I loop through $children object using | ForEach $_ within loop has "147852" as $_.Name, and the following object as $_.Definition I'd like to parse the object within $_.Definition but cannot figure out how to access it.

The Definition object looks like this:

  TypeName : System.Management.Automation.PSCustomObject
 
Name   MemberType   Definition
----   ----------   ----------
147852 NoteProperty System.Management.Automation.PSCustomObject 147852=@{nodeType=node; name=test1; flag=N0; creationDate=2022-02-17T14:50:16+00:00; hasAlerts=False; children=}

And I wish to access any property within the 147852 key (such as nodeType, name, flag, ..., children).

$_.147852 outputs an error saying 147852 was not found.

Thanks.

API json returned:

{
    "data": {
        "130333": {
            "nodeType": "node",
            "name": "Test name",
            "flag": "N0",
            "children": {
                "147852": {
                    "nodeType": "node",
                    "name": "test1",
                    "flag": "N0",
                    "hasAlerts": false,
                    "children": {
                        "147853": {
                            "nodeType": "node",
                            "name": "test2",
                            "flag": "N0",
                            "children": {
                                "NP12-N9-S4": {
                                    "nodeType": "agent",
                                    "name": "Win10",
                                    "type": "S"
                                }
                            }
                        }
                }
            }
        }
    }
} 

Solution

  • Jeroen Mostert provided the crucial pointer in the comments, and Bender the Greatest links to what is effectively a duplicate question, but given that the latter is hashtable-focused, let me recapitulate the problem in the context of custom objects ([pscustomobject]):

    Leaving the accidental use of Get-Member out of the picture, your problem ultimately boils down to a parser bug in PowerShell (see GitHub issue #14036):

    To avoid it, quote property names that look like numbers - e.g., to access property 147852 on object $obj, use $obj.'147852'

    Strictly speaking, the bug only surfaces if you attempt an additional (nested) property access:

    # Nested sample custom object.
    $obj = [pscustomobject] @{ 147852 = [pscustomobject] @{ name = 'test1' } }
    
    # OK - number-like property is accessed without quoting, but *not nested*.
    # However, $obj.'147852' is preferable.
    $obj.147852
    
    # *Nested* property access:
    
    # !! BUG triggers error: "Missing property name after reference operator."
    $obj.147852.name
    
    # OK: Quoting avoids the problem.
    $obj.'147852'.name # -> 'test1'