The office tool is a module with tests and a check by the analyzer. This lesson takes it to the places where it is meant to work: a remote machine, a pipeline, an automation. After that you collect in one place the traps this course has stumbled over, each with the output that shows it.

Everything was run in PowerShell 7.6.9 on Windows 11. Whatever needs a remote target, a pipeline or an Azure account is not run, read against Learn; each of these sections has a part that runs without the service.

Remoting over SSH

PowerShell can run commands on another machine. Learn ("PowerShell remoting over SSH"): remoting normally uses WinRM; SSH is now available on Linux and Windows and allows remoting between all platforms. It needs PowerShell 6 or higher and SSH on both machines (client ssh and server sshd); the server gets an entry in its configuration that starts PowerShell as an SSH subsystem (the page shows the line per platform). The three commands New-PSSession, Enter-PSSession and Invoke-Command have a parameter set with -HostName, -UserName and -KeyFilePath. Not run:

PowerShell
Enter-PSSession -HostName '<host>' -UserName '<user>' -KeyFilePath '<private-key-file>'
Invoke-Command -HostName '<host>' -UserName '<user>' -ScriptBlock { Get-Date }

Enter-PSSession opens an interactive session, Invoke-Command runs a script block and returns the result. Learn notes two things: with password sign-in PowerShell asks interactively, and a key (-KeyFilePath) is better; and SSH remoting currently supports no endpoint configuration and no Just Enough Administration (JEA). Whoever needs JEA needs WinRM. As with every sign-in, PS.4.2 applies: the key is never in the script and never in the repository.

What comes back can be shown without SSH. A result from afar is not a live object but a copy that was rebuilt into an object. You can show this without a remote machine with a job that runs in its own process and returns its results the same way:

PowerShell
$job = Start-Job { Get-Item $env:TEMP }
$item = Receive-Job $job -Wait -AutoRemoveJob
$item.GetType().FullName
$item.PSObject.TypeNames[0]
($item | Get-Member -MemberType Method | Measure-Object).Count
$local = Get-Item $env:TEMP
$local.GetType().FullName
($local | Get-Member -MemberType Method | Measure-Object).Count
System.Management.Automation.PSObject
Deserialized.System.IO.DirectoryInfo
2
System.IO.DirectoryInfo
20

The job read the same DirectoryInfo, but a PSObject with the type name Deserialized.System.IO.DirectoryInfo came back: the properties are there, most methods are not (2 against 20). A rule for remote scripts follows: fetch data from afar (properties, numbers, text), and let every action happen on the far side inside the script block instead of fetching the result back and calling methods on it. The script block does not receive values by itself: use -ArgumentList (and param(...) in the block) or $using:variable. A machine that is to be treated like a remote machine also needs its own -WhatIf: a script that lets remote commands change something stands behind ShouldProcess, and the change runs only in the block after ShouldProcess has said yes.