locked
Replace a tag in Web.config RRS feed

  • Question

  • Dear All,

    I am trying to replace a tag in my web.config but its not working, below is my script

    $webConfig = '.\web.config'
    [xml]$web =  (Get-Content $webConfig)
    $web -replace '<httpErrors errorMode="Detailed" />','<httpErrors errorMode="Custom"><remove statusCode="404" subStatusCode="-1" /><error statusCode="404" path="/test/home.aspx" responseMode="Redirect" /></httpErrors>'
    $web.Save($webConfig)

    Any help would be greatly appreciated

    Wednesday, October 24, 2018 3:05 PM

All replies

  • Any one??
    Wednesday, October 24, 2018 5:21 PM
  • (get-content C:\Temp\test.txt).Replace('<httpErrors errorMode="Detailed" />',
      '<httpErrors errorMode="Custom"><remove statusCode="404" subStatusCode="-1" /><error statusCode="404" path="/test/home.aspx" responseMode="Redirect" /></httpErrors>') | 
        Set-Content C:\Temp\test.txt



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, October 24, 2018 5:26 PM
  • Thank you for your reply. I tried it and it works , but  it displays in single line, I want to display it like this

    <httpErrors errorMode="Custom"> 
          <remove statusCode="404" subStatusCode="-1" /> 
          <error statusCode="404" path="/test/home.aspx" responseMode="Redirect" /> 
    </httpErrors> 

    Thanks once again

    Wednesday, October 24, 2018 6:07 PM
  • Then you must manipulate it using XML. A quick google search will be helpful

    https://www.google.com/search?ei=_7nQW4vjHIzVzgL9tZm4BA&q=powershell+remove+and+add+nodes&oq=powershell+remove+and+add+nodes&gs_l=psy-ab.3..33i22i29i30l2.21451.24192..24328...0.0..0.150.2710.0j20......0....1..gws-wiz.......0j0i71j0i131i67j0i67j0i131j0i22i30.MlZIDYGJ9RI


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, October 24, 2018 6:31 PM edit link
    Wednesday, October 24, 2018 6:30 PM
  • This does not address the multiple issue that can come from trying to edit XML.

    $webConfig = Resolve-Path '.\new.config'
    
    [xml]$xml =  Get-Content $webConfig
    $node = $xml.SelectSingleNode('//httpErrors[@errorMode="Detailed"]')
    $node.errorMode = 'Custom'
    
    $frag = $xml.CreateDocumentFragment()
    $frag.InnerXml = '<remove statusCode="404" subStatusCode="-1" />' 
    $node.AppendChild($frag)
    
    $frag = $xml.CreateDocumentFragment()
    $frag.InnerXml = '<error statusCode="404" path="/test/home.aspx" responseMode="Redirect"/>' 
    $node.AppendChild($frag)
    
    $xml.Save($webConfig)
    np $webConfig
    


    \_(ツ)_/

    Wednesday, October 24, 2018 8:37 PM