使用批处理powershell命令将文件中的单引号替换为两个单引号

使用批处理powershell命令将文件中的单引号替换为两个单引号,powershell,single-quotes,Powershell,Single Quotes,我目前正在做一个自动安装程序。它必须执行SQL命令。我决定用一个简单的批处理文件创建它,该文件允许我使用诸如sqlcmd或Powershell之类的命令 我正在处理一个XML文件,它将被插入到另一个文件中。首先,我必须替换其中的一些字符串。我成功地使用了-replace命令,如下所示: powershell -Command "(gc source_file.xml) -replace 'utf-8', 'utf-16' | sc updated_file.xml" 现在,更新的_file.x

我目前正在做一个自动安装程序。它必须执行SQL命令。我决定用一个简单的批处理文件创建它,该文件允许我使用诸如sqlcmd或Powershell之类的命令

我正在处理一个XML文件,它将被插入到另一个文件中。首先,我必须替换其中的一些字符串。我成功地使用了-replace命令,如下所示:

powershell -Command "(gc source_file.xml) -replace 'utf-8', 'utf-16' | sc updated_file.xml"
现在,更新的_file.xml仍然包含单引号,我想用“”(两个单引号)替换。我想再次使用replace命令进行替换。我试图避开单引号字符,但没有成功:

powershell -Command "(gc source_file.xml) -replace "`'", "`'`'" | sc updated_file.xml"
该错误显示未转义单引号。此外,在我的例子中,replace命令似乎只读取单引号之间的字符串。我还尝试使用regex::Escape(“`'),但没有成功

你知道吗

非常感谢

试试这个:

powershell -Command "(gc source_file.xml)  -replace 'utf-8', 'utf-16' -replace '''', '''''' | sc updated_file.xml"
试试这个:

powershell -Command "(gc source_file.xml)  -replace 'utf-8', 'utf-16' -replace '''', '''''' | sc updated_file.xml"

对于cmd.exe,必须用反斜杠转义内部双引号,因此这应该可以:

powershell -NoP -C "(gc source_file.xml) -replace 'utf-8','utf-16' -replace \"'\",\"''\" | Set-Content updated_file.xml" 
提示:在即将发布的PowerShell中,别名sc已被删除,因此请避免使用它。
另外,更改内容文本utf8=>utf-16也不会更改编码(可能会将
-encoding utf8
附加到gc中。

对于cmd.exe,必须用反斜杠转义内部双引号,因此这应该可以:

powershell -NoP -C "(gc source_file.xml) -replace 'utf-8','utf-16' -replace \"'\",\"''\" | Set-Content updated_file.xml" 
提示:在即将发布的PowerShell中,别名sc已被删除,因此请避免使用它。
另外,更改内容文本utf8=>utf-16不会更改编码(可能会将
-encoding utf8
附加到gc。

尝试以下操作:-替换“$([char]39)”,“$([char]39)$([char]39)”帮你自己一个忙——一旦出现像这样复杂的引用问题,你必须弄清楚如何以
cmd
和PowerShell都满意的方式引用内容,切换到显式脚本或使用
-encodeCommand
。在PowerShell中,
-replace“,”
工作正常,无需再转义.powershell-Command”(gc source_file.xml)-替换“utf-8”,“utf-16”-替换“”,“sc updated_file.xml”尝试以下操作:-替换“$([char]39)”,“$([char]39)$([char]39)”帮你自己一个忙——一旦出现像这样复杂的引用问题,你必须弄清楚如何以
cmd
和PowerShell都满意的方式引用内容,切换到显式脚本或使用
-encodeCommand
。在PowerShell中,
-replace“,”
工作正常,无需再转义.powershell命令”(gc source_file.xml)-替换“utf-8”、“utf-16”-替换“sc updated_file.xml”