Home > front end >  Powershell get values from xml file
Powershell get values from xml file

Time:07-29

I am new to powershell and have a requirement to extract oldVersion and newVersion value from web.config file under assembly bindings where assemblyIdentity name="identitiy1", not sure how to do it, any help would be highly appreciated, below is the sample from the config file.

I tried to get the appsetting keys something like this Select-Xml -Path filePath -XPath '/configuration/appSettings/add' | ForEach-Object { $_.Node.key} and that works but note sure how to read it from assemblyBinding.

    <configuration>
   <appSettings file="filepath">       
    <add key="key1" value="value1"/>
    <add key="key2" value="value2" />
    <add key="key3" value="value3" />
  </appSettings>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="xxxx" publicKeyToken="xxx" culture="xx" />
            <bindingRedirect oldVersion="xxx-yyy" newVersion="yyy" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="identitiy1" publicKeyToken="xxx" culture="xxx" />
            <bindingRedirect oldVersion="1.0.0-5.0.0" newVersion="5.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="xxx" publicKeyToken="xxx" culture="xxx" />
            <bindingRedirect oldVersion="xxx-yyy" newVersion="yyy" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>

CodePudding user response:

You might simply use PowerShell's dot notation for this:

$Xml = [Xml]@'
<configuration>
   <appSettings file="filepath">
    <add key="key1" value="value1"/>
    <add key="key2" value="value2" />
    <add key="key3" value="value3" />
  </appSettings>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="xxxx" publicKeyToken="xxx" culture="xx" />
            <bindingRedirect oldVersion="xxx-yyy" newVersion="yyy" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="identitiy1" publicKeyToken="xxx" culture="xxx" />
            <bindingRedirect oldVersion="1.0.0-5.0.0" newVersion="5.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="xxx" publicKeyToken="xxx" culture="xxx" />
            <bindingRedirect oldVersion="xxx-yyy" newVersion="yyy" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
'@
$Xml.configuration.runtime.assemblyBinding.dependentAssembly.bindingRedirect

oldVersion  newVersion
----------  ----------
xxx-yyy     yyy
1.0.0-5.0.0 5.0.0
xxx-yyy     yyy

Note that below the level of assemblyBinding you actually make use of the nifty PowerShell feature called member access enumeration. This means if you want to address a specific property (e.g. the newVersion of the assembly with id name identitiy1), you might do something like this:

$Xml.configuration.runtime.assemblyBinding.dependentAssembly.Where{
    $_.assemblyIdentity.name-eq 'identitiy1'
}.bindingRedirect.newVersion
5.0.0
  • Related