使用powershell编辑xml

时间:2015-02-12 21:36:54

标签: xml powershell edit

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

1 个答案:

答案 0 :(得分:0)

使用[Xml]类型时,PowerShell非常灵活。在下面的示例中,我从上面读取xml(修复了一些xml格式错误),然后使用散列数组($ replacementments)指定哪些命名依赖程序集应具有哪些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遍历$ replacementments中的元素,然后通过其assemblyIdentity.name查找匹配的dependentAssembly,然后更新newVersion值。