0

I need to update this config file in such a way that, all the values of 'newVersion' needs to be updated to a single value.

<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>

I have tried this below

$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)

I know this would change only to the "D" part, how can change to the above A,B and C also. Thanks for your help !

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

'newversion=.*' will also match and replace the closing "/>" and thereby render your XML-file invalid. I'd rather use: (gc C:\temp\config.xml) -replace '(newversion=)".*?"', '$1"1.1.1.1"'.
Keep in mind, that this replaces the value of ALL occurences of "newversion", regardless of which node it is an attribute. The answer of @dotnetom is more save.
@ManuelBatsching thanks for the remarks. Btw the op said he wants to replace all occurrence and did not mention any restriction. Using replace is more intuitive in this case I think.
Thank you Manuel and Kayasax, yes it is true that even closing "/>" got replaced and I just added 'newversion="%buildnumber%/>"' immediately. However, how should I understand -replace '(newversion=)".*?"', '$1"1.1.1.1"'.. If the question mark is representing only a single character, then I think /> are 2 characters right... correct me.. I might be completely wrong on this... thanks to both of you again..
The star-quantor in the regular expression .*" is greedy, which means that it will match as much as possible. In your case it will match any character until it finds the last " in the line. This works in the example above only if newversion="someValue" is the last attribute in the line. If not, it will mess up your XML-document again, because it will also match any attributes that follow newversion. The ? removes this danger by changing the behavior of the star-quantor to "non-greedy". So I'd like to encourage you to keep the ?.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.