Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Accessing object property named with dollar sign in PowerShell

I have a JSON (array of objects) obtained via Invoke-RestMethod and I try to iterate trough them using foreach.
The property I’m interested to use as a filter is named "$value" and I cannot change this, being a COTS application.

foreach($item in $Result.value) {
        if ($item.properties.threadType) {
            Write-Host $item.properties.threadType
            if ($item.properties.threadType.$value -eq "1234567") {
                Write-Host $item.id
            }
        }
    }

Output of Write-Host $item.properties.threadType, if statements evaluates as false.

@{$type=System.String; $value=18792098}
@{$type=System.String; $value=N/A}
@{$type=System.String; $value=1234567}

Snippet of that JSON:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    ...
    "properties": {
        "threadType": {
            "$type": "System.String",
            "$value": "1234567"
        }
    },
    ...

How can I access $value property?
I’ve tried using

  1. $item.properties.threadType.$value
  2. $item.properties.threadType.value
  3. even $item.properties.threadType['$value']

>Solution :

You can use quotation marks to qualify a member name – in this case you’ll want to use single-quotes to avoid expansion of $value as a variable expression:

if ($item.properties.threadType.'$value' -eq "1234567") { <# ... #> }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading