在批处理文件中调用Powershell脚本时,如何使用嵌套引号?

在批处理文件中调用Powershell脚本时,如何使用嵌套引号?

问题描述:

我有一个DOS批处理文件,该文件具有执行powershell脚本的行.首先,我在批处理文件中尝试了一个非常简单的脚本与此行:

I have a DOS batch file that has a line that executes a powershell script. First I tried a very simple script with this line in the batch file:

powershell -command "get-date" < nul

效果很好.但是该脚本具有嵌套的双引号字符,有时可以使用反引号(`)字符对其进行转义.所以我尝试了这个:

That worked great. But the script has nested double-quote characters, which can sometimes be escaped with a backtick (`) character. So then I tried this:

powershell -command "Write-Host `"hello world`"" < nul

那也很棒.但是,我需要运行的脚本非常复杂,并且具有多个嵌套的双引号字符.我已经将复杂的脚本简化为一个具有相同原理的示例:

That also worked great. However, the script I need to run is pretty complicated and has more than one level of nested double-quote characters. I have taken the complicated script and simplified it to an example that has the same principles here:

[string]$Source =  "    `"hello world`" ";
Write-Host $Source;

如果我将此脚本保存在PS脚本文件中并运行,它可以正常工作,打印出包括双引号的"hello world",但我需要将其嵌入批处理文件的行中.因此,我将脚本全部放在一行中,然后尝试将其插入批处理文件行中,但是它不起作用.我尝试转义双引号,但仍然无法正常工作,就像这样:

If I save this script inside a PS script file and run it, it works fine, printing out "hello world" including the double quotes, but I need to embed it in the line in the batch file. So I take the script and put it all on one line, and try to insert it into the batch file line, but it doesn’t work. I try to escape the double-quotes, but it still doesn’t work, like this:

powershell -command "[string]$Source =  `"    `"hello world`" `";Write-Host $Source;" < nul

有什么方法可以做我想要的吗?您可能会问为什么我要这样做,但这是一个漫长的故事,所以我将不赘述. 谢谢

Is there a way to do what I want? You might ask why I am doing this, but it’s a long story, so I won’t go into the details. thanks

您必须结合使用批处理的转义字符和PowerShell的转义字符.

You'll have to use a combination of batch's escape character and PowerShell's escape character.

在批处理中,转义引号时,可以使用通用的外壳反斜杠(\)来转义那些引号.在Powershell中,使用反引号`.

In batch, when escaping quotes, you use the common shell backslash (\) to escape those quotes. In Powershell, you use the backtick `.

因此,如果您想使用批处理在Powershell中打印出带引号的字符串,则需要先批处理转义引号,以在Powershell中声明变量,然后确保字符串被引用,您需要批处理,而Powershell转义另一个引号,然后添加所需的字符串,以确保先批量转义.

So if you wanted to use batch to print out a quoted string with Powershell, you need to first batch escape the quote to declare the variable in Powershell, then to ensure the string is quoted you need batch and Powershell escape another quote, and then your add your desired string, ensuring you batch escape first.

以您的示例为例,

powershell -command "[string]$Source =  \"`\"hello world`\"\"; Write-Host $Source;"

这里是$ Source变量声明的细分:

Here's a break down of the declaration of the $Source variable:

"[string]$Source = # open quote to begin -command parameter declaration
\"          # batch escape to begin the string portion
`\"         # Powershell+Batch escape
hello world # Your content
`\"         # Posh+Batch again
\";         # Close out the batch and continue
more commands " # Close quote on -command parameter 

这将批量渲染这样的字符串:

This renders the string like this in batch:

`"hello world`"

请注意,您无需将$Source显式转换为字符串,因为您是从头开始将其构建为文字字符串.

One note, you don't need to explicitly cast $Source as a string since you are building it as a literal string from scratch.

$Source = "string stuff"将按预期工作.