Reputation: 5266
I have a PowerShell script to replace DEV web config entries with QA values.
So far good, but now I have some values with double quotes:
(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
I get parser error when I run. If I change double quote to single quote like
`-replace 'valwithdoublequote' 'valwithdoublequote' still I get parser error. How to escape this?
Upvotes: 1
Views: 901
Reputation: 58991
Since -replace
is using regex, you should use [regex]::Escape
to escape your characters (or use $_.Replace()
instead):
(Get-Content $targetFile) |
Foreach-Object {
$_ -replace [regex]::Escape('<add key="Path" value="/DEV/Reports" />'), '<add key="WebReportsPath" value="/QA/Reports" />' `
-replace [regex]::Escape('<add key="EmpReport" value="/DEV/Emp/Reports" />'), '<add key="EmpReport" value="/QA/Emp/Reports" />' `
-replace "olddatabase", "newdatabase"
} |
Set-Content $targetFile
Upvotes: 5