This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
Windows Management Framework, which includes Windows Power Shell 2.0, WinRM 2.0, and BITS 4.0, was officially released to the world this morning. (...)
You can access properties of those elements the same way you would normally work with an object: PS > ($xml.rss.channel.item).title Windows Management Framework is here!
Since these are rich PowerShell objects, Example 10-3 demonstrates how you can use PowerShell’s advanced object-based cmdlets for further work, such as sorting and filtering. Example 10-3. Sorting and filtering items in an XML document PS > $xml.rss.channel.item | Sort-Object title | Select-Object title title ----Analyzing Weblog Data Using the Admin Development Model Announcing: Open Source PowerShell Cmdlet and Help Designer Help Us Improve Microsoft Windows Management Framework Introducing the Windows 7 Resource Kit PowerShell Pack New and Improved PowerShell Connect Site PowerShell V2 Virtual Launch Party Remoting for non-Admins Select -ExpandProperty The Glory of Quick and Dirty Scripting Tonight is the Virtual Launch Party @ PowerScripting Podcast Understanding the Feedback Process What's New in PowerShell V2 - By Joel "Jaykul" Bennett What's Up With Command Prefixes? Windows Management Framework is here! XP and W2K3 Release Candidate Versions of PowerShell Are Now Available ...
Discussion PowerShell’s native XML support provides an excellent way to easily navigate and access XML files. By exposing the XML hierarchy as properties, you can perform most tasks without having to resort to text-only processing or custom tools. In fact, PowerShell’s support for interaction with XML goes beyond just presenting your data in an object-friendly way. The objects created by the [xml] cast in fact represent fully featured System.Xml.XmlDocument objects from the .NET Framework. Each property of the resulting objects represents a System.Xml.XmlElement object from the .NET Framework as well. The underlying objects provide a great deal of additional functionality that you can use to perform both common and complex tasks on XML files.
10.1 Access Information in an XML File | 239
The underlying System.Xml.XmlDocument and System.Xml.XmlElement objects that support your XML also provide useful properties in their own right: Attributes, Name, OuterXml, and more. PS > $xml.rss.Attributes #text ----2.0 http://purl.org/dc/elements/1.1/ http://purl.org/rss/1.0/modules/slash/ http://wellformedweb.org/CommentAPI/
In PowerShell version one, PowerShell hid these underlying properties by default. To access them in PowerShell version one, use the PsBase property on any node. The PsBase property works on any object in PowerShell and represents the object underneath the PowerShell abstraction.
For more information about using the underlying .NET objects for more advanced tasks, see Recipe 10.2 and Recipe 10.4. For more information about working with XML in PowerShell, see Table F-11 in Appendix F.
See Also Recipe 10.2, “Perform an XPath Query Against XML” Recipe 10.4, “Modify Data in an XML File” Recipe 12.1, “Download a File from the Internet” Table F-11
10.2 Perform an XPath Query Against XML Problem You want to perform an advanced query against an XML file, using XML’s standard XPath syntax.
Solution Use PowerShell’s Select-Xml cmdlet to perform an XPath query against a file. For example, to find all post titles shorter than 30 characters in an RSS feed: PS > $query = "/rss/channel/item[string-length(title) < 30]/title" PS > Select-Xml -XPath $query -Path .\powershell_blog.xml | Select -Expand Node
240 | Chapter 10: Structured Files
#text ----Remoting for non-Admins
Discussion Although a language all its own, the XPath query syntax provides a powerful, XMLcentric way to write advanced queries for XML files. The Select-Xml cmdlet lets you apply these concepts to files, XML nodes, or simply plain text. The XPath queries supported by the Select-Xml cmdlet are a popular industry standard. Beware, though. Unlike those in the rest of PowerShell, these queries are case-sensitive!
The Select-Xml cmdlet generates a SelectXmlInfo object. This lets you chain separate XPath queries together. To retrieve the actual result of the selection, access the Node property. PS > Get-Content page.html Welcome to my Website
...PS > $content = [xml] (Get-Content page.html) PS > $result = $content | Select-Xml "/HTML/HEAD" | Select-Xml "TITLE" PS > $result Node ---TITLE