双引号中的 Powershell 变量

Powershell Variable in Double Double Quotes

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

"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 部分有多重双引号和单引号以及多重变量,但我无法找到如何正确地转义脚本按预期运行的所有内容并填写 html代码

我使用 PS 7.0

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

@"
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

要使用 Here 字符串,您需要有 @"@',然后换行放置您的字符串。然后关闭 Here string 你需要换行和 "@'@"@'@ 必须在新行的第一个开头。

这里有一些可能会失败的例子

#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`""

等于嘿嘿"Buddy"