Hi all
I'm a Powershell newbie and I'm desperately trying to work out how to add the following value to a web.config file under the <defaultProxy> node
<bypasslist> />
<add address="https://myapp.my.org.uk" />
</bypasslist>
The web.config is stored in "D:Inetpubwwwrootmyappweb.config"
I've got some ideas but no clear idea of how to put it all together
" - Start by casting it to XML- Then walk through the nodes and edit them- When finished, you have to call a method to write it back out ot XML format"
How do I do all these things?
"Start by casting it to XML"
Will this work?
$xml = [xml](get-content $webConfigPath)
$xml.Save($backup)
$root = $xml.get_DocumentElement();
"Then walk through the nodes and edit them"
Now I'm lost...
Anyone out there who can help a struggling beginner?
Cheers
Jonj
append data to web.config using Powershell
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
append data to web.config using Powershell
Hi all
I'm a Powershell newbie and I'm desperately trying to work out how to add the following value to a web.config file under the <defaultProxy> node
<bypasslist> />
<add address="https://myapp.my.org.uk" />
</bypasslist>
The web.config is stored in "D:Inetpubwwwrootmyappweb.config"
I've got some ideas but no clear idea of how to put it all together
" - Start by casting it to XML- Then walk through the nodes and edit them- When finished, you have to call a method to write it back out ot XML format"
How do I do all these things?
"Start by casting it to XML"
Will this work?
$xml = [xml](get-content $webConfigPath)
$xml.Save($backup)
$root = $xml.get_DocumentElement();
"Then walk through the nodes and edit them"
Now I'm lost...
Anyone out there who can help a struggling beginner?
Cheers
Jonj
I'm a Powershell newbie and I'm desperately trying to work out how to add the following value to a web.config file under the <defaultProxy> node
<bypasslist> />
<add address="https://myapp.my.org.uk" />
</bypasslist>
The web.config is stored in "D:Inetpubwwwrootmyappweb.config"
I've got some ideas but no clear idea of how to put it all together
" - Start by casting it to XML- Then walk through the nodes and edit them- When finished, you have to call a method to write it back out ot XML format"
How do I do all these things?
"Start by casting it to XML"
Will this work?
$xml = [xml](get-content $webConfigPath)
$xml.Save($backup)
$root = $xml.get_DocumentElement();
"Then walk through the nodes and edit them"
Now I'm lost...
Anyone out there who can help a struggling beginner?
Cheers
Jonj
append data to web.config using Powershell
You're on the right track. Practice "walking" the tree and pipe the different objects to get-member so you can see the methods. I don't know if you are adding an entire node or just the attribute. Or are you modifying an existing node and attribute? Use the Save() method when you are finished.
append data to web.config using Powershell
The existing web.config looks like this:
<defaultProxy>
<proxy proxyaddress="http://172.16.11.118:8080
<defaultProxy>
<proxy proxyaddress="http://172.16.11.118:8080
append data to web.config using Powershell
When you load the XML document, you can navigate it like an object.PS C:test> cat pets.xml<Pets> <Pet> <Breed>Ferret</Breed> <Age>3</Age> <Name>Patch</Name> </Pet> <Pet> <Breed>Bulldog</Breed> <Age>5</Age> <Name>Chesty</Name> </Pet></Pets>PS C:test> [xml]$pets=get-content pets.xmlPS C:test> $petsPets----PetsPS C:test> $pets.petsPet---{Patch, Chesty}PS C:test> $pets.pets.petBreed Age Name----- --- ----Ferret 3 PatchBulldog 5 ChestyPS C:test> $pets.pets.pet[0]Breed Age Name----- --- ----Ferret 3 PatchPS C:test> $pets.pets.pet[0].breedFerretPS C:test>
append data to web.config using Powershell
$newNode = $xml.createElement("bypasslist")
$xml.defaultProxy.appendChild($newNode)
Remember that XML is case sensitive.
bypasslist and ByPassList are not equivalent.
$xml.defaultProxy.appendChild($newNode)
Remember that XML is case sensitive.
bypasslist and ByPassList are not equivalent.
append data to web.config using Powershell
Sorry but you are trying to mix apples and oranges.
You need to select teh target node and use AppendCHild with aa newly created node. Doing it with a text representation is flaky at best. ImportNode grabs nodes from a document and loads them but is not good for newly created nodes.
Simplify!
Yhe above works correctly on my copy of web.config. It will not work if teh file has been damaged by hand editing but that should show up on loading into the xml class.
I renamed the saved copy so you can retry and not lose the original. Changing the save name will allow the file to be overwritten.
Be careful of editing a web.config file on an active web site. CHnages cause the site to be restarted and take effect immediately. Errors in web.config can crash site until errors are fixed.
jvierra2009-06-16 08:00:06
You need to select teh target node and use AppendCHild with aa newly created node. Doing it with a text representation is flaky at best. ImportNode grabs nodes from a document and loads them but is not good for newly created nodes.
Simplify!
Code: Select all
$filename = "c:web.config"
$xml= [xml] (Get-Content $filename)
$target=$xml.selectSingleNode("//system.net")
$newNode=$xml.createElement("bypasslist")
$target.defaultProxy.AppendChild($newNode)
$addnode = $xml.createElement("add")
$newNode.AppendChild($addNode)
$newAttribute = $xml.CreateAttribute("address")
$newAttribute.set_Value("https:// myapp.my.org.uk")
$addNode.SetAttributeNode($newAttribute )
$xml.Save("$filename.xml")
Yhe above works correctly on my copy of web.config. It will not work if teh file has been damaged by hand editing but that should show up on loading into the xml class.
I renamed the saved copy so you can retry and not lose the original. Changing the save name will allow the file to be overwritten.
Be careful of editing a web.config file on an active web site. CHnages cause the site to be restarted and take effect immediately. Errors in web.config can crash site until errors are fixed.
jvierra2009-06-16 08:00:06
append data to web.config using Powershell
Thanks you very much - that is awsesome work and soooo fast -
It works - inspiring stuff - thanks again
Jonj
It works - inspiring stuff - thanks again
Jonj