Lesson 6 of 13 · 15m
Sessions, Fan-Out, and the Serialization Tax
Objects crossing the remoting boundary arrive as method-less CLIXML corpses, so actions must run inside remote scriptblocks.
Invoke-Command -ComputerName web01 { Get-Process } does not give you the Process objects living on web01. It gives you photographs of them. Between the remote runspace and yours is a serialization boundary: every object crossing it is flattened to CLIXML, rehydrated on your side as a Deserialized.System.Diagnostics.Process — properties intact, methods gone, live handles dead. Call .Kill() on it and you get an error telling you the method doesn't exist. The object describes a process on web01; it cannot do anything to it.
Actions run on the far side
The consequence is a design rule: send verbs across the boundary, not nouns. If the goal is "kill the w3wp eating CPU on web01," the scriptblock contains the kill:
Invoke-Command -ComputerName web01 -ScriptBlock {
Get-Process w3wp | Sort-Object CPU -Descending | Select-Object -First 1 | Stop-Process -Force
}Everything inside the braces is live objects on web01 — Stop-Process acts on a real Process. What returns is whatever the block outputs: nothing, a status string, or a small result object. The failure mode of the wrong model is common and silent: Invoke-Command web01 { Get-Service w3svc } | Restart-Service looks right — but the deserialized service has no Restart(), and Restart-Service doesn't refuse it cleanly: -Name binds pipeline input ByValue, so the corpse is coerced to a string — its type name, not w3svc — and the call dies on a service-not-found error. The restart must be inside the scriptblock.
State also doesn't cross back implicitly. $x = Invoke-Command web01 { $x = 5 } assigns the emitted output, not the remote variable. Remote scriptblocks see no local variables at all — $using:name (PS 3.0+) splices a local value into the remote block, no param() block required: Invoke-Command web01 { Get-EventLog -Newest $using:count System }.
Ad-hoc vs persistent sessions
Invoke-Command -ComputerName builds and tears down a runspace per call. New-PSSession web01 keeps the runspace alive, and Invoke-Command -Session $s {...} reuses it — worth it when you run many commands against the same host (no repeated session setup cost), when the remote end needs state between calls (Set-Location, imported modules, variables), and for Import-PSSession, which generates local proxy functions that forward to the remote endpoint. That last one is how Exchange on-prem and older Exchange Online admins got Get-Mailbox locally: implicit remoting, a local function shim for remote cmdlets.
-ThrottleLimit (default 32) caps how many sessions a single Invoke-Command fans out to at once. On a jump box talking to 500 servers, set it deliberately — every session is a wsmprovhost.exe on the far end.
Disconnected sessions exist for the long job: Invoke-Command web01 -InDisconnectedSession {...} or Disconnect-PSSession leave the runspace running on the server while your client, your VPN, and your laptop all go away. Connect-PSSession — from anywhere, by anyone with rights — reattaches and collects output via Receive-PSSession. This is the correct answer to "the patch audit takes six hours," not leaving an RDP session logged in over the weekend.
What the wire costs
CLIXML survives round-trips for strings, numbers, dates, enums, hashtables, and your own [PSCustomObject]s. It does not survive live handles, streams, sockets, COM objects, or methods. Complex .NET types degrade: some emit a ToString(), some partially rehydrate. A DataTable comes back usable for property access; a FileStream comes back a corpse. If a remote query returns something you'll act on locally (paths, IDs, names), select down to those scalar fields before it crosses — Invoke-Command web01 { Get-AppxPackage | Select-Object Name, Version, InstallLocation } — smaller over the wire, cleaner on arrival.
Every result also gains note properties: PSComputerName, RunspaceId, PSShowComputerName. PSComputerName is how fan-out results stay attributable — Invoke-Command -ComputerName (gc servers.txt) {Get-Service w3svc} | Group-Object PSComputerName gives you per-server buckets for free.
WSMan quotas and the money patch
Remote work runs under wsmprovhost.exe governed by quotas in WSMan:\localhost\Shell\*: MaxConcurrentUsers, MaxProcessesPerShell, MaxMemoryPerShellMB (150 MB on old WinRM builds, 1024 MB on current builds through Server 2025), MaxShellsPerUser. The classic incident: a data-hungry remoting script dies with The WSMan provider host process did not return a proper response or out-of-memory errors on a healthy server — that's MaxMemoryPerShellMB, tunable via Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 4096 or winrm set winrm/config/winrs @{MaxMemoryPerShellMB="4096"}. Size the quota to the workload, not to an unbounded "just in case."
The practice
Design remoting scriptblocks so the action and the heavy data stay remote; return small, typed summaries. Reuse PSSessions for repeated work, disconnect for long work, and remember PSComputerName is your attribution. When something "works locally but not over remoting," check for the serialization corpse first — a dead method, a missing type — before blaming the network.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
A Fake GTA 6 ISO Didn’t Install a Game. It Installed a Wiper.
A fake GTA 6 ISO used a believable installer error to cover remote-access malware, browser-data theft, and destructive file wiping. The “License not found” message wasn’t the crack failing. It was there so the victim would stop looking.
BlueHammer: The Unpatched Windows Zero-Day That Weaponizes Microsoft Defender Against Your Own System
BlueHammer is an unpatched, publicly released Windows local privilege escalation exploit that chains Microsoft Defender's update workflow, Volume Shadow Copy, Cloud Files callbacks, and opportunistic locks to reach NT AUTHORITY\SYSTEM from a standard user account.
Running a Minecraft Network: Four JVMs, One Box, and the Settings That Lie to You
Four servers on one host means four JVMs each sizing its thread pools for a machine it does not own. The measured cost of that, the systemd ordering that guarantees nothing, and the throttle that switches itself back on when your forwarding breaks.
Building a Minecraft Network: Four Servers Pretending to Be One Place
A proxy, a lobby, an SMP and a PvP arena is four JVMs that know nothing about each other. What actually has to be shared, why your permissions don't follow players, and the two LuckPerms defaults that silently share nothing at all.