0

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>

1 Answer 1

1

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

2 Comments

it works in higher version of powershell but is actually complaining for version 3.0 and lower with the below error; + ... tAssembly.Where{ + ~ Unexpected token '{' in expression or statement. At line:18 char:345 + ... tAssembly.Where{ + ~ Missing closing ')' in expression.
In earlier PowerShell versions the .where{ ... } method indeed doesn't exist. For those versions you might use the Where-Object cmdlet: ... |Where-Object { $_.assemblyIdentity.name-eq 'identitiy1' }

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.