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.
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" }
"@
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.