如何从.bat文件中转义PowerShell双引号 [英] How to escape PowerShell double quotes from a .bat file

查看:377
本文介绍了如何从.bat文件中转义PowerShell双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从Windows 7中的bat文件运行的PowerShell命令,用两个双引号替换文件(temp1.txt)中的所有双引号:

I'm trying to replace all double quotes in a file (temp1.txt) with two double quotes using this PowerShell command run from a bat file in Windows 7:

powershell -Command "(gc c:\temp\temp1.txt) -replace '\"', '\"\"' | Out-File -encoding UTF8 c:\temp\temp2.txt"

我不断收到错误消息:

 'Out-File' is not recognized as an internal or external command.

当我更改将字母"a"替换为字母"b"的命令时,它的工作原理如下:

When I change the command to replace the letter "a" with the letter "b", it works fine like this:

powershell -Command "(gc c:\temp\temp1.txt) -replace 'a', 'b' | Out-File -encoding UTF8 c:\temp\temp2.txt"

我需要对双引号进行转义,因为整个powershell -Command都在双引号字符串内.您如何转义双引号?

I need to escape the double quote since the entire powershell -Command is within a double quoted string. How do you escape the double quote?

推荐答案

Hm,您需要在命令行中的双引号字符串内转义".根据我的测试,似乎唯一起作用的是加引号参数内的四重双引号"""":

Hm, here you need to escape the " on the command line, inside a double quoted string. From my testing, the only thing that seems to work is quadruple double quotes """" inside the quoted parameter:

powershell.exe -command "echo '""""X""""'"

所以您的命令行应该是:

So then your command line should be:

powershell -Command "(gc c:\temp\temp1.txt) -replace '""""', '""""""""' | Out-File -encoding UTF8 c:\temp\temp2.txt"

还有另一种使用PowerShell处理此问题的方法,假设您不想仅将这些命令放在文件中并以这种方式调用:使用-EncodedCommand.这样,您就可以对整个命令或脚本进行base64编码,并在命令行上将其作为单个参数传递.

There is another way to handle this with PowerShell, assuming you don't want to just put these commands in a file and call it that way: use -EncodedCommand. This lets you base64 encode your entire command or script and pass it as a single parameter on the command line.

这是您的原始命令:

(gc c:\temp\temp1.txt) -replace '"', '""' | Out-File -encoding UTF8 c:\temp\temp2.txt

这是一个对其进行编码的脚本:

Here's a script to encode it:

$c = @"
(gc c:\temp\temp1.txt) -replace '"', '""' | Out-File -encoding UTF8 c:\temp\temp2.txt
"@
$b = [System.Text.Encoding]::Unicode.GetBytes($c)
$e = [System.Convert]::ToBase64String($b)

$e现在包含:

KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA =

KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

因此您的新命令行可以是:

So your new command line can be:

powershell.exe -encodedCommand KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

无需担心转义任何事情.

There is no need to worry about escaping anything.

这篇关于如何从.bat文件中转义PowerShell双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆