the powershell scripting toolmaking book

3,323 613 6MB

English Pages [548]

Report DMCA / Copyright

DOWNLOAD FILE

the powershell scripting toolmaking book

Table of contents :
Table of Contents
About This Book
Dedication
Acknowledgements
About the Authors
Additional Credits
Foreword
Feedback
Introduction
Pre-Requisites
Versioning
The Journey
Following Along
Providing Feedback
A Note on Code Listings
Lab Setup
Create a Virtualized Environment
Use the Windows 10 Sandbox
Adding Lab Files and Configuring PowerShell
Assumptions Going Forward
Part 1: Review PowerShell Toolmaking
Functions, the Right Way
Tool Design
Start with a Command
Build a Basic Function and Module
Adding CmdletBinding and Parameterizing
Emitting Objects as Output
Using Verbose, Warning, and Informational Output
Comment-Based Help
Handling Errors
Are You Ready
PowerShell Tool Design
PowerShell Tools Do One Thing
PowerShell Tools are Testable
PowerShell Tools are Flexible
PowerShell Tools Look Native
An Example
Your Turn
Let's Review
Start with a Command
Your Turn
Let's Review
Build a Basic Function and Module
Start with a Basic Function
Create a Script Module
Pre-Req Check
Running the Command
Your Turn
Let's Review
Adding CmdletBinding and Parameterizing
About CmdletBinding and Common Parameters
Accepting Pipeline Input
Mandatory-ness
Parameter Validation
Parameter Aliases
Your Turn
Let's Review
Emitting Objects as Output
Assembling the Information
Constructing and Emitting Output
A Quick Test
Your Turn
Let's Review
An Interlude: Changing Your Approach
The Critique
Our Take
Summary
Using Verbose, Warning, and Informational Output
Knowing the Six Channels
Adding Verbose and Warning Output
Doing More With Verbose
Informational Output
Your Turn
Let's Review
Comment-Based Help
Where to Put Your Help
Getting Started
Going Further with Comment-Based Help
Broken Help
Your Turn
Let's Review
Handling Errors
Understanding Errors and Exceptions
Bad Handling
Two Reasons for Exception Handling
Handling Exceptions in Our Tool
Handling Exceptions for Non-Commands
Going Further with Exception Handling
Deprecated Exception Handling
Your Turn
Let's Review
Basic Debugging
Two Kinds of Bugs
The Ultimate Goal of Debugging
Developing Assumptions
Debugging Tool 1: Write-Debug
Debugging Tool 2: Set-PSBreakpoint
Debugging Tool 3: The PowerShell ISE
Debugging Tool 4: VS Code
Your Turn
Let's Review
Verify Yourself
The Transcript
Our Read-Through
Our Answer
How'd You Do
Part 2: Professional-Grade Toolmaking
Going Deeper with Parameters
Parameter Position
Validation
Multiple Parameter Sets
Value From Remaining Arguments
Help Message
Alias
More CmdletBinding
A Demonstration
Your Turn
Let's Review
Advanced Function Tips and Tricks
Defining an Alias
Specify Output Type
Adding Labels
Use Your Command Name Programmatically
ArgumentCompleter
Dynamic Parameters
Declaring Dynamic Parameters
Using Dynamic Parameters
Let's Review
Writing Full Help
External Help
Using Platyps
Supporting Online Help
``About'' Topics
Making Your Help Updatable
Your Turn
Let's Review
Unit Testing Your Code
Starting Point
Sketching Out the Test
Making Something to Test
Expanding the Test
But Wait, There's More
Your Turn
Let's Review
Extending Output Types
Understanding Types
The Extensible Type System
Extending an Object
Using Update-TypeData
Next Steps
Advanced Debugging
Getting Fancy with Breakpoints
Getting Strict
Getting Remote
Let's Review
Command Tracing
Getting in PowerShell's Brain
Analyzing Your Script
Performing a Basic Analysis
Analyzing the Analysis
Your Turn
Controlling Your Source
The Process
Tools and Technologies
Let's Review
Converting a Function to a Class
Class Background
Starting Point
Doing the Design
Making the Class Framework
Coding the Class
Adding a Method
Making Classes Easy To Use
Wrapping Up
Publishing Your Tools
Begin with a Manifest
Publishing to PowerShell Gallery
Publishing to Private Repositories or Galleries
Your Turn
Let's Review
Part 3: Controller Scripts and Delegated Administration
Basic Controllers: Automation Scripts and Menus
Building a Menu
Using UIChoice
Writing a Process Controller
Your Turn
Let's Review
Graphical Controllers in WPF
Design First
WinForms or WPF
WPF Architecture
Using .NET
Using XAML
A Complete Example
Just the Beginning
Recommendations
Your Turn
Let's Review
Proxy Functions
For Example
Creating the Proxy Base
Modifying the Proxy
Adding or Removing Parameters
Your Turn
Let's Review
Just Enough Administration: A Primer
Requirements
Theory of Operation
Roles
Endpoints
Let's Review
PowerShell in ASP.NET: A Primer
Caveats
The Basics
Beyond ASP.NET
Part 4: The Data Connection
Working with SQL Server Data
SQL Server Terminology and Facts
Connecting to the Server and Database
Writing a Query
Running a Query
Invoke-Sqlcmd
Thinking About Tool Design Patterns
Let's Review
Review Answers
Working with XML Data
Simple: CliXML
Importing Native XML
ConvertTo-Xml
Creating native XML from scratch
Your Turn
Let's Review
Working with JSON Data
Converting to JSON
Converting from JSON
Your Turn
Let's Review
Working With CSV Data
I Want to Script Microsoft Excel
Know Your Data
Custom Headers
Importing Gotchas
Your Turn
Let's Review
Part 5: Seriously Advanced Toolmaking
Tools for Toolmaking
Editors
3rd Party
PowerShell Community Modules
Books, Blogs and Buzz
Recommendations
Measuring Tool Performance
Is Performance Important
Measure What's Important
Factors Affecting Performance
Key Take-Away
PowerShell Workflows: A Primer
Terminology
Theory of Execution
A Quick Illustration
When to Workflow
Sequences and Parallels are Standalone Scopes
Workflow Example
Workflow Common Parameters
Checkpointing Workflows
Workflows and Output
Your Turn
Let's Review
Globalizing Your Tools
Starting Point
Make a Data File
Use the Data File
Adding Languages
Defaults
Let's Review
Using ``Raw'' .NET Framework
Understanding .NET Framework
Interpreting .NET Framework Docs
Coding .NET Framework in PowerShell
Loading Assemblies
Wrap It
Your Turn
Let's Review
Scripting at Scale
To Pipeline or not
Foreach vs ForEach-Object
Write-Progress
Leverage Remoting
Leverage Jobs
Leverage Runspaces
Design Considerations
Your Turn
Let's Review
Scaffolding a Project with Plaster
Getting Started
Plaster Fundamentals
Invoking a Plaster Template
Creating a Plaster Module Template
Creating a Plaster Function Template
Integrating Plaster into your PowerShell Experience
Creating Plaster Tooling
Adding Auto Completion
ValidateSet
Argument Completer Attribute
Advanced Argument Completers
Your Turn
Let's Review
Adding Custom Formatting
Format.ps1xml
Define a TypeName
Defining a View Definition
Update-FormatData
New-PSFormatXML
Adding to a Module
Your Turn
Let's Review
Adding Logging
Why Are You Logging
Logging or Transcript
Structured vs Unstructured
Write-Information
Toolmaking Tips and Tricks
Format Code
Part 6: Pester
Why Pester Matters
Core Pester Concepts
Installing Pester
What is Pester
Pester's Weak Point
Understand Unit Testing
Scope
Sample Code
New-Fixture
Writing Testable Code
What to Test
Describe Blocks
Context Blocks
BeforeEach and AfterEach
It Blocks
Should and Assertions
Should Operators
Mocks
Where to Mock
How to Mock
Verifiable Mocks
Parameter Filters
Mocking the Unmockable
Pester's TESTDRIVE
Clean Slate and Auto-Cleanup
Working with Sample Data
Using TESTDRIVE
Pester for Infrastructure Validation
Spinning Up the Validation Environment
Taking Actual Action
Testing the Outcomes of Your Actions
Measuring Code Coverage
Displaying Code Coverage Metrics
An Example
Test-Driven Development
Part 7: PowerShell 7 Scripting
PowerShell 7 Scripting Features
Updating Your Editor
Ternary Operators
Chain operators
Null-Coalescing Assignment
Null Conditional Operators
ForEach-Object Parallel
Using ANSI
Cross Platform Scripting
Know Your OS
State Your Requirements
Testing Variables
Environment Variables
Paths
Watch Your Aliases
Leverage Remoting
Custom Module Manifests
Wish List
Release Notes

Polecaj historie