使用 powershell 编辑 xml

Edit xml using powershell

<configuration>
  <runtime>
    <assemblyBindings>
      <dependentAssemblyss>
        <assemblyIdentity name="A" publicKeyToken="5d861ad8ad8cd06f" culture="neutral" />
        <bindingRedirects oldVersion="0.0.0.0-68834.68834.68834.68834" newVersion="4.5.0.103" />
      </dependentAssemblys>
      <dependentAssembly>
        <assemblyIdentity name="B" publicKeyToken="ae714df8cd90bc8f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="3.0.0.103" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="C" publicKeyToken="22955931b98512b6" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="8.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>

我已经发布了这个问题并得到了这个问题的答案...我本可以发布这个新问题作为对该问题的评论,但由于字符限制,我将其发布在这里

如何更改 newVersion 值? A、B 将具有 0.0.0.1,C、D 将具有 0.0.0.2 谢谢!!

PowerShell 在处理 [Xml] 类型时非常灵活。在下面的示例中,我从上面读取 xml(修复了一些 xml 格式错误),然后使用哈希数组($replacements)指定哪些命名依赖程序集应具有哪些 newVersion 值.

[Xml]$doc = @"
<configuration>
  <runtime>
    <assemblyBindings>
      <dependentAssembly>
        <assemblyIdentity name="A" publicKeyToken="5d861ad8ad8cd06f" culture="neutral" />
        <bindingRedirects oldVersion="0.0.0.0-68834.68834.68834.68834" newVersion="4.5.0.103" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="B" publicKeyToken="ae714df8cd90bc8f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="3.0.0.103" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="C" publicKeyToken="22955931b98512b6" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-65534.65534.65534.65534" newVersion="8.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>
    </assemblyBindings>
  </runtime>
</configuration>
"@
$replacements =@(@{'name'='C'; 'newVersion'='0.0.1'},@{'name'='D'; 'newVersion'='0.0.3'})
foreach($replacement in $replacements)
{
  $doc.configuration.runtime.assemblyBindings.dependentAssembly | `
     Where-Object -FilterScript {$_.assemblyIdentity.name -eq $replacement.name} |% `
     {$_.bindingRedirect.newVersion = $replacement.newVersion}
}
#Output the raw xml to show the changes:
write-host $doc.OuterXml

foreach遍历$replacements中的元素,然后通过其assemblyIdentity.name找到匹配的dependentAssembly,然后更新newVersion值。