2015-02-11 93 views
0

我需要以這種方式更新此配置文件,所有'newVersion'值都需要更新爲單個值。更新config xml文件並使用powershell保存

<configuration> 
<runtime> 
<assemblyBinding> 
    <dependentAssembly> 
     <assemblyIdentity name="A" 
publicKeyToken="5d861ad8ad8cd06f" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="4.0.0.103" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="B" publicKeyToken="ae714df8cd90bc8f" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="4.0.0.103" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="C" publicKeyToken="22955931b98512b6" culture="neutral" /> 
     <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="4.0.0.103" /> 
     </dependentAssembly> 
     <dependentAssembly> 
       <assemblyIdentity name="D" 
            publicKeyToken="585a888b4a9ba2e3" 
            culture="neutral" /> 
       <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" 
           newVersion="2.5.0.1286"/> 

       </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> 
    </startup> 
</configuration> 

我已經試過這下面

$appconfig='file location' 
$doc = (Get-Content $appconfig) -as [Xml] 
$obj = $doc.configuration.runtime.assemblyBinding.dependentAssembly 
$obj2 = $obj.assemblyIdentity | where {$_.name -eq 'D'} 
$obj.bindingRedirect.newVersion="1.1.1.1" 
echo $obj.bindingRedirect.newVersion 
$doc.Save($appconfig) 

我知道這會改變只爲「d」的一部分,怎麼可以在上面A,B和C也更改爲。謝謝你的幫助 !

回答

1
(gc C:\temp\config.xml) -replace '(newversion=)".*?"' ,'$1"1.1.1.1"' # if you want to save the file add |sc c:\temp\config.xml 
+0

你搖滾!!非常感謝! – Bhaskar 2015-02-11 21:00:09

+1

''newversion =。*''也會匹配並替換關閉的「/>」,從而導致您的XML文件無效。我寧願使用:'(gc C:\ temp \ config.xml)-replace'(newversion =)「。*?」','$ 1「1.1.1.1」''。 – 2015-02-11 21:20:30

+0

請記住,這取代了「newversion」的所有出現的值,而不管哪個節點是屬性。 @dotnetom的答案更省錢。 – 2015-02-11 21:29:44