Asked by:
Replace a tag in Web.config

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