This is one of those things that always seems like it should be really easy, and straightforward.
Well, if you are deeply familiar with XML it might be. And If you can pry your string apart into an array using split or regex you have half of the problem tackled.
Lets begin by looking at my string.
$s = "Machine Tier - ScaleOut[Machine03,Machine04,Machine05]Machine Tier[Machine02]"
Ugly thing. That actually contains three elements of data.
The entire string represents a Service. The data outside the brackets represents a Tier. The data inside the brackets is a list of VMs in that Tier.
First, I need to break it all apart.
$a = $s.Split(']');
foreach($b in $a) {
$c = $b.Split('[');
"Tier = " + $c[0];
"VMs = " + $c[1];
}
Tier = Machine Tier - ScaleOut
VMs = Machine03,Machine04,Machine05
Tier = Machine Tier
VMs = Machine02
Tier =
VMs =
Okay, that is close, but not quite there. And still not usable.
First, lets do something about that empty last item in the array.
$a = $s.Split(']');
foreach($b in $a) {
if($b){
$c = $b.Split('[');
"Tier = " + $c[0];
"VMs = " + $c[1];
}
}
And now, break apart the VM name string
$a = $s.Split(']');
foreach($b in $a) {
if($b){
$c = $b.Split('[');
"Tier = " + $c[0];
$VMs = $c[1].Split(",")
foreach ($vm in $VMs){
"VM = " + $vm
}
}
}
Tier = Machine Tier - ScaleOut
VM = Machine03
VM = Machine04
VM = Machine05
Tier = Machine Tier
VM = Machine02
Okay, now I have something that is usable. And now I want to turn that into XML.
I have spent a great deal of time searching on PowerShell and XML. Trying to figure out how to build an XML using PowerShell on the fly in my script. All the examples always start with a TXT based framework of some type that is in turn manipulated by the author. Or a file is read, or objects are queried.
I am sorry, but this is not an example of generating an XML using PowerShell as the title suggests. It is a huge formatted text object that is manipulated. Such a frustrating example to hit over and over again.
Well, I just have this silly string I parsed apart. And I want that to be XML. It already has meaning to the pieces, I just need to make them tags and whatnot.
I mentioned early on that I don’t know much about XML. I read it, transpose it, consume the XML of others’, I never made my own. So, I had to visit the wisdom of a developer friend and make sure that i was doing it ‘correctly’ and it was ‘proper’.
In it simplest sense, to create an empty XML document in PowerShell do this: “$service = New-Object System.Xml.XmlDocument” and you have an XML document. But how do you put things into it.
Okay, this is all about objects, and object manipulation. You don’t just add tags in. You create objects that are of the type $service and you add them back to $service in the correct order.
I began with this:
$service = New-Object System.Xml.XmlDocument
$tier = $service.CreateElement("Tier")
$tier.SetAttribute("Name","My Test Tier")
$vm = $service.CreateElement("VM")
$vm.SetAttribute("Name","My VM Name")
$tier.AppendChild($vm)
$service.AppendChild($tier)
I create the XML Document $service. Then I create an Element of type $service and define a Name and value as an Attribute of that Element. I repeat this and create a $vm element as well.
If you query $service, you find that these things aren’t there. They are three separate objects at this point. They are all share the same object type of $service. But nothing more. Now I assemble them together.
I take the $tier object and I add the $vm object to it as a child. This nests <vm> under <tier> in the XML. I then repeat this adding this updated $tier object to $service as a child.
The above is fine enough. However, I was informed that I was missing a root element. To define the Document.
$service = New-Object System.Xml.XmlDocument
$root = $service.CreateElement("RootElement")
$tier = $service.CreateElement("Tier")
$tier.SetAttribute("Name","My Test Tier")
$vm = $service.CreateElement("VM")
$vm.SetAttribute("Name","My VM Name")
$tier.AppendChild($vm)
$root.AppendChild($tier)
$service.AppendChild($root)
I have been informed that simply doing what I just showed above is still not quite good enough. It meets the requirement of a root element but totally missed on the intent or spirit. We will get back to that.
So, what does this XML document look like? Well, you can step through $service and try to imagine it in your head our you can send it out to a file and open it in notepad.
$service.Save(".\service.xml")
Open that up and you have:
<RootElement>
<Tier Name="My Test Tier">
<VM Name="My VM Name" />
</Tier>
</RootElement>
Now. I have some XML. And I am feeling pretty proud of myself.
Why did I ever do this in the first place? So I could do this:
PS C:\Users\Public\Documents> $service.GetElementsByTagName("Tier")
Name VM
---- --
My Test Tier VM
PS C:\Users\Public\Documents> $service.GetElementsByTagName("VM")
Name
----
My VM Name
Now I have associations that I can look for and query against.
Now, the fun part, meshing those two different activities together as one. And I have the following:
$tiers = $s.Split(']')
$service = New-Object System.Xml.XmlDocument
$root = $service.CreateElement("RootElement")
foreach($tierString in $tiers) {
if($tierString){ #ignore any empties
$tier = $service.CreateElement("Tier")
$e = $tierString.Split('[')
$tier.SetAttribute("Name",$e[0])
$VMs = $e[1].Split(",")
foreach ($vmString in $VMs){
if($vmString){
$vm = $service.CreateElement("VM")
$vm.SetAttribute("Name",$vmString)
}
$tier.AppendChild($vm)
}
}
$root.AppendChild($tier)
}
$service.AppendChild($root)
Now, back to that really lazy root element I created. In practice, that should be some meta information about the XML document itself. If you look at lots of XML you will see things like creation dates, versions, authors, and a name that is somehow descriptive.
After I create the $root object (with a better name) I just update it with a few attributes and I am good to go.
$root.SetAttribute("version","1.0")
$root.SetAttribute("createon",(Get-Date))
$root.SetAttribute("createdby","brianeh")
Now, a really short example of what I can now do with this information.
# query on a specific VM element with a Name equal to "Machine02"
# The Item(0) returns the XML object itself instead of a reference to it. $me = $me.ItemOf(0)
$me = ($service.SelectNodes("//VM[@Name='Machine02']")).Item(0)
# What Tier do I belong to?
$me.ParentNode.Name
# Do I have Siblings or am I an only Child?
$me.NextSibling
$me.PreviousSibling
$me.ParentNode.ChildNodes.Count
Note: be careful. These queries are in XPath format, and they are case sensitive.
You can also simply walk the XML as PowerShell supports that as a ‘.’ notation path.
No comments:
Post a Comment