在-replace中逃脱双引号

时间:2016-10-05 12:59:08

标签: powershell command-line powershell-v2.0

我有一个PowerShell脚本用QA值替换DEV Web配置条目。

到目前为止很好,但现在我有一些带双引号的值:

   (Get-Content $targetFile) |
         Foreach-Object {              
            $_ -replace "<add key="Path" value="/DEV/Reports" />", "<add key="WebReportsPath" value="/QA/Reports" />" `
                -replace "<add key="EmpReport" value="/DEV/Emp/Reports" />", "<add key="EmpReport" value="/QA/Emp/Reports" />" `                                        
                -replace "olddatabase", "newdatabase"

         } |
         Set-Content $targetFile

运行时出现解析器错误。如果我将双引号更改为单引号,如

`-replace 'valwithdoublequote' 'valwithdoublequote' still I get parser error. How to escape this?

1 个答案:

答案 0 :(得分:4)

由于DEFAULT_TIMEOUT_SEC正在使用正则表达式,您应该使用-replace来转义字符(或使用[regex]::Escape代替):

$_.Replace()