如何逃避一个bat文件PowerShell的双引号? [英] How to escape powershell double quotes from a bat file?

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

问题描述

我想在Windows 7中使用此PowerShell命令运行两个双引号从一个bat文件来替换在一个文件中(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"

我不断收到错误:

I keep getting the error:

 '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, 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来处理这个问题,假设你不希望只是把这些命令的文件,并调用它的方法:使用 -En codedCommand 。这可让您采用base64 EN code整个命令或脚本,并把它作为在命令行上一个参数。

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.

因此​​,这里是你原来的命令:

So here's your original command:

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

下面是一个脚本来连接code是:

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)

现在包含:

KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

所以,你的新的命令行可以是:

So your new command line can be:

powershell.exe -encodedCommand KABnAGMAIABjADoAXAB0AGUAbQBwAFwAdABlAG0AcAAxAC4AdAB4AHQAKQAgAC0AcgBlAHAAbABhAGMAZQAgACcAIgAnACwAIAAnACIAIgAnACAAfAAgAE8AdQB0AC0ARgBpAGwAZQAgAC0AZQBuAGMAbwBkAGkAbgBnACAAVQBUAEYAOAAgAGMAOgBcAHQAZQBtAHAAXAB0AGUAbQBwADIALgB0AHgAdAA=

没有必要担心什么逃逸

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

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