Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 多行字符串中未替换字符串变量_String_Powershell_Concatenation_Multiline_Substitution - Fatal编程技术网

String 多行字符串中未替换字符串变量

String 多行字符串中未替换字符串变量,string,powershell,concatenation,multiline,substitution,String,Powershell,Concatenation,Multiline,Substitution,以下是代码/输出一体化: PS C:\code\misc> cat .\test.ps1; echo ----; .\test.ps1 $user="arun" $password="1234*" $jsonStr = @" { "proxy": "http://$user:$password@org.proxy.com:80", "https-proxy": "http://$user:$password@org.proxy.com:80" } "@ de

以下是代码/输出一体化:

PS C:\code\misc> cat .\test.ps1; echo ----; .\test.ps1
$user="arun"
$password="1234*"
$jsonStr = @"
{
        "proxy": "http://$user:$password@org.proxy.com:80",
        "https-proxy": "http://$user:$password@org.proxy.com:80"
}
"@
del out.txt
echo $jsonStr >> out.txt
cat out.txt
----
{
        "proxy": "http://1234*@org.proxy.com:80",
        "https-proxy": "http://1234*@org.proxy.com:80"
}
字符串变量
$user
的内容在
$jsonStr
中未被替换

替换它的正确方法是什么?

冒号是字符:
$scope:$variable
。PowerShell认为您正在从作用域
$user
调用变量
$password
。您可能可以使用一个子表达式

$user="arun"
$password="1234*"
@"
{
        "proxy": "http://$($user):$($password)@org.proxy.com:80",
        "https-proxy": "http://$($user):$($password)@org.proxy.com:80"
}
"@
或者可以使用格式运算符

$user="arun"
$password="1234*"
@"
{{
        "proxy": "http://{0}:{1}@org.proxy.com:80",
        "https-proxy": "http://{0}:{1}@org.proxy.com:80"
}}
"@ -f $user, $password
只要确保在使用format操作符时避开大括号即可

你也可以用反勾号从结肠中逃脱

$jsonStr = @"
{
        "proxy": "http://$user`:$password@org.proxy.com:80",
        "https-proxy": "http://$user`:$password@org.proxy.com:80"
}
"@