Chapter 20. PowerShell and XML: better together

 

Josh Gavant

In the words of PowerShell creator Jeffrey Snover in his 2002 “Monad Manifesto” (www.jsnover.com/Docs/MonadManifesto.pdf), “[t]he traditional model for administrative automation ... requires clumsy, lossy, imprecise text manipulation utilities.” Command-line input and answer files are unstructured text; output is a hodgepodge of details that the command’s authors deem helpful. To use the text output from one command as input for another, that text has to be parsed, and the script author has to pray that the chosen parsing works consistently. In fact, Snover coined the term prayer-based parsing, and defines it as follows:

Definition

Prayer-based parsing is when you parse the text and pray that you got it right. Examples include, cutting off the first three lines and praying that it wasn’t four, cutting out columns 30–40 and praying that those spaces aren’t tabs, or casting as an integer and praying that it’s 32 bits. (“Monad Manifesto,” p. 4, note 7)

Figure 1 provides a simple example of prayer-based parsing gone wrong. In the first series, the word “quick” is replaced with “slow” and all is well. In the second series, because the input file now uses the four-letter “fast” instead of the five-letter “quick,” the parser unintentionally removes a space between words. A small difference, but it could easily crash a script or a program.

What is XML?

XML in .NET and PowerShell

Adapted objects and XMLNodeAdapter

Read and write XML documents

Special XML cases

Summary

About the author

sitemap