String 将JSON参数传递到Azure DevOps Powershell脚本

String 将JSON参数传递到Azure DevOps Powershell脚本,string,powershell,azure-devops,String,Powershell,Azure Devops,我试图将变量传递到JSON post参数中,但是,下面的代码仅适用于硬编码值,但我需要将$var传递给主体参数: $var="demo" Write-Host $var $postParams = @' { "scope": "DemoScope","principal": "$($var)" } '@ 到目前为止,我在上面的脚本中尝试了$(var)和$($var),但没有任何效果。因为您使用'变量无法使用,我喜欢这样创建json: $postParams = @{ scop

我试图将变量传递到JSON post参数中,但是,下面的代码仅适用于硬编码值,但我需要将
$var
传递给
主体
参数:

$var="demo"

Write-Host $var


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

到目前为止,我在上面的脚本中尝试了
$(var)
$($var)
,但没有任何效果。

因为您使用
'
变量无法使用,我喜欢这样创建json:

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

# Result:
{
    "principal": "demo",
    "scope": "DemoScope"
}
另一种使用
的方法是安装

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