Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

The Powershell Scripting & Toolmaking

Book Don Jones


Visit to download the full and correct content document:
https://textbookfull.com/product/the-powershell-scripting-toolmaking-book-don-jones/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Learn Windows PowerShell in a Month of Lunches, Third


Edition Don Jones

https://textbookfull.com/product/learn-windows-powershell-in-a-
month-of-lunches-third-edition-don-jones/

Let's Talk Business: Stuff Your Boss Wishes You Knew 1


(2019-08-26) Edition Don Jones

https://textbookfull.com/product/lets-talk-business-stuff-your-
boss-wishes-you-knew-1-2019-08-26-edition-don-jones/

Let's Talk Business: Stuff Your Boss Wishes You Knew 1


(2019-08-26) Edition Don Jones

https://textbookfull.com/product/lets-talk-business-stuff-your-
boss-wishes-you-knew-1-2019-08-26-edition-don-jones-2/

Learn Windows PowerShell in a Month of Lunches Donald


W. Jones

https://textbookfull.com/product/learn-windows-powershell-in-a-
month-of-lunches-donald-w-jones/
Learn PowerShell Scripting in a Month of Lunches: Write
and organize scripts and tools, 2nd Edition James Petty

https://textbookfull.com/product/learn-powershell-scripting-in-a-
month-of-lunches-write-and-organize-scripts-and-tools-2nd-
edition-james-petty/

Own Your Tech Career Soft skills for technologists MEAP


V03 Don Jones

https://textbookfull.com/product/own-your-tech-career-soft-
skills-for-technologists-meap-v03-don-jones/

Don t Look Pike Wisconsin Book 1 1st Edition Alexandra


Ivy

https://textbookfull.com/product/don-t-look-pike-wisconsin-
book-1-1st-edition-alexandra-ivy/

PowerShell Cookbook, 4th Edition Lee Holmes

https://textbookfull.com/product/powershell-cookbook-4th-edition-
lee-holmes/

PowerShell 7 Workshop: Learn how to program with


PowerShell 7 on Windows, Linux, and the Raspberry Pi
1st Edition Nick Parlow

https://textbookfull.com/product/powershell-7-workshop-learn-how-
to-program-with-powershell-7-on-windows-linux-and-the-raspberry-
pi-1st-edition-nick-parlow/
The PowerShell Scripting and Toolmaking
Book
Forever Edition

Don Jones and Jeff Hicks


This book is for sale at http://leanpub.com/powershell-scripting-toolmaking

This version was published on 2018-09-20

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.

© 2017 - 2018 Don Jones and Jeff Hicks


Tweet This Book!
Please help Don Jones and Jeff Hicks by spreading the word about this book on Twitter!
The suggested tweet for this book is:
I got The #PowerShell #Toolmaking book http://leanpub.com/powershell-scripting-toolmaking
@JeffHicks & @concentrateddon
The suggested hashtag for this book is #PowerShellToolmaking.
Find out what other people are saying about the book by clicking on this link to search for this
hashtag on Twitter:
#PowerShellToolmaking
Also By These Authors
Books by Don Jones
The DSC Book
Become Hardcore Extreme Black Belt PowerShell Ninja Rockstar
Be the Master
Don Jones’ PowerShell 4N00bs
Don Jones’ The Cloud 4N00bs
Instructional Design for Mortals
How to Find a Wolf in Siberia
Tales of the Icelandic Troll
PowerShell by Mistake
The Culture of Learning

Books by Jeff Hicks


The PowerShell Practice Primer
The PowerShell Conference Book
Contents

About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i

Dedication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii

Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv

About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v


Additional Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vi

Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Pre-Requisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
The Journey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Following Along . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Providing Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x

A Note on Code Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

Lab Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii


What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Setting Up a Virtual Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Installing Windows 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv
Adding Lab Files and Configuring PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Assumptions Going Forward . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi

Part 1: Review: PowerShell Toolmaking . . . . . . . . . . . . . 1


Functions, the Right Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Tool Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Start with a Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Build a Basic Function and Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
CONTENTS

Adding CmdletBinding and Parameterizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


Emitting Objects as Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Using Verbose, Warning, and Informational Output . . . . . . . . . . . . . . . . . . . . . . . . 4
Comment-Based Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Ready to Go? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Verify Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
The Transcript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Our Read-Through . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Our Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
How’d You Do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

Part 2: Professional-Grade Toolmaking . . . . . . . . . . . . . . 12


Going Deeper with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Parameter Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Multiple Parameter Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Value From Remaining Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Help Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
More CmdletBinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
A Demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

Dynamic Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Declaring Dynamic Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Using Dynamic Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

Writing Full Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30


External Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Using Platyps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Supporting Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
“About” Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Making Your Help Updatable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Unit Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45


Starting Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Sketching Out the Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Making Something to Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
CONTENTS

Expanding the Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48


But Wait, There’s More . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Extending Output Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55


Understanding Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
The Extensible Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Extending an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Using Update-TypeData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Next Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Advanced Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Getting Fancy with Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Getting Strict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Getting Remote . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

Command Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Getting in PowerShell’s Brain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Analyzing Your Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74


Performing a Basic Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Analyzing the Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Controlling Your Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78


The process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Tools and Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

Converting a Function to a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83


Class Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Starting Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Doing the Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Making the Class Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Coding the Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Adding a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Making classes easy to use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

Publishing Your Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101


Begin with a Manifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Publishing to PowerShell Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Publishing to Private Repositories or Galleries . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
CONTENTS

Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107


Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Part 3: Controller Scripts and Delegated Adminis-


tration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Basic Controllers: Automation Scripts and Menus . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Building a Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Using UIChoice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Writing a Process Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

Graphical Controllers in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127


Design First! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
WinForms or WPF? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
WPF Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Using .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Using XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
A Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Just the Beginning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

Proxy Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147


For Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Creating the Proxy Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Modifying the Proxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Adding or Removing Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

Just Enough Administration: A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157


Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

PowerShell in ASP.NET: A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166


Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
CONTENTS

Beyond ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

Part 4: The Data Connection . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Working with SQL Server Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169


SQL Server Terminology and Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Connecting to the Server and Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Writing a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Running a Query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Invoke-Sqlcmd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Thinking About Tool Design Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Review Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

Working with XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178


Simple: CliXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Importing Native XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
ConvertTo-XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Creating native XML from scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Working with JSON Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191


Converting to JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Converting from JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Part 5: Seriously Advanced Toolmaking . . . . . . . . . . . . . 203

Tools for Toolmaking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204


Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
3rd Party . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Books, Blogs and Buzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

Measuring Tool Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210


Is Performance Important? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Measure What’s Importance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Factors Affecting Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Key Take-Away . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
CONTENTS

PowerShell Workflows: A Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215


Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Theory of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
A Quick Illustration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
When to Workflow? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Sequences and Parallels are Standalone Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Workflow Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Workflow Common Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Checkpointing Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Workflows and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Globalizing Your Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230


Starting Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Make a Data File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Use the Data File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Adding Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

Using “Raw” .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240


Understanding .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Interpreting .NET Framework Docs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Coding .NET Framework in PowerShell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Loading Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Wrap It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

Scripting at Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248


To Pipeline or not? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Foreach vs Foreach-Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Write-Progress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Leverage Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Leverage Jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Leverage Runspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Design Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Your Turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Let’s Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

Scaffolding a Project with Plaster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272


Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Plaster Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
CONTENTS

Invoking a Plaster Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273


Creating a Plaster Module Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Creating a Plaster Function Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Integrating Plaster into your PowerShell Experience . . . . . . . . . . . . . . . . . . . . . . . 288
Creating Plaster Tooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

Toolmaking Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

write sorted results to the pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

Part 6: Pester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

Why Pester Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

Core Pester Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302


Installing Pester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
What is Pester? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Pester’s Weak Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Understand Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
New-Fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

Writing Testable Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

Describe Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

Context Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312


BeforeEach and AfterEach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

It Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

Should and Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317


Should Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

Mocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Where to Mock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
How to Mock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Verifiable Mocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Parameter Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Mocking the Unmockable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Pester’s TESTDRIVE: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324


Clean Slate and Auto-Cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Working with Sample Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
CONTENTS

Using TESTDRIVE: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

Pester for Infrastructure Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327


Spinning Up the Validation Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Taking Actual Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Testing the Outcomes of Your Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328

Measuring Code Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329


Displaying Code Coverage Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334

Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337


About This Book
The ‘Forever Edition’ of this book is published on LeanPub¹, an “Agile” online publishing platform.
That means the book is published as we write it, and that means we’ll be able to revise it as needed
in the future. We also appreciate your patience with any typographical errors, and we appreciate you
pointing them out to us - in order to keep the book as “agile” as possible, we’re forgoing a traditional
copyedit. Our hope is that you’ll appreciate getting the technical content quickly, and won’t mind
helping us catch any errors we may have made. You paid a bit more for the book than a traditional
one, but that up-front price means you can come back whenever you like and download the latest
version. We plan to expand and improve the book pretty much forever, so it’s hopefully the last one
you’ll need to buy on this topic!
You may also find this book offered on traditional booksellers like Amazon. In those cases, the book
is sold as a specific edition, such as “Second Edition.” These represent a point-in-time snapshot of
the book, and are offered at a lower price than the Agile-published version. These traditionally
published editions do not include future updates.

If you purchased this book, thank you. Know that writing a book like this takes hundreds of hours
of effort, during which we’re not making any other income. Your purchase price is important to
keeping a roof over our families’ heads and food on our tables. Please treat your copy of the book as
your own personal copy - it isn’t to be uploaded anywhere, and you aren’t meant to give copies to
other people. We’ve made sure to provide a DRM-free file (excepting any DRM added by a bookseller
other than LeanPub) so that you can use your copy any way that’s convenient for you. We appreciate
your respecting our rights and not making unauthorized copies of this work.
If you got this book for free from someplace, know that you are making it difficult for us to write
books. When we can’t make even a small amount of money from our books, we’re encouraged to
stop writing them. If you find this book useful, we would greatly appreciate you purchasing a copy
from LeanPub.com or another bookseller. When you do, you’ll be letting us know that books like
this are useful to you, and that you want people like us to continue creating them.

Please note that this book is not authorized for classroom use unless a unique copy has been
purchased for each student. No-one is authorized or licensed to manually reproduce the PDF
version of this book for use in any kind of class or training environment.

¹http://leanpub.com
About This Book ii

This book is copyrighted (c)2017-2018 by Don Jones and Jeffery Hicks, and all rights are reserved.
This book is not open source, nor is it licensed under a Creative Commons license. This book is not
free, and the authors reserve all rights.
Dedication
This book is fondly dedicated to the many hardworking PowerShell users who have, for more than
a decade, invite us into their lives through our books, conference appearances, instructional videos,
live classes, and more. We’re always humbled and honored by your support and kindness, and you
inspire us to always try harder, and to do more. Thank you.
Acknowledgements
Thanks to Michael Bender, who has selflessly provided a technical review of the book. Any
remaining errors are, of course, still the authors’ fault, but Michael has been tireless in helping
us catch many of them.
About the Authors
Don Jones has been a Microsoft MVP Award recipient since 2003 for his work with Windows
PowerShell and administrative automation. He has written dozens of books on information tech-
nology, and today helps design the IT Ops curriculum for Pluralsight.com. Don is also President,
CEO, and co-founder of The DevOps Collective (devopscollective.org), which offers IT education
programs, scholarships, and which runs PowerShell.org and PowerShell + DevOps Global Summit
(powershellsummit.org).
Don’s other recent works include:

• Learn Windows PowerShell in a Month of Lunches (Manning.com)


• The DSC Book (LeanPub.com)
• The Pester Book (LeanPub.com)
• The PowerShell Scripting & Toolmaking Book (LeanPub.com)
• Learn PowerShell Toolmaking in a Month of Lunches (Manning.com)
• Learn SQL Server Administration in a Month of Lunches (Manning.com)

Follow Don on Twitter @concentratedDon, on Facebook at facebook.com/concentrateddon, or on


LinkedIn at LinkedIn.com/in/concentrateddon. He blogs at DonJones.com.
Jeffery Hicks is a grizzled IT veteran with over 25 years of experience, much of it spent as an
IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in
automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award. He works
today as an independent author, teacher and consultant. Jeff has taught and presented on PowerShell
and the benefits of automation to IT Pros worldwide since for over a decade. Jeff has authored
and co-authored a number of books, writes for numerous online sites and print publications, is a
contributing editor at Petri.com², a Pluralsight author, a frequent speaker at technology conferences
and user groups, and the Director of Community Affairs for The DevOps Collective.
You can keep up with Jeff on Twitter as @JeffHicks and on his blog at https://jdhitsolutions.com/blog.

Additional Credits
Technical editing has been helpfully provided not only by our readers, but by Michael Bender. We’re
grateful to Michael for not only catching a lot of big and little problems, but for fixing most of them
for us. Michael rocks, and you should watch his Pluralsight videos. However, anything Michael
didn’t catch is still firmly the authors’ responsibility.
²http://petri.com
Foreword
After the success of Learn PowerShell in a Month of Lunches, Jeff and I wanted to write a book that
took people down the next step, into actual scripting. The result, of course, was Learn PowerShell
Toolmaking in a Month of Lunches. In the intervening years, as PowerShell gained more traction
and greater adoption, we realized that there was a lot more of the story that we wanted to tell.
We wanted to get into help authoring, unit testing, and more. We wanted to cover working with
different data sources, coding in Visual Studio, and so on. These were really out of scope for the
Month of Lunches series’ format. And even in the “main” narrative of building a proper tool, we
wanted to go into more depth. So while the Month of Lunches book was still a valuable tutorial in
our minds, we wanted something with more tooth.
At the same time, this stuff is changing really fast these days. Fast enough that a traditional
publishing process - which can add as much as four months to a book’s publication - just can’t keep
up. Not only are we kind of constantly tweaking our narrative approach to explaining these topics,
but the topics themselves are constantly evolving, thanks in part to an incredibly robust community
building add-ons like Pester, Platyps, and more.
So after some long, hard thinking, we decided to launch this effort. As an Agile-published book on
LeanPub, we can continuously add new content, update old content, fix the mistakes you point out
to us, and so on. We can then take major milestones and publish them as “snapshots” on places like
Amazon, increasing the availability of this material. We hope you find the project as exciting and
dynamic as we do, and we hope you’re generous with your suggestions - which may be sent to us
via the author contact form from this book’s page on LeanPub.com. We’ll continue to use traditional
paper publishing, but through a self-publishing outlet that doesn’t impose as much process overhead
on getting the book in print. These hardcopy editions will be a “snapshot” or “milestone edition” of
the electronic version.
It’s important to know that we still think traditional books have their place. PowerShell Scripting in
a Month of Lunches, the successor to Learn PowerShell Toolmaking in a Month of Lunches, covers
the kind of long-shelf-life narrative that is great for traditionally published books. It’s an entry-level
story about the right way to create PowerShell tools, and it’s very much the predecessor to this book.
If Month of Lunches is about getting your feet under you and putting them on the right path, this
book is about refining your approach and going a good bit further on your journey.
Toolmaking, for us, is where PowerShell has always been headed. It’s the foundation of a well-de-
signed automation infrastructure, of a properly built DSC model, and of pretty much everything else
you might do with PowerShell. Toolmaking is understanding what PowerShell is, how PowerShell
wants to work, and how the world engages with PowerShell. Toolmaking is a big responsibility.
My first job out of high school was as an apprentice for the US Navy. In our first six weeks, we
rotated through various shops - electronics, mechanical, and so on - to find a trade that we thought
Foreword vii

we’d want to apprentice for. For a couple of weeks, I was in a machine shop. Imagine a big, not-
climate-controlled warehouse full of giant machines, each carving away at a piece of metal. There’s
lubrication and metal chips flying everywhere, and you wash shavings out of yourself every evening
when you go home. It was disgusting, and I hated it. It was also boring - you set a block of metal
into the machine, which might take hours to get precisely set up, and then you just sat back and
kind of watched it happen. Ugh. Needless to say, I went into the aircraft mechanic trade instead.
Anyway, in the machine shop, all the drill bits and stuff in the machine were called tools and dies.
Back in the corner of the shop, in an enclosed, climate-controlled room, sat a small number of nicely-
dressed guys in front of computers. They were using CAD software to design new tools and dies for
specific machining purposes. These were the tool makers, and I vowed that if I was ever going to
be in this hell of a workplace, I wanted to be a toolmaker and not a tool user. And that’s really the
genesis of this book’s title. All of us - including the organizations we work for - will have happier,
healthier, more comfortable lives as high-end, air-conditioned toolmakers rather than the sweaty,
soaked, shavings-filled tool users out on the shop floor.
Enjoy!
Don Jones
Feedback
We’d love your feedback. Found a typo? Discovered a code bug? Have a content suggestion? Wish
we’d answered a particular question? Let us know.
Zeroth, make sure you’re not using Leanpub’s online reader, as it omits some of the front matter
from the book. Use the PDF, EPUB, or MOBI version, or a printed edition you bought someplace
else.
First, please have a chapter name, heading reference, and a brief snippet of text for us to refer to.
We can’t easily use page numbers, because our source documents don’t have any.
Second, understand that due to time constraints like having full-time jobs, we can’t personally
answer technical questions and so forth. If you have a question, please hop on the forums at
PowerShell.org³, where we and a big community of enthusiasts will do our best to help.
Third, keep in mind that the EPUB and MOBI formats in particular allow little control over things
like code formatting. So we can’t usually address those for you.
Then, head to the LeanPub website and use their email link⁴ to email us. We can’t always reply
personally to every email, but know that we’re doing our best to incorporate feedback into the
book.
Finally, accept our thanks!
³http://powershell.org
⁴https://leanpub.com/powershell-scripting-toolmaking/email_author/new
Introduction
Pre-Requisites
We’re assuming that you’ve already finished reading an entry-level tutorial like Learn Windows
PowerShell in a Month of Lunches, or that you’ve got some solid PowerShell experience already
under your belt. Specifically, nothing on this list should scare you:

• Find commands and learn to use them by reading help


• Write very basic “batch file” style scripts
• Use multiple commands together in the pipeline
• Query WMI/CIM classes
• Connect to remote computers by using Remoting
• Manipulate command output to format it, export it, or convert it, using PowerShell commands
to perform those tasks

If you’ve already done things like written functions in PowerShell, that’s marvelous - but, you may
need to be open to un-learning some things. Some of PowerShell’s best practices and patterns aren’t
immediately obvious, and especially if you know how to code in another language, it’s easy to go
down a bad path in PowerShell. We’re going to teach you the right way to do things, but you need
to be willing to re-do some of your past work if you’ve been following the Wrong Ways.
We also assume that you’ve read PowerShell Scripting in a Month of Lunches, a book we wrote for
Manning. It provides the core narrative of “the right way to write PowerShell functions and tools,”
and this book essentially picks up where that one leaves off. Look for that book in late 2017 from
Manning or your favorite bookseller. Part 1 of this book briefly slams through this “the right way”
narrative just to make sure you’ve got it in your mind, but the Month of Lunches title really digs
into those ideas in detail.

Versioning
This book is written against Windows PowerShell v5/v5.1 running on Microsoft Windows. In
January 2018, Microsoft announced the General Availability of PowerShell Core 6.0, which is a
distinct cross-platform “branch” of PowerShell. As far as we can tell, everything we teach in this
book applies to Core, too - although some of our specific examples may still only work on Windows
PowerShell, the concepts and techniques are applicable to PowerShell Core.
Introduction x

The Journey
This book is laid out into five parts:

1. A quick overview of “the right way” to write functions.


2. Professional-grade toolmaking, where you amp up your skills, comes next in a second narrative.
This part is less tightly coupled than the first, so you can just read what you think you need,
but we still recommend reading the chapters in order.
3. Moving on from toolmaking for a moment, we’ll cover different kinds of controller scripts that
can put your tools to use. Read these in whatever order you like.
4. Data sources are often a frustrating point in PowerShell, and so this part is dedicated to those.
Again, read whichever ones you think you need.
5. More advanced topics complete the book, and again you can just read these as you encounter
a need for them.

Following Along
We’ve taken some pains to provide review Q&A at the end of most chapters, and to provide lab
exercises (and example answers) at the end of many chapters. We strongly, strongly encourage you
to follow along and complete those exercises - doing is a lot more effective than just reading. And
if you get stuck, hop onto the Q&A forums on PowerShell.org and we’ll try and unstick you. We’ve
tried to design the labs so that they only need a Windows client computer - so you won’t need a
complex, multi-machine lab setup. Of course, if you have more than one computer to play with,
some of the labs can be more interesting since you can write tools that query multiple computers
and so forth. But the code’s the same even if you’re just on a single Windows client, so you’ll be
learning what you need to learn.

Providing Feedback
Finally, we hope that you’ll feel encouraged to give us feedback on this book. There’s a “Contact
the Authors” form on this book’s page⁵ on LeanPub.com, and you’re also welcome to contact
us on Twitter @concentratedDon and @JeffHicks. You can also post in the Q&A forums on
PowerShell.org, which frankly is a lot easier to respond to than Twitter. If you purchased the “Forever
Edition” of this book on LeanPub, then you’ll see us incorporating suggestions and releasing a new
build of the book all the time. If you obtained the book elsewhere, we can’t turn your purchase
into a LeanPub account for you. However, when the book changes enough for us to publish a new
“edition” to other booksellers, that might be a time to pick it up on LeanPub instead, provided you
understand the “Agile publishing” model and are comfortable with it.
⁵http://leanpub.com/powershell-scripting-toolmaking
A Note on Code Listings
The code formatting in this book only allows for about 60-odd characters per line. We’ve tried our
best to keep our code within that limit, although sometimes you may see some awkward formatting
as a result.
For example:

1 Invoke-CimMethod -ComputerName $computer `


2 -MethodName Change `
3 -Query "SELECT * FROM Win32_Service WHERE Name = '$ServiceNa\
4 me'" `

Here, you can see the default action for a too-long line - it gets word-wrapped, and a backslash
inserted at the wrap point to let you know. We try to avoid those situations, but they may sometimes
be unavoidable. When we do avoid them, it may be with awkward formatting, such as in the above
where we used backticks (‘) or:

1 Invoke-CimMethod -ComputerName $computer `


2 -MethodName Change `
3 -Query "SELECT * FROM Win32_Service WHERE Name = '$ServiceName'" `

Here, we’ve given up on neatly aligning everything to prevent a wrap situation. Ugly, but oh well.
You may also see this crop up in inline code snippets, especially the backslash.

If you are reading this book on a Kindle, tablet or other e-reader, then we hope you’ll
understand that all code formatting bets are off the table. There’s no telling what the
formatting will look like due to how each reader might format the page. We trust you know
enough about PowerShell to not get distracted by odd line breaks or whatever.

When you write PowerShell code, you should not be limited by these constraints. There is no reason
for you to have to use a backtick to “break” a command. Simply type out your command. If you
want to break a long line to make it easier to read without a lot of horizontal scrolling, you can hit
Enter after any of these characters:

• Open parenthesis (
• Open curly brace {
• Pipe |
A Note on Code Listings xii

• Comma ,
• Semicolon ;
• Equal sign =

This is probably not a complete list, but breaking after any of these characters makes the most sense.
Anyway, we apologize for these artifacts. Keep in mind that you can, and should, use Install-Module
PowerShell-Toolmaking to download and install the code samples from the PowerShell Gallery.
They’ll end up in \Program Files\WindowsPowerShell\Modules\PowerShell-Toolmaking, typically
broken down by chapter. We do update that download pretty often, so if you don’t have the latest
version installed, do that.
Lab Setup
We hope that you plan to follow along with us in this book, and to help you do so we’ve provided
hands-on exercises at the end of most chapters. To complete those, you won’t need much of a lab
environment - just a Windows 10 (or later) computer to which you have Administrator access, and
which has Internet connectivity. Business editions (not “Home”) of Windows are recommended.
We’ve built the labs so that there’s no need for a domain controller, servers, or anything else. In
this short chapter, we’ll walk you through the lab setup process, just in case you’re building a new
Windows 10 computer and aren’t familiar with that procedure.

This is an intermediate-to-advanced-level course; if you are not already familiar with


installing Windows and accomplishing common administrative tasks, then you may find
this course difficult to follow.

What You’ll Need


Before you begin, you’ll need Windows 10 installation media. This is usually an ISO image; for
the following examples, we will assume it’s called windows10.iso. In reality, it’s probably got a
different filename, so be sure to ensure that you change the filename in the examples below. Also,
make sure that you’re installing a 64-bit edition of Windows 10. Other than that, any business edition
(Professional, Enterprise, etc.) is supported.

Setting Up a Virtual Machine


If you plan to run your lab environment in a virtual machine (VM), you’ll need to set up your VM
host first. You can use a variety of hosts, including Amazon Web Services (AWS), Microsoft Azure,
VMware vSphere, Hyper-V, and so on.

Setting Up a VM on Windows Server Hyper-V


We’ll assume that you’re using Microsoft Hyper-V, on Windows Server, and provide setup instruc-
tions for that. We assume that your host computer has at least 4096MB (4GB) of memory free for
the VM to use. We also assume that the Hyper-V role is already installed and functional on your
server, and that the Windows 10 ISO image is available locally on the server.

1. Launch Hyper-V Manager.


Lab Setup xiv

2. In the left panel, select your server.


3. In the Actions panel on the right, select “Virtual Switch Manager…”.
4. In the Virtual Switches section, select “New Virtual Network Switch”.
5. On the right, select “External” and click Create Virtual Switch.
6. Name the network “Internet Access” and click OK.
7. Click “Yes” to acknowledge the warning. Note that this will disconnect you from the machine
if you are performing the action remotely. Reconnect and proceed.
8. In Hyper-V Manager, select your server in the left panel.
9. From the Action menu, select New > Virtual Machine.
10. On the Before You Begin page click Next.
11. On the Specify Name and Location page, enter “CLIENT”.
12. On the Specify Generation page, select ‘Generation 2’ and click Next.
13. On the Assign Memory page, enter “4096” and click Next. You can assign a lower number, if
needed, but the performance of the VM may be poor.
14. On the Configure Networking page, select “Internet Access” and click Next.
15. On the Connect Virtual Hard Disk page, click Next.
16. On the Installation Options page, select “Install an operating system from a boot CD/DVD-
ROM”.
1. Select the “Image file (.iso):” option and click Browse.
2. Locate the Windows ISO image (windows10.iso, or whatever filename you used) and click
Open.
3. Click Next.
17. On the Completing the New Virtual Machine Wizard page, click Finish.
18. In Hyper-V Manager, right-click CLIENT and select Connect to launch the VM in a console
window.
19. In the Action menu of VM console window, select Start.

Setting up a VM on Windows 10 Hyper-V


Windows 10 supports Hyper-V on computers with a compatible processor. If you have already
installed and enabled both “Hyper-V Management Tools” and “Hyper-V Platform,” then you should
be able to open Hyper-V Manager and set up a VM, using the instructions above for Windows Server
Hyper-V. Note that we recommend your computer have at least 8GB of RAM, so that 4GB can be
reserved for your host, and 3-4GB allocated to the VM.

Installing Windows 10
You can follow these steps whether you’re installing Windows in a VM or on a regular host computer.
This assumes you are installing from a DVD (or equivalent ISO image). Note that these instructions
were written for Windows 10 build 1607; future builds may introduce a somewhat different
Setup process, but are still fully compatible with this course.
Lab Setup xv

1. On the Windows Setup dialog box, select your language, time and currency format, and
keyboard. Then click Next (or press Alt+N).
2. Press Enter or click Install now.
3. You must provide a valid activation key. The activation key determines the edition of Windows
that is installed; we recommend installing either a Professional or Enterprise edition. You can
also click “I don’t have a Product key” to continue installing. If you do so, you may be asked to
choose an edition to install. Again, we recommend Professional (“Pro”) or Enterprise. Be sure
the Architecture states “x64.” We do not recommend using a Home edition.
4. Click Next (or press Alt+N).
5. Select the checkbox to accept the licensing terms, and click Next.
6. Click “Custom: Install Windows only (advanced)”.
7. Select the drive to install Windows on. This will usually be “Drive 0” on a new system or VM.
8. Click Next.
9. Wait while Setup completes.
10. After Setup completes, you may see a “Get going fast” screen. You can click Use Express
settings. Or, if you need to customize the settings described, select Customize. We will assume
you are choosing to use the Express settings.
11. You may see a “Who owns this PC?” screen. Select “I own it” and click Next.
12. Select “Skip this step” (located near the bottom-left of the screen).
13. For “Create an account for this PC,” enter the user name “User”. For the password, enter
“P@ssw0rd” twice, enter “password” for the hint, and then click Next.
14. For “Meet Cortana,” click “Not now”.
15. Wait while Windows sets up your PC.

Adding Lab Files and Configuring PowerShell


We have published all of the lab files for this book on the PowerShell Gallery, to make it easier to
install them.

1. On your Windows computer, press Windows+R, type “powershell”, and press Enter.
2. Right-click the PowerShell icon on the Task Bar, and select Run as Administrator.
3. Click Yes.
4. In the new PowerShell window (which must say “Administrator: Windows PowerShell” in the
title bar), type Install-Module PowerShell-Toolmaking and press Enter.
5. You may be notified that “NuGet provider is required to continue.” Type Y and press Enter.
6. You may be notified of an “Untrusted repository.” Type Y and press Enter.
7. Type Set-ExecutionPolicy Bypass and press Enter.
8. Type Y and press Enter.

If the installation fails, or if you see an error or warning when setting the execution policy, ensure
that PowerShell is running as Administrator and that the computer has unrestricted Internet access.
On a company-owned computer, restrictions may be in place that prevent the installation of files or
the changing of the execution policy. You will need to consult your company’s IT administrators to
remedy that.
Lab Setup xvi

Assumptions Going Forward


Because scripting and toolmaking are not entry-level tasks, we assume that readers are already
aware of the need to run PowerShell “as Administrator” when developing scripts and tools. We
assume a basic level of familiarity with the PowerShell Integrated Scripting Environment (ISE), and
we assume an intermediate or higher level of familiarity with PowerShell itself. If you don’t feel
you meet these expectations, we suggest first completing Learn Windows PowerShell in a Month of
Lunches⁶ available from most booksellers or from Manning.com.
We also assume that your lab computer (or virtual machine) will have Internet access.

⁶https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-third-edition
Part 1: Review: PowerShell
Toolmaking
This first Part of the book is essentially a light-speed refresher of what PowerShell Scripting in a
Month of Lunches covers. If you’ve read that book, or feel you have equivalent experience, then this
short Part will help refresh you on some core terminology and techniques. If you haven’t… well, we
really recommend you get that fundamental information under your belt first.
Another random document with
no related content on Scribd:
The Project Gutenberg eBook of Regényirók
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.

Title: Regényirók
Tanulmányok

Author: Géza Voinovich

Release date: December 14, 2023 [eBook #72409]

Language: Hungarian

Original publication: Budapest: Franklin-Társulat, 1921

Credits: Albert László from page images generously made available


by the Hungarian Electronic Library

*** START OF THE PROJECT GUTENBERG EBOOK


REGÉNYIRÓK ***
KULTURA és TUDOMÁNY

REGÉNYIRÓK

TANULMÁNYOK

IRTA VOINOVICH GÉZA

BUDAPEST, 1921

FRANKLIN-TÁRSULAT
MAGYAR IROD. INTÉZET ÉS KÖNYVNYOMDA

KIADÁSA

REGÉNYIRÓK

TANULMÁNYOK
IRTA
VOINOVICH GÉZA

BUDAPEST, 1921

FRANKLIN-TÁRSULAT
MAGYAR IROD. INTÉZET ÉS KÖNYVNYOMDA

KIADÁSA
Gogoly 5
Lermontov 18
Goncsárov 36
Turgenyev 50
Tolsztoj 61
Dosztojevszkij 81
Björnson Björnstjerne 116
Scott Walter 131
Bulwer 147
Thackeray 159
Keller Gottfried 173
Meyer Konrád Ferdinánd 190

FRANKLIN-TÁRSULAT NYOMDÁJA.
GOGOLY.

Az orosz irodalom kezdődő fénykorában, a mult század huszas


és harminczas éveiben, a nyugat-európai mozgalmak hatása alatt,
egész sereg költő és író hirdette lelkesülten, hogy az orosz nem
csupán számos, hanem nagy nemzet is. A szláv életközöny és
fásúltság akkor, s az idő azóta mégis azoknak adott igazat, a kik
csüggedten szemlélték a muszka jellemet és mélabúval az orosz
életet. Ezek voltak a mélyrelátó és komoly hazafiak, s egyúttal a
valódi nagy tehetségek. Nem számítva a tisztán lyrikus Kolzovot, a ki
az élet nyomorúságait emberi általánosságban panaszolja, ennek a
nemzeti pessimismusnak adtak hangot a költészetben Puskin és
Lermontov, a prózában Gogoly.
Kortársak voltak. Puskin mindössze tíz esztendővel volt korosabb
Gogolynál, Lermontov öttel fiatalabb. Puskin nevét már országszerte
ismerték, mikor Gogoly-Janowszkij Wasziljewics Miklós 1829-ben,
mint húsz esztendős fiatal ember, Szent-Pétervárra ment szerencsét
próbálni, nem is álmodva arról, hogy valaha Puskinnak barátja s
hírnevének osztályosa lehessen. Nem is sejtette, mennyi tehetség
szunnyad benne, s hogy lelkében milyen szerencsésen vegyül össze
a nehézkes orosz komolyság a kisoroszok nyilt és tüzes
természetével. Megvolt benne a szláv mélabú és révedezés, az
örökös leigázottság nyomában járó fásúltság, mely tompa
keserűsegben emészti magát s a hamu alatt mégis ízzik. De a
Dnjeper fölött járó nap tüzet cseppentett a vérébe s Ukraina
verőfényétől lelkének zordonsága is földerengett, mint a jég tükre.
Nagyapja hetman volt a lengyelverő zaporogi kozákok közt; e vad
hordákról hallgatott meséket gyermekkorában; képzeletében ezek a
szilaj hősök száguldoztak, első benyomásai e buja föld képeit
vázolták emlékezetébe, s e népfaj mondáit, melyeket más író nem
tarolt le előtte. A thémákon és vérmérsékletén kívül még mást is
köszönhet kisebb hazájának: azt a hol lelkes, hol meg elégikus mély
érzést, mely áttüzel efajta elbeszélésein, s azt az elfogulatlanságot,
melylyel ezzel szemben a nagy Oroszország életét és népét
szemléli.
Mint a hogy’ a mesék kincséhez csak lekaszabolt sárkányokon át
juthatni, a magunk képességei is olyan mélyre vannak rejtve
önmagunk előtt is, hogy csak küzdelmek és erőfeszítések árán
akadhatunk rájok. Gogolynak, mikor Szent-Pétervárra ment, meg
sem fordult az eszében, hogy szülőföldje járatlan vidékének képei s
ismeretlen népfajának hagyományai kész írói anyag, s hogy az a
rendkívüli megfigyelő- és leíró-képesség, mely eddig anyjának írt
leveleiben kallódott el, nagyobbra termett. Határozott becsvágygyal
és határozatlan tervekkel érkezett fel Pétervárra, melyek dúsan
nőnek minden húsz éves fiatal ember lelkében, a tapasztalatlanság
televény-földjén. Csak miután egy álló esztendeig görnyedezett
egyik minisztérium hivatalszobájában, a hol a sok köhögős, szegény
irnok közt talán ott kuksolt az együgyű jámbor Akakiévics is; miután
megpróbálkozott a szinészkedéssel, a mihez nem volt elég hangja, s
gyötörte magát nevelősködéssel mágnás-családoknál: csak azután
sugallta neki a folytonos elégedetlenség azt a gondolatot, hogy
ujságczikkeket próbáljon írni. Talán még ekkor sem lett volna belőle
író, ha Puskin nem biztatja, a kinek felötlöttek névtelenül megjelent
legelső czikkei.
Írói tehetségének az a legfényesebb bizonyítéka, hogy első két
kötetének egypár darabja azóta sem vesztette színét, nem fakult ki.
Egészen érthető volna, ha Turgenyev, Tolsztoj, Csehov mesteri apró
elbeszélései után kezdetlegeseknek találnánk az ő régibb vázlatait
és rajzait. De Gogolynak írói alakját nem törpíti el ilyen optikai
csalódás. Bulyba Tárász-ról Guizot azt mondotta, hogy a XIX.
században ezen kívül nem ismer epopéiát, mely érdemes volna erre
a névre. Csakugyan, a régi kozákvilág éposa ez az elbeszélés, még
pedig marczona hőseinek szilajságát és faragatlanságát, az
erkölcsök nyerseségét, a kegyetlen törvényeket és durva szokásokat
tekintve, szinte homéri eposa. Megvan benne a regemondó naiv hite
és közvetlensége is, hiszen hallomásból, nagyapja és apja meséi
nyomán írt, alig ötven esztendővel az után, hogy a kozák hordák
utoljára sarczoltak lengyel földön. A fényűző lengyelek úgy vannak itt
rajzolva, hogy fölelevenednek előttünk a Matejko fényes alakjai; a
kozákok meg úgy, mintha Rjepin innen válogatott volna modèle-eket.
Shakesperei méretek szerint vannak mintázva ezek az alakok.
Bulyba a kozák-hősiség mintaképe: fiait hadra neveli, az ellenségre
viszi őket; tulajdon kezével oltja el életét a kisebbiknek, mikor azt
egy szép szempár a lengyelek pártjára vonzza; végignézi a
nagyobbiknak kivégzését, a mint a lengyelek összetörik csontját;
csak kis csapatának menekülésével gondol, s tanácsokat kiabál
utánok, mikor végezetül őt magát elevenen megégetik. Igazán, akkor
sem lehetne ez az epos véresebb és kegyetlenebb, ha magában a
kozákok táborában támadt volna. Az emberi és írói erő a kép minden
egyes vonásában magára ismer. Ha Gogoly hozzálágyúl az anyai
szív aggodalmainak és búbánatának rajzához, csak szomorúságát
és csüggedését érti meg, s olyan sötét képet rajzol, mint a Bulyba-
fiúk szülőanyjáé, mikor elnézi fiait a hadrakelés éjszakáján, s titkon,
elfojtva siratgatja őket. Csak a lehangoltságban érez közösséget
alakjaival. Itt van tehetségének egyik határa. A női szívbe
egyesegyedül csak a szomorúság ajtaján át van bejárása. Az
álmokat és zsendülő szenvedélyeket nem ismeri; a lengyel hajadon
rajza, a ki felgyujtja Bulyba ifjabb fiának a szívét, olcsó olajnyomat.
Általában a szomorúság embere. Abban a kötetben, melyben
Bulyba Tárász 1834-ben megjelent, van egy másik elbeszélés is, A
régi jó időkből. Egy öreg pár rajza ez, kiket a megszokás erősebben
fűz össze a szenvedélynél. A két összetöpörödött kis emberke a
sárga szalma sustorgó tüze mellett a túlfütött, alacsony szobában, a
hol egymást éri a sok almáriom és láda, melyek közébe kisimított
papirok és összegöngyölgetett vászondarabkák vannak dugdosva,
falain meg vékony rámájú, foltos, ódon aczélmetszetek függnek,
felváltva kis zacskókkal, melyekben virág-, dinnye- és ugorkamagvak
vannak eltéve jövőre: mindez olyan, mint valami ócska bábszínház
pókhálós, porlepett alakjai és requisituma, melyet félrelöktek a
padlás egy zugában, s melylyel régesrég nem játszott senki. Daudet
is lefestett a Lettres de mon moulin skizz-könyvében egy ilyen öreg
házaspárt, a bolthajtásos kis szobában, melynek ablakán betolong a
nyári napsugár s a kis kert és a mezők illata; a két öreg szunyókál,
egy kis árvalány sillabizálva olvas fel a szent könyvből, hogy a
legyek is elszundítanak tőle az asztalon s az ablak sarkában. Daudet
csak a szemével nézte őket, s megmosolyogta a vén ember
reszketeg legénykedését, mikor sipkáját félrecsúsztatta a fején,
miközben vendégét kikisérte. Daudetnak mindez azért tetszett, mert
új és szokatlan volt előtte, s a maga hangulatán át mutatta meg
alakjait; Gogoly szemlátomást régi ismerőse ennek a világnak;
együtt érez embereivel, s még az olvasója is érzi, hogy ő maga is
ebből a fajtából való, ezek közül a csüggedt, szűkszavú emberek
közül, a kiknek szürke élete hosszú öregedés és csöndes halálra
várás; s éppen higgadt leírásával kelt hangulatot. Humora nem
alakjaival incselkedik, hanem felénk fordul, mintha azt mondaná:
Ime, milyen gyötrelmes semmiség az ember tengődése, és ezt
életnek hívjátok, végigélitek és ragaszkodtok hozzá.
Ez az elbeszélés új világot nyitott meg Gogoly előtt: a múltak
mondái helyett az egész való életet, a hatalmas és apró
embereknek, a kevélyeknek és lealázottaknak örökös
herczehurczáival, naponként megújuló reményeikkel és
mindennapos csalódásaikkal, mindazt a türelmet, törekvést,
fájdalmat, az egész állandó összevisszaságot, melyet egy szóval
életnek nevezünk. S rávezette ez az elbeszélés arra a módra is, a
melylyel ezt a világot fényképezni kell: rávezette megfigyelő és
részletrajzoló tehetségére. A mindenséget, az élet nagy problémáit
látta maga előtt fölnyitva; szakított régi terveivel, odahagyta
egyetemi kathedráját, a hol három leczkeóra közül addig is egyen ha
megjelent, hogy várak és városok régi fametszetű képeinek
mutogatásával s lassan döczögő beszélgetésével kihúzza az órát.
1835-ben vidáman írja egy levelében: «Megint szabad kozák
vagyok». Essayket, darabot, elbeszéléseket ír, ezek közt
leghíresebb kis munkáját, A köpönyeget, melyet nem kisebb ember
fordított magyarra, mint Arany János, a ki czikkei alatt is nem
egyszer vette magára álnevűl a szerény vágyaiban is csalódott
Akakievics Akaki köpönyegét.
Egy kis tragédia ez az elbeszélés, egy szegény irnok tragédiája,
a ki egész életét másolással töltötte; nem voltak más ismerősei a
betűkön kívül, melyek közül egynémelyikre, valahányszor egymásra
akadtak, állandó rokonszenvvel nézett, s az ilyeneket
összecsucsorított szájjal, különös gonddal írta le. Ezt a szegény
tengődőt is fölkereste egyszer a vágy jótékony tündére, ha csak
élete vége felé s egy melegebb új köpönyeg képében is, de oly
lelket-betöltőn, akár Macbethet a korona álma, s midőn e tömérdek
zsugorgás árán szerzett gallért első estve elrabolják válláról, a
balsors is olyan erővel, halálosan csap le rá, akárcsak a
hatalmasokra. Ma az apró ember apró szenvedése, mint Péterfy
mondja, irodalmi verkli-ária, de a Gogoly idejében az ilyen théma és
felfogás éppen annyira ujság volt, mint az az aprólékos realismus,
melylyel azt az élet másává tette. Ezért a kettős érdeméért tartotta őt
Merimée a legnagyobb orosz írónak, s Vogüé kétszeresen is
elmondhatja, hogy az egész későbbi orosz irodalom ebből a
köpönyegből szakadt ki.
Gogoly itt is méla szomorúsággal nézi a maga emberét, s ironiája
és gúnyja a társadalmat, Oroszországban hát elsősorban a
hivatalnok-osztályt illeti, a mely hatalmaskodón, bárgyúvá
dolgoztatja az együgyű munkás-embert, de ahhoz sem esze, sem
komolysága, sem ereje nem elég, hogy ennek a szerzeményét
megvédelmezze.
Gyűlölte az orosz hivatalnok-osztályt, hiszen ismerte. Úgy látta,
hogy a sors náluk a nép türelméből és elnyomóinak czudarságából
fonja ostorát. Ki is mondta ezt vígjátékában, A revizor-ban, mely
kétségbeejtette az egész orosz hivatalnoki kart. El is tiltották volna
előadását, de Miklós czár elolvasta a darabot, mulatott rajta, végig is
nézte és megtapsolta, akármilyen savanyú arczot vágtak hozzá a
kormányzók és hivatalnokaik, mikor a nép fülük hallatára hahotázott
rajtok. A darab azt mutatta meg, hogy Oroszországban a hivatalnok-
sereg a nép sorsa, és hogy milyen ez a sereg és ez a sors. A
közönség előtt feltárultak a hivatalnokok lakásai; a kórházigazgatók
maguk kifecsegték, hogy elvonják a betegektől a falatot és a takarót;
a kormányzók magok bevallották, hogy ok nélkül korbácsoltattak
meg özvegyeket; a postaigazgatót rajtaérjük, a mint felbontogatja a
leveleket, s világos az ostobasága az egész hivatali csőcseléknek,
mely maga löki magát a verembe, mikor abban a sejtelemben, hogy
egy szép napon revizor fog a kormányzóságba toppanni, egyszerre
egy átutazó fiatal emberre kezd gyanakodni, a ki beletalálja magát a
tréfába, a kormányzóhoz költözködik, átdőzsöl nagy lakomákat,
végighallgatja, a mint a hivatalnokok rendre befeketítik egymást,
megvesztegetteti magát mindegyiktől, s szerencsésen odább áll,
mikorára az igazi revizor, mindnyájok rémületére, megérkezik.
Erre a torzképre Gogoly igaz lélekkel illesztette oda mottóul ezt:
«Ne a tükörre vess; a mit látsz, ten ábrázatod». A darab csakugyan
az élet képe; az író mindössze annyi nagyítást engedett meg
magának, a mennyi ahhoz szükséges, hogy a mit mondani akart, a
színpad lámpasorán át s a könyv csendjéből is az élet erejével
hasson, s hogy keserű utóízét valami mosolylyal enyhítse, a mi csak
növelte a darab hatását és népszerűségét, mely máig sem igen
apadt meg. Oroszországban az irányzatos irodalmi munkáknak
megvan az a szomorú szerencséjök, hogy nem avulnak el egy
század alatt sem.
A Revizor előleg volt Gogolynak egy nagyobb tervéből, melyben
egész Oroszországot akarta megírni, a népet és a vezetőket,
megmutatva, milyenek ezek, s milyenné teszik a nép sorsát s a
népet magát. Ő maga azt mondja, Puskin biztatta erre a nagy
munkára, azzal sarkallva, hogy Cervantes neve sem maradt volna
fenn, pusztán gyönyörű elbeszélései révén, ha Don Quixote-t meg
nem írja, Vogüé meg azt bizonyozza, hogy egyenesen Cervantes
hatott reá, a kinek munkáit rendkívül megszerette Spanyolországban
jártakor. Az bizonyos, hogy Gogoly könyveinek is nemzete,
nemzetének lelke a hőse. Hogy Cervantes a spanyoloknak idejöket
múlt kósza ábrándjait figurázta ki a föllengző hóbortok lovagjában,
Gogoly meg a Holt lelkek-ben nemzetének éppen földhöz-
ragadtságát, elmaradottságát, tudatlanságát, életközönyét,
elfásultságát állítja pellengére; hogy mindkettejök mosolya a
gondolkodó kesernyés gúnyját s a hazafi elkeseredését leplezi, de a
spanyol verőfény ebből is virágokat pattant ki, az orosz klima pedig
tüskéket; hogy Cervantes munkája igazság szerint a végtelenségig
nyújtható kalandos epos, s Gogoly is költeménynek nevezte
regényét, s fejezeteit énekeknek; hogy Cervantes nemzete
rajongását a nyurga vitézben, a nyers józanságot pedig tömzsi
kisérőjében személyesítette meg, míg Gogoly inkább a
mellékalakokban fejezte ki nemzete szellemét; hogy Cervantes
elhamarkodva végezte be könyvének utolsó részét, mert valami
irodalmi portyázó a folytatással elébe vágott, Gogoly pedig csak az
elsőt írhatta meg a tervezett három rész közül: mindez már
hiábavaló erőszakolása a párhuzamoknak.
Az bizonyos, hogy Gogoly mindazt a tömérdek arczképet, melyet
az első részben egymás mellé aggatott, élet után festette, csakúgy,
mint magát a főalakot. Gogoly idejében valósággal megtörtént, hogy
valami szédelgő potom pénzen összevásárolta a gazdáktól a holt
jobbágyokat, a kiknek a neve még nem volt kitörölve a reviziós
lajstromból, erre az értékre pénzt vett fel, s már éppen jószágot akart
vásárolni, a hol megtelepedjék és tisztességben éljen, mikor a
csalás kiderült s szerzője börtönbe jutott. Ez a kalandor éppen
kapóra jött Gogolynak. Ha egy ilyen országkóborlót végig kisér
jártában-keltében, leírhatja Oroszországnak minden vidékét és
minden fajta emberét. Megfordúl pusztuló tanyákon és
mintagazdaságokban; találkozik iszákos Nozdrevekkel, gyámoltalan
semmittevőkkel, a szervező mania fanatikusaival, rongyoskodó
zsugoriakkal és hitelre dinom-dánomozókkal, a kiknek a hasuk az
istenük; belepillanthat a Téntétnikovok nembánomságába, ebbe a
csökönyös orosz vonásba, mely minduntalan visszatér
Dosztojevszkij és Turgenyev alakjaiban épp úgy, mint a Tolsztoj
Bezuhovjában és Levinjében; megfigyelheti az embereket a kényes
pillanatokban, mikor ráismernek vagy nem ismernek rá a szédelgőre,
az alku leplezetlen perczében, a mint belecsapnak a markába,
egyesek gyanakodó óvatossággal, mások gyámoltalan örömmel,
némelyek kapzsin. Benyithat a kormányzósági hivatalszobákba, a
hol az adásvételi szerződéseket kötik; észreveheti, hogy’ járnak
kedvében a főnökök a pénzes embereknek, hogy’ lesnek egy kis
megvesztegetésre az alantasabb hivatalnokok. Magában ebben az
első részben, mely óriásivá nőtt praeludiuma egy el nem készült
colossalis regénynek, egy városra menő embertömeget jellemez,
úgy, hogy még a gestusaik is mások és mások.
Maga Csicsikov, a lélekvásárló, typikus orosz. Pénz és rang
nélkül, abban a világban csak alkalmazkodással és csalással
boldogul. Simulékony, számító és hízelgő; csodamódra bírja a
fáradságot, a sikerért mindent eltűr, a boldogulásért mindent
koczkáztat.
Puskin, mikor az első fejezeteket végighallgatta, fölsóhajtott: –
Be’ szomorú az ön Oroszországa! – Az a bizonyítvány, melyet
Gogoly az életről és az emberek erkölcséről kiállít, csakugyan
nagyon lehangoló. A mint a Csicsikov trojkája végig csilingel a rossz
országút mentén, nyomorult falvakon át, düledező jobbágyviskók
előtt, bámészkodó emberek közt, a kiknek az életében esemény, ha
egy telega elzötyög a házuk előtt, s a kiket a gazdáik adnak-
vesznek, az a gondolat lankaszt el, hogy az egész civilisatiót csak
egypár százezer ember csinálja egymás kedvéért s a többieknek
nincs benne részök. Az emberek ki vannak szolgáltatva a mások
eszének és a saját szenvedélyeiknek; a rosszra csábítják, a jóra
csak rábeszélik egymást; tisztességtelen úton keresik a
gazdagságot, hogy azután tisztességesen élhessenek.
Nem csoda, hogy a közönség fölháborodással fogadta a regényt,
mikor 1842-ben megjelent. Gogoly maga is megsokallt ennyi
kietlenséget, s hozzáfogott a második részhez, melyet enyhébbnek
szánt. De a milyen lassan dolgozott, a hosszú munka alatt írói ereje
cserben hagyta, gondolkodása is megváltozott. Az első részt
külföldön írta, hová a harminczas évek derekán szakadt. Sokáig élt
együtt Rómában s a Soracte alján egy Ivanov nevű festővel, a ki már
akkor húsz esztendeje dolgozott egy soha el nem készült Krisztus-
képen. Ivanov rajongása vallásos hallucinatiokig csigázta Gogoly
vakbuzgóságát. A Szent-földre készült zarándok-útra, s hogy a
szükséges pénzt megszerezze, szemelvényeket adott ki barátaival
való levelezéséből, melyben a censura szükségét, a zsarnoki
hatalom hasznos voltát s a rabszolgai megalázkodás kötelességét
hirdeti. A legkomolyabb kritikusok, Herzen, Belinszky álmélkodása
és támadásai ellen azzal védekezett, hogy részleteket olvasott föl
Arabeszkek czímen összegyűjtött első czikkeiből, melyekbe
belemagyarázta ez új fölfogásának csiráit. Így látta őt jeruzsálemi
útja után, 1851-ben, utóljára Turgenyev, a ki ezt a találkozást föl is
jegyezte emlékezései közt. Gogoly köpczös alakja megfogyott,
ravasz-vágású hosszas orra megnyúlt, szúró szeme folyvást
röpködött, rendszerint összeszorított ajkai beszédesekké váltak, kis-
oroszosan halántékára simított szőke haja megszürkült és
megritkult. A Holt lelkek-et említeni sem volt szabad előtte; a
megkezdett második részt félretette, s halála estéjén, 1852.
februárius 21-én – az ó-naptár szerint, – egyéb írásaival együtt el is
égette. Egy másolat őrizte meg, a mennyi elkészült volt belőle.
Így csonkán is a nagy regények egyike a Holt lelkek. Nemcsak
azért, mert egy falu népességét kitevő embertömeget egyénenként
elevenen és találón jellemzett, olyan ügyességgel, hogy e nagyrészt
ellenszenves emberek iránt fel bírta gerjeszteni egész
érdeklődésünket; hanem, mert ezeken át bámulatos írói erővel
megrajzolta, sőt meg is magyarázta az egész orosz nép jellemét és
lelkét. A mellett valamennyi orosz író Gogolyhoz járt iskolába; ő volt
az első nagyszabású regényíró orosz földön. Az olvasónak is nála
kell kezdeni az orosz irodalom megismerését; az ő jellemképeiből
ismerheti meg az idegen olvasó az orosz typusokat épp úgy, mint az
orosz realismust. Alkotásai méltán mértékei lehetnek a többi írók
munkáiban rajzolt arczképek élethűségének, s egyben az írók írói
erejének is.
LERMONTOV.

Lermontovról egypár kitűnő írott arczkép maradt reánk,


melyeknek nagy hasznát veszszük. Ezekből kell kiolvasnunk
életrajzát is, mert legmunkásabb éveit az emberek szemétől távol, a
Kaukázusban töltötte s oly fiatalon tünt el, hogy – mint egyik
életrajzírója mondja – a kortársakanak idejük sem volt kivallatni, hol
és mikor tanult a fiatal lángész declinálni és conjugálni.
Turgenyev kétszer látta: valami álarczosbálon, a hol közömbös
arczczal, unottan állt a körülte sereglő masque-ok között; másszor
egy elfogadó-teremben, a testőrhuszárok egyenruhájában,
kardosan, keztyűsen ülve egy alacsony tabouretten, valamelyik
pétervári szépség előtt, kinek nevetésére álmélkodva, szinte
sértődötten nézett föl. «Volt rajta valami tragikai és baljóslatú. A
pessimismus és szenvedélyesség, mint valami félelmes daemoni
hatalom, borongott napbarnított arczán; nagy szemeinek sötét
tekintete különös ellentétben volt ajkainak gyermekesen gyöngéd és
nyájas kifejezésével. Zömök alakja, nagy feje és széles vállai nem
tették megnyerővé, ha lehetetlen volt is meg nem látni, hogy kiváló
ember».
E kép a milyen eleven, bizonyára oly élethű is, mert ámbár
Turgenyev öreg korában vázolta fel visszaemlékezéseinek egy
lapjára, annak idején bizonyosan a kezdő író mohó kiváncsiságával
véste lelkébe a hírneves költő vonásait.
A kép hitelességéről különben teljesen meggyőz egy másik,
részletesebb rajz. Bodenstedt 1840–41 telén Oroszországban járva,
egy ízben előkelő fiatal oroszokkal ebédelt; egyszerre új vendég
nyitott közéjük. «Urias, fesztelen magatartású, középtermetű s ritka
mozgékonyságú férfi volt. A mint zsebkendőjét elő akarva venni,
hogy deres bajúszát megtörölje, kirántotta tárczáját, oly hajlással
nyúlt utána, mintha minden csontja össze lett volna törve, pedig a
mellből és vállból ugyancsak erős csontokat lehetett gyanítani. Síma,
szőke, kétfelől könnyen bodrozott haja a rendkívüli magas homlokot
egészen szabadon hagyta. A nagy, lelkes szemeknek, úgy látszott,
semmi részük sincs a gúnyos kifejezésben, mely a finom metszésű
száj körül libegett». Az új vendég, kinek finom, gondosan ápolt kezei
különösen feltűntek Bodenstedtnek, egész este szórta az élczet és
gúnyt, oly szaporán, hogy később ugyancsak igyekeznie kellett
kiengesztelni megsértett fiatalkori barátait. «Belépte óta az egész
mulatság úgy hangzott fülembe, – írja a szelíd német, – mintha
valaki üveget karczolászna». Pedig «tudott szelíd, gyengéd lenni,
mint a gyermek s általában mély, melancholikus hangulat uralkodott
rajta. E mély komolyság lelkének alapvonása, melyekkel könnyebb,
élczes költeményei oly viszonyban vannak, mint ajka körül ama
gúnyos vonás, nagy, lelkes szemeivel».
Ez az arczkép is biztosan hű és találó, hiszen a lelkiismeretes és
szorgos fordító már akkor ismerte és csodálta Lermontovnak 1840-
ben megjelent kötetét s a költő annyira érdekelte, hogy később
összes munkáit lefordította, sőt a csak kéziratban elterjedtekből
gazdagabb gyűjteményt adott a németek kezébe az eredeti orosz
kiadásoknál.
E portrait-k olyan gondosan vannak festve, hogy ezek nyomán
felismerjük egy harmadik arczkép vonásait.
«Középtermetű ember volt. Deli, karcsú termete és széles vállai
oly erős szervezetre vallottak, a mely képes elviselni a barangoló
élet viszontagságait s a melyet nem tettek tönkre sem a fővárosi élet
kicsapongásai, sem lelki viharok… Keztyűi olyanok voltak, mintha
külön megrendelésre varrták volna apró, arisztokratikus kezeire…
Mikor leült, egyenes termete meghajlott, mintha egy darab csont
sem lett volna a hátában, testének egész alkotása valami ideges
gyöngeséget mutatott… Bőre nőies finomságú volt; természettől
göndör, szőke haja festőien lengte körül halvány, nemes homlokát, a
melyen csak hosszabb vizsgálat után lehetett észrevenni a ránczok
nyomait… Szemei nem nevettek, mikor ő maga nevetett. Ez vagy
rosszlelkűség, vagy mélységes, állandó búbánat jele».
Ezt az arczképet maga Lermontov festette, a Korunk hősé-ben
és Pecsorin nevet írt alája. Ez arczmás vonásról-vonásra annyira
összevág Turgenyev és Bodenstedt vázlataival, hogy a biróság sem
állapíthatná meg hitelesebben a személyazonosságot. Nyilvánvaló,
hogy Pecsorin = Lermontov maga.
Lermontovnak ez a képe olyan, mint Wilde regényében a Dorian
Gray képemása, a mely folyton-folyvást magára ölti eredetijének, az
élő alaknak minden vonását. Pecsorin azt mondja a maga
fiatalságáról: «Mikor a szülői felügyelet alól kikerültem,
nekiszilajodva kezdtem élvezni mindazokat a gyönyöröket, melyeket
pénzért megvehetni – s természetesen hamar beteltem velök. Aztán
belevetettem magam a társaséletbe s hamar megúntam azt is;
szerelmes voltam nagyvilági szépségekbe s azok is szeretgettek
engem… Elkezdtem olvasgatni, tanulni, de a tudományokat is
megúntam… Akkor aztán rettenetesen unatkoztam. Nem sokára
áthelyeztek a Kaukázusba, ez volt életem legnagyobb öröme. Azt
reméltem, hogy az unalom nem ölhet meg a csecsencz-golyók
záporában, – de csalódtam, – egy hónap alatt úgy megszoktam a
golyók süvöltését, a halál közelét, hogy jobban féltem a
szúnyogoktól és még sokkal inkább úntam magamat, mint azelőtt…
Bolond vagyok-e vagy gonosztevő, nem tudtam; de annyi szent,
hogy megérdemeltem a szánalmat».
E sorokat Lermontov életrajzírója szóról-szóra lemásolhatná,
csak annyit kellene hozzátennie, hogy a szülői felügyelet
tulajdonképpen nagyanyai volt, mert az árva kétéves kora óta annál
nevelkedett; hogy a szilaj életet – társaival együtt – már a nemes
úrfiak nevelőintézetében megízlelte, valami efféle csiny miatt oda is
kellett hagynia az iskolát s ez a tört pálya sodorta azután a katonai
életre. Megférhetlen természete ott is bajba keverte s egy időre
fogságba jutott. A fiatal gárdistának a kaukázusi sereghez küldetése
enyhébb száműzetés számba ment, melynek oka először Puskin
halálára írt ódája volt, 1841-ben pedig valami párbaj, a történetíró de
Barante fiával. Önjellemzésének benső meghasonlásra valló

You might also like