2016-10-05 2 views
0

DEV Web config 항목을 QA 값으로 바꾸는 PowerShell 스크립트가 있습니다.-replace에서 큰 따옴표를 이스케이프 처리하지 마십시오.

지금까지 좋은,하지만 지금은 큰 따옴표로 일부 값이 : 나는 실행할 때

(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 때문에

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

답변

4

처럼 작은 따옴표로 큰 따옴표를 변경하는 경우, 당신은 당신의 문자를 탈출 [regex]::Escape를 사용 (또는 대신 $_.Replace() 사용)해야합니다

(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 
+0

우수함. 감사. 하지만 우리는 QA1에서 QA 4까지의 루프를 가지고 있습니다. 카운트를 대체 할 수 있습니까? '-replace ':: Escape (' '),' ''. 당신이 작은 따옴표를 사용하고 있기 때문에 나는 나쁜 대체 어구 – Billa

+0

을 얻습니다, 당신은 또한'[regex] :: Escape ('), (''-f $ i)' –