liveBook · Manning

Wrapper

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A wrapper is a software layer that presents an existing component or interface through a different, usually simpler interface.

A wrapper presents lower-level components through a consistent, simpler interface. PowerShell cmdlets translate commands into .NET Framework, CIM, COM, and Windows API operations, hiding implementation details and reducing maintenance. section 25.1

A useful PowerShell wrapper:

  • Follows native command patterns, including parameters and pipeline input. section 25.3
  • Keeps setup in Begin, processes input in Process, and exposes useful implementation choices such as synchronous or asynchronous behavior. section 25.3
  • Creates a reusable seam for help, team reuse, and Pester mocking when direct .NET calls cannot be mocked. section 25.3

Wrapping a speech synthesizer

Invoke-Speech loads System.Speech once, accepts text from the pipeline, and calls Speak for each phrase. An Asynchronous switch selects SpeakAsync; assigning its return value to $null suppresses unwanted completion objects. section 25.3

Listing 25.1 Wrapper for the speech synthesizer
function Invoke-Speech {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true)]       #1
        [string[]]$Text
    )
    BEGIN {
        Add-Type -AssemblyName System.Speech       #2
        $speech = New-Object -TypeName
System.Speech.Synthesis.SpeechSynthesizer
    }
    PROCESS {
        foreach ($phrase in $text) {
            $speech.speak($phrase)
        }
    }
    END {}
}
"One","Two","Three" | Invoke-Speech

Inspect the complete Invoke-Speech wrapper, including CmdletBinding, pipeline parameters, assembly setup, and Begin/Process structure. section 25.3

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Demonstrates wrapper internals through assembly loading, object lifetime, pipeline blocks, synchronous calls, asynchronous calls, and output suppression.
Examples
Uses Invoke-Speech with piped phrases, synchronous Speak, asynchronous SpeakAsync, and suppressed completion output.
What you can do after
Build it from scratch as a PowerShell-compliant command that hides .NET details and provides testable, reusable behavior.
How it connects
Connects PowerShell cmdlets with .NET Framework, CIM, COM, Windows APIs, pipeline input, and Pester-based mocking.

Also explained in

Compare how 4 books explain wrapper →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.