-1

Need help from the experts, $ProjectID will return valid but when I reference it in the code it comes back with an error. If I list out the ProjectID manually works fine. Any suggestions would be greatly welcomed.... Thanks in advance.

enter image description here

mklement0
  • 382,024
  • 64
  • 607
  • 775
Nino
  • 1

1 Answers1

1

You're using a single-quoted here-string (@'<newline>...<newline>'@), which, just like a regular single-quoted string, treats its content verbatim.

In order to use string interpolation in (here-)strings - i.e. the ability to embed variable references and expressions and have them replaced with what they evaluate to - use double-quoted (here-)strings, a.k.a expandable (here-)strings.

For an overview of PowerShell's string literals, see the bottom section of this answer.

Applied to your case:

  • Note the double-quoted here-string syntax, @"<newline>...<newline>"@

  • Note that the variable reference, $ProjectId is also enclosed in "..." in order to satisfy the JSON syntax requirements.

$body = @"
[
  { "from": "",
    "op":0,
    "path": "/projectEntitlements",
    "value": {
      "projectref": {
        "id": "$ProjectId",
        "group": { "groupType": 3 }  
      }
    }
  }
]
"@
mklement0
  • 382,024
  • 64
  • 607
  • 775