1

This is my web.config file and content.

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
  </dependentAssembly>
</assemblyBinding>
</runtime>

This is my power shell script to remove everything from the tag ...

(Get-Content C:\Temp\web.config) | 
Foreach-Object {$_ -replace '<dependentAssembly>[\s\S]*?<\/dependentAssembly>', ''} | 
Set-Content C:\Temp\web.config

Script is running but its not removing anything. Can anyone please help me in this. What the mistake i am making in above code.

Expected output:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    </assemblyBinding>
</runtime>
Mukesh Singh
  • 303
  • 2
  • 5
  • 17

1 Answers1

2

Don't use regular expressions for doing that. PowerShell offers you all what you need to deal with XML.

$xml = [xml] @"
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
  </dependentAssembly>
</assemblyBinding>
</runtime>
"@

$xml.runtime.assemblyBinding.RemoveAll()

$xml.Save("c:\temp\test.xml")
David Brabant
  • 41,623
  • 16
  • 83
  • 111