[英]PowerShell content replacement and escaping double quotes
Need to replace string name = null
with string name = "Jane Doe";
需要用string name = "Jane Doe";
替换string name = null
string name = "Jane Doe";
trying to use below code - no success. 尝试使用下面的代码-没有成功。
I do not want to save it in parameters and change because of I have to keep doing multiple times in 3rd party application 我不想将其保存在参数中并进行更改,因为我必须在第三方应用程序中多次执行此操作
(Get-Content -Path %testPath%) -replace 'string name = null', 'string name = \`"Jane Doe\`"' |
Set-Content %testPath%
Your whole approach/(original) script line only makes sense to me 您的整个方法/(原始)脚本行仅对我有意义
when invoked out of a batch file/other higher level management. 从批处理文件/其他更高级别的管理中调用时。
Then escaping on multiple levels is always a difficult thing. 然后逃离多个级别始终是一件困难的事情。
As the script line itself is in double quotes, 由于脚本行本身用双引号引起来,
the inner double quotes have to be escaped from the executing cmd.exe with a backslash. 内部双引号必须使用反斜杠从正在执行的cmd.exe中转义。
:: Q:\Test\2019\08\23\SO_57618950.cmd
@Echo off
set "testPath=Q:\Test\2019\08\23\foo.txt"
powershell -NoP -C "(Get-Content -Path '%testPath%') -replace 'string name = null', 'string name = \"Jane Doe\"'|Set-Content -Path '%testPath%"'
type "%testPath%"
Sample run: 样品运行:
> Q:\Test\2019\08\23\SO_57618950.cmd
# This is foo.txt
string name = "Jane Doe"
# end of text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.