双倍双引号中的Powershell变量

时间:2020-04-17 17:47:40

标签: powershell escaping

我有一个PS脚本,该脚本应生成带有添加内容$ datei的html文件 工作正常,但这是问题所在, 我有这样的东西:

"some html...
<textarea id="Text$counter" style="width: 100%; height: 100%;"></textarea>
some html...
<span>$NameEdit</span>
some html...
document.getElementById('Text$counter').select();
some html...
" | add-content $datei

所以我在html部分中有多个double和single qoutes以及multible变量,但是我无法找出如何在脚本按预期运行的方式中转义所有内容并在html代码中填充变量

我使用PS 7.0

1 个答案:

答案 0 :(得分:1)

您可能想要的解决方案是此处字符串 但是您也可以使用转义字符`

@"
some html...
<textarea id="Text$counter" style="width: 100%; height: 100%;"></textarea>
some html...
<span>$NameEdit</span>
some html...
document.getElementById('Text$counter').select();
some html...
"@ | add-content $datei

要使用此处字符串,您需要先输入@"@',然后在新行中放置字符串。然后,要关闭此处字符串,您将需要使用新行和"@'@"@'@必须在新行的第一行。

有些例子可能会失败

#This will NOT work
@"
Hey There "Buddy"
  "@

#This will NOT work
@"Hey There "Buddy""@

#This will NOT work
@"
Hey There "Buddy""@

这是一对正确工作的例子

#This WILL work
@"
Hey There "Buddy"
"@

#This WILL work
  @"
Hey There "Buddy"
"@

#This WILL work
$Test = @"
Hey There "Buddy"
"@

这确实使美化代码更加困难 例子

Function Test(){
    $Text1 = "Hello There"
    $Text2 = @"
"Buddy"
"@
    return "$Text1 $Text2"
}

现在让我们越过转义字符` 它称为 Backtick

"Hey There `"Buddy`""

等于嘿,“哥们”