2016-12-29 148 views
0

任務是使用power-shell腳本更新web.config和app.config中的應用程序設置。經過一番搜索後,我發現一些腳本更新單個文件,但不是多個文件。誰能幫忙?使用PowerShell腳本更新多個配置文件

$Config = C:\inetpub\wwwroot\TestService\Web.config 
$doc = (Get-Content $Config) -as [Xml] 
$obj = $doc.configuration.appSettings.add | where {$_.Key -eq 'SCVMMServerName'} 
$obj.value = CPVMM02 
$doc.Save($Config) 
+1

所以你想Get-ChildItem和for循環? – Matt

+0

我不確定。但這可能有幫助。你能分享怎麼做嗎? – Sridhar

+0

$文件名= GET-ChildItem -Path 「C:\ TestService的」 -Recurse -Include的* .config 的foreach($在$文件名的文件) {\t \t $ DOC =(獲取內容$文件) - 作爲[XML ] If($ doc -match'key1'){ $ obj1 = $ doc.configuration.appSettings.add |其中{$ _。關鍵-eq '鍵1'} \t $ obj1.value = '真' $ doc.Save($文件)\t }} 這沒有工作 – Sridhar

回答

0

我可以給你一個邏輯關係。您可以使用- 匹配選擇字符串獲得想要更新的行,然後類似地,您可以使用-notmatch選擇文件中已存在的其餘事項。 把它們放在變量中。更新該行,將其存儲回變量中。

然後同時設置(修改後的行變量,哪些是你沒有修改剩下的值)使用集內容

希望你有一個掀起對如何處理

+0

$文件名= GET-ChildItem -Path 「C:\ TestService的」 -Recurse -Include *的.config 的foreach(在$文件名$文件) {\t \t $ DOC =(獲取內容文件$)-as [XML] 如果($ DOC -match' key1'){ $ obj1 = $ doc.configuration.appSettings.add |其中{$ _。關鍵-eq '鍵1'} \t $ obj1.value = '真' $ doc.Save($文件)\t }} 這沒有工作 – Sridhar

+0

讓我通過它去 –

+0

同解決方案爲我工作。我用包含,我的鑰匙是'SCVMMServerName',所以它的工作。 '$ file =(Get-Content $ file)-as [Xml]'$ fileNames = Get-ChildItem -Path「C:\ TestService」-Recurse -Include'* .config' foreach($ file in $ fileNames) 如果($ doc -contains'SCVMMServerName') {$ obj1 = $ doc.configuration.appSettings.add | {$ _。Key -contains'key1'} \t $ obj1.value ='true'$ doc.Save($ file)}}'。 Sodawillow的解決方案也是有效的 –

0

迴文件有很多方法可以做到這一點,例如:

"C:\inetpub\wwwroot\TestService\Web.config", 
"C:\inetpub\wwwroot\TestService\App.config" | 
    ForEach-Object { 
     $doc = (Get-Content $_) -as [Xml] 
     $obj = $doc.configuration.appSettings.add | 
      Where-Object { $_.Key -eq 'SCVMMServerName' } 
     $obj.value = CPVMM02 
     $doc.Save($_) 
    } 
+0

如何在kudu調試控制檯中執行此腳本來更新web.config? – Sridhar