Wednesday, June 22, 2011

More PowerShell, XML and XPath -- Select-Xml and multi-node selections

Let's start with the same document as before.

Now at PowerShell 2, we can also do

which avoids the explicit call to the .net infrastructure, but wraps up the actual content we got in the previous example as the Node field inside an object. This is not so much of a problem when picking a single node, but when you want to do the equivalent of SelectNodes nested (e.g. for each Rule, do something with each setting) some disassembly is required in order to perform the inner selection:

It is, however, a bit of an oversight that the Select-Xml cmdlet doesn't consume SelectXmlInfo objects through the usual type conversion mechanisms inside the cmdlet infrastructure:

but once you know there's that gotcha, it can be worked around.

Post a Comment