简体   繁体   English

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

[英]Passing JSON Parameters into the Azure DevOps Powershell Script

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: 我正在尝试将变量传递到JSON post参数中,但是,以下代码仅适用于对值进行硬编码,但是我需要将$var传递给principal参数:

$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. 到目前为止,我在上述脚本中尝试使用$(var)$($var) ,但没有任何效果。

Because you use ' ' the variable can't be used, I like this way to create the json: 因为您使用' '变量不能被使用,所以我喜欢这种方式来创建json:

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

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

Another way it to use " instaed of ' : 它使用" instaed of '另一种方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM