2016-04-26 25 views
0

我正在嘗試編寫一個用於替換所有匹配值的循環。替換PowerShell中的每個字符串都不按預期方式工作

Key               Value              
---               -----              
client              xxxx             
type               Test               
name              xxx           
env             ALL  

和我還有一個app.config文件,如下所示,

<add key="ClientName" value="{{client}}" /> 
    <add key="InstallForClients" value="{{client}}" /> 

我特林每串(其中兩個大括號之間退出),與每一位關鍵匹配在字典中。如果匹配,則用值替換字符串。

我無法替換這些值。這裏是我正在處理的powershell腳本,

if ("$string" -eq "$stringtocheck") 
{ 
echo "Replacing $stringtocheck with $newstring in $source" 
(Get-content $source) | ForEach-Object { $_ -replace '{{$stringtocheck}}', $newstring } | Set-Content $destination 
} 
} 

有人可以幫助我更新此替換循環。

+0

不知道這是唯一的問題,但就在這裏:'-replace「{{$ stringtocheck}}」'你應該改變單引號以雙引號,如果你想使用$ stringtocheck變量的值 –

+0

即使使用雙引號,它也不起作用。 – mahesh

回答

0

試試這個

​​
+0

用此行更新了我的腳本, $ config = $ source -replace [regex] :: Escape($ stringtocheck),$ myDictionary.GetEnumerator() $ config |設置內容$目標 但我仍然無法取代值。 – mahesh

+0

你說你更新了腳本。你有沒有嘗試過使用我的腳本?嘗試用你的配置文件的路徑替換c:\ temp \ configoriginal.txt。 –

+0

我已經使用這個命令。 $ config = $ source -replace [regex] :: Escape($ stringtocheck),$ myDictionary.GetEnumerator()$ config |設置內容$目的地 – mahesh

相關問題