Chapter 10. Metaprogramming with scriptblocks and dynamic code
This chapter covers
- Scriptblocks
- Creating and managing objects
- Creating code dynamically
- Steppable pipelines
Philosophy have I digested, The whole of Law and Medicine, From each its secrets I have wrested, Theology, alas, thrown in. Poor fool, with all this sweated lore, I stand no wiser than I was before.
Johann Wolfgang Goethe, Faust
Greek letters are cool ...
Not actually a quote from Beavis and Butthead
Chapters 6 through 9 covered the basic elements of programming in PowerShell and introduced modules as a way of aggregating your code into reusable pieces. In this chapter, we’ll take things to the next level and talk about metaprogramming, the term used to describe the activity of writing programs that create or manipulate other programs. If you’re not already familiar with this concept, you may be asking why you should care. In most environments, if the designer makes a mistake, the user is stuck with the result. This isn’t true in PowerShell. Metaprogramming lets you poke into the heart of the system and make things work the way you need them to.
Here’s an analogy that should give you the full picture: Imagine buying a computer that was welded shut. You can run all the existing programs and even install new programs. But a case that’s welded shut doesn’t allow for hardware upgrades.