liveBook · Manning

Remoting session

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A session object is a PowerShell object that represents an established remoting session and its connection information.

A remoting session is a PowerShell object representing an established connection to another computer and the connection information needed to use it. New-PSSession creates the object and keeps it in PowerShell memory; Get-PSSession retrieves open sessions later. Sessions reduce repeated connection setup and support interactive shells or commands sent to several computers. section 18.1

Use session objects to manage remote work:

  • Store returned objects in variables when the same computers will be contacted repeatedly. section 18.1
  • Pass one session to Enter-PSSession for an interactive remote shell; Exit-PSSession ends the shell but leaves the session open. section 18.2
  • Pass several sessions to Invoke-Command to run a command in parallel across their remote computers. section 18.3
  • Remove unused sessions with Remove-PSSession because active sessions consume local and remote resources. section 18.1

Create, inspect, and use sessions

Create reusable connections with New-PSSession, for example `$iis_servers = New-PSSession -ComputerName web01,web02,web03`. Retrieve them with `Get-PSSession`, inspect their ComputerName property with `Get-Member`, and select one for an interactive shell with `Get-PSSession -ComputerName SRV02 | Enter-PSSession`. For batch work, use `Invoke-Command -Command { Get-Process } -Session $iis_servers`. section 18.1 section 18.2 section 18.3

PS C:\> new-pssession -computername srv02,dc17,print99
 
PS C:\> new-pssession -hostname LinuxWeb01,srv03

Compare session creation over WinRM with -ComputerName and SSH with -Hostname. section 18.1

PS C:\> get-pssession

List the sessions currently maintained by PowerShell. section 18.1

PS C:\> invoke-command -command { Get-Process } -session $session01

Run one command against every session stored in a variable. section 18.3

PS C:\> Disconnect-PSSession -Id 4
Id Name              ComputerName  State
-- ----------------- ------------- -----
 4 Session4          COMPUTER2     Disconnected

Disconnect a session while leaving its PowerShell process running remotely, then reconnect it later with Connect-PSSession. section 18.5

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains creation, storage, inspection, interactive use, parallel invocation, cleanup, and disconnection, but not session internals.
Examples
Demonstrates web-server session variables, process queries, ComputerName filtering, interactive prompts, and disconnecting and reconnecting a session.
What it assumes
Assumes basic PowerShell commands and variables while teaching session creation, retrieval, selection, use, and cleanup.
What you can do after
Operate/configure reusable remote connections, run commands across them, enter interactive shells, and clean up or reconnect sessions.
How it connects
Connects PowerShell remoting with New-PSSession, Get-PSSession, Enter-PSSession, Invoke-Command, WinRM, SSH, and disconnected sessions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.