0

I'm trying to pass my variable into my JSON post parameters, however, the below code only works for hardcoding the values, but I need to pass the $var to the principal parameter:

$var="demo"

Write-Host $var


$postParams = @'
{   "scope": "DemoScope","principal": "$($var)" }
'@

So far I tried like $(var) and $($var) in the above script but nothing worked.

1 Answer 1

1

Because you use ' ' the variable can't be used, I like this way to create the json:

$postParams = @{
    scope = "DemoScope"
    principal = $var
} | ConvertTo-Json

# Result:
{
    "principal": "demo",
    "scope": "DemoScope"
}

Another way it to use " instaed of ':

$postParams = @"
{    "scope": "DemoScope","principal": "$var" }
"@
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.