After my first post (Powershell: Parsing XML part 1) about working with XML and Powershell, I have create this second post which describes how to create a XML file, Add content to the XML file, remove content from the XML file and save the XML objects as a file.
While I was writing a script which communicates with a webserver, I realized that I need some error handling if the server can not anwser a request from my script. For example if the webserver is down or has to much load.
I created a little retry part (Powershell: Simple retry logic) which retries several times. But if the Server is down for several hours or days your script hangs in a retry loop. Obviously this cant be the solution. After a little bit of thinking a decided to write the data, which I was trying to send, down in a XML file. And the next time the script runs it reads the XML file and tries to send the data again.
Thats the story behind my idea for saving data in a XML file.
Creating a XML object
<pre lang="xml">$FruitList = "<Box> <Fruit> <Name>Banana</Name> <Color>yellow</Color> </Fruit> </Box>"
Save a XML object as a XML file
$FruitList.Save("./myfruitlist.xml")
XML:
<Box> <Fruit> <Name>Banana</Name> <Color>yellow</Color> </Fruit> </Box>
Open a saved XML file
<pre lang="xml">$FruitList = Get-Content ./myfruitlist.xml
Add data to XML object and save it in a XML file
# Copy Object from Banana [Object]$CopyFruit = FruitList.Box.Fruit | Where-Object {$_.Name -eq "Banana"} $NewFruit = $CopyFruit.Clone() # Add Fruit to new Object $NewFruit.Name = "Apple" $NewFruit.Color = "green" # Add Fruit to XML Object $FruitList.Box.AppendChild($NewFruit) #Save to XML object ot XML file $FruitList.Save("./myfruitlist.xml")
XML:
<Box> <Fruit> <Name>Banana</Name> <Color>yellow</Color> </Fruit> <Fruit> <Name>Apple</Name> <Color>green</Color> </Fruit> </Box>
Change data from XML Object and save it as XML file
# Change Apple Color $FruitList.Box.Fruit | Where-Object {$_.Name -eq "Apple"} | ForEach-Object { $_.Color = "red" } #Save to XML object ot XML file $FruitList.Save("./myfruitlist.xml")
XML:
<Box> <Fruit> <Name>Banana</Name> <Color>yellow</Color> </Fruit> <Fruit> <Name>Apple</Name> <Color>red</Color> </Fruit> </Box>
Remove data from XML Object and save it as XML file
# Remove Banana from Object $RemoveFruit = $FruitList.Box.Fruit | Where-Object {$_.Name -eq "Banana"} $FruitList.Box.RemoveChild($RemoveFruit) #Save to XML object ot XML file $FruitList.Save("./myfruitlist.xml")
XML:
<Box> <Fruit> <Name>Apple</Name> <Color>green</Color> </Fruit> </Box>
I’m searching but can find my answer – read this article still not clear. Problem is the following
I have a an XML file with
I want to loop through the scope rules and put the value from the xml into a variable.
I already put a reference to the xml file in $xmlData with Get-Content cmdlet.
$xmlData.ContentSourcesScopesRules.ScopeRules.Rule |
ForEach-Object{
$ScopeName=$_.ScopeName
$contentSourceName=$_.ContentSourceName
When debugging I see the value of $_.ScopeName in the popup in Powershell ISE but when I then use the $ScopeName variable in another cmdlet it is empty.
Do you have any idea how to get the xml value into a variable as a string? I tried some strongtyping of the variables but may some casting is needed…
Thanks for any feedback – I know it is maybe better to ask in a forum ..
HI,
I want to write into xml and adding this parameter
Release number
2014-01-21.26
can you help me
Good example, this is partially what I am looking for.
If I need to add pineapple after apple to the above list, how do I make it happen?
Is cloning possible multiple times?
I ask this because if I use the same object to write pineapple, my apple is over ridden by pineapple.
Problem statement : I need to have multiple fruits in this fruit list.
Thanks in advance!