Chapter 18: Sessions: Remote control with less work
The introduction explains why repeatedly opening remote connections is tedious and motivates reusable PowerShell sessions.
After this chapter you will be able to
- Motivation for reusable sessions. Explain why reusable sessions are useful when repeatedly using Invoke-Command or Enter-PSSession for remote control.
- Persistent remoting session. Explain that a session is a persistent connection between local and remote copies of PowerShell that can be reused to invoke commands or enter a remote shell.
- Session creation and retrieval. Describe how New-PSSession creates session objects for specified computers, how Get-PSSession retrieves open sessions, and why storing the objects in a variable makes later access convenient. Distinguish the protocols selected by -ComputerName and -HostName.
- Interactive shell with a session. Explain how Enter-PSSession can use a single session object instead of a computer name, and what happens to that session after Exit-PSSession.
- Finding the right open session. Describe how to select one session when a variable holds several, and how Get-PSSession can retrieve an open session for Enter-PSSession without remembering its index.
- Targeting reusable remote sessions. Explain how Invoke-Command uses session objects to send a command or script to multiple remote computers in parallel, including when the sessions are stored in a variable.
- Remote command import process. Trace how a session with a computer that has a module installed becomes a set of commands available in the local shell.
- Remote execution and object limits. Explain where an imported command executes and why some returned objects cannot be used through implicit remoting.
- Disconnected Session Continuity. Describe what remains on the remote computer when a session is disconnected and how its creator can find and reconnect to it from another computer to resume work.
- Disconnected Session Resource Limits. Explain why disconnected sessions need cleanup controls and distinguish the WSMan settings that limit idle time, shell lifetime, users, shells per user, and incoming connections.
- PowerShell-enabled product inventory. Describe checking which PowerShell-enabled products are in your environment and recognizing that many of their modules are accessible through PowerShell remoting.
What it builds on
- Using Enter-PSSession and Exit-PSSession for one-to-one remoting (chapter 13) · Introduces one-to-one remoting with Enter-PSSession.
- Using Invoke-Command for one-to-many remoting (chapter 13) · Introduces one-to-many remoting with Invoke-Command.
- PSRP over SSH overview (chapter 13) · Introduces remoting over SSH.
- WinRM overview (chapter 13) · Introduces WinRM.
- The idea behind remote PowerShell (chapter 13) · Introduces remote PowerShell connections.
- Working with multiple objects in a variable (chapter 16) · Introduces working with multiple objects stored in a variable.
- Deserialized objects (chapter 13) · The distinction between remote results and local objects
Section by section
Introduction
The introduction explains why repeatedly opening remote connections is tedious and motivates reusable PowerShell sessions.
- Motivation for reusable sessions: Explain why reusable sessions are useful when repeatedly using Invoke-Command or Enter-PSSession for remote control.
Why it matters: It gives you a reason to use sessions when remote work involves repeated connections.
Key terms: Invoke-Command, Enter-PSSession, remoting connection, reusable sessions
18.1 Creating and using reusable sessions
This section explains what reusable PowerShell remoting sessions are, how to create and keep track of them, and when to close them.
- Persistent remoting session: Explain that a session is a persistent connection between local and remote copies of PowerShell that can be reused to invoke commands or enter a remote shell.
Why it matters: A reusable connection saves you from treating every remote action as a separate connection. - Session creation and retrieval: Describe how New-PSSession creates session objects for specified computers, how Get-PSSession retrieves open sessions, and why storing the objects in a variable makes later access convenient. Distinguish the protocols selected by -ComputerName and -HostName.
Why it matters: Keeping session objects accessible lets you reuse the intended remote connections. - Closing unused sessions: Explain why unused sessions should be closed and describe using Remove-PSSession to close selected sessions or all open sessions.
Why it matters: Closing idle sessions avoids tying up resources on your computer and the remote machine.
Key terms: session, persistent connection, remote shell, New-PSSession, Get-PSSession, session object, -ComputerName, -HostName, WinRM, Remove-PSSession
18.2 Enter-PSSession with session objects
This section shows how to enter a one-to-one remote shell using an existing session object and how to find the right session when several are open.
- Interactive shell with a session: Explain how Enter-PSSession can use a single session object instead of a computer name, and what happens to that session after Exit-PSSession.
Why it matters: You can leave an interactive remote shell and use the same open session again. - Finding the right open session: Describe how to select one session when a variable holds several, and how Get-PSSession can retrieve an open session for Enter-PSSession without remembering its index.
Why it matters: You can reach the intended remote computer without keeping track of session indexes.
Key terms: Enter-PSSession, Exit-PSSession, session object, Get-PSSession, PSSession object, -session parameter, ComputerName property
18.3 Invoke-Command with session objects
This section shows how to target reusable sessions with Invoke-Command, including a parenthetical alternative when session objects cannot be piped to it.
- Targeting reusable remote sessions: Explain how Invoke-Command uses session objects to send a command or script to multiple remote computers in parallel, including when the sessions are stored in a variable.
Why it matters: Reusable sessions let you target multiple remote computers without specifying them afresh for each command. - Parenthetical session selection: Distinguish passing session objects through Invoke-Command's -Session parameter from piping them to Invoke-Command, and describe how a parenthetical command can select sessions by computer name.
Why it matters: The parenthetical form provides a way to select sessions when piping them directly will not work.
Key terms: Invoke-Command, session objects, $session01, -session parameter, parenthetical command, pipeline
18.4 Implicit remoting: Importing a session
This section explains how implicit remoting makes commands from a remote module available locally, why that is useful, and what limits the returned objects have.
- Remote command import process: Trace how a session with a computer that has a module installed becomes a set of commands available in the local shell.
Why it matters: This explains why a command can appear in your shell without its module being installed on your computer. - Command naming and availability: Explain why the example adds a prefix to imported commands and how long those commands remain available.
Why it matters: The prefix helps you recognize which commands use the remote computer, while the open session keeps them accessible. - Remote execution and object limits: Explain where an imported command executes and why some returned objects cannot be used through implicit remoting.
Why it matters: A command can be convenient to run remotely while its returned objects still lack actions that local objects would have.
Key terms: implicit remoting, session, temporary module, rem prefix, New-remADUser, deserialized, properties, methods
18.5 Using disconnected sessions
Explains how disconnected PowerShell sessions can be resumed and why administrators must manage the remote shells they leave running.
- More Resilient Remote Sessions: Explain that PowerShell v3 sessions can survive brief network interruptions, even when you use Enter-PSSession with -ComputerName rather than an explicit session object.
Why it matters: A brief connection problem need not end your remote work. - Disconnected Session Continuity: Describe what remains on the remote computer when a session is disconnected and how its creator can find and reconnect to it from another computer to resume work.
Why it matters: You can continue remote work after leaving one computer or losing a connection. - Disconnected Session Resource Limits: Explain why disconnected sessions need cleanup controls and distinguish the WSMan settings that limit idle time, shell lifetime, users, shells per user, and incoming connections.
Why it matters: Disconnected sessions remain a resource you are responsible for managing.
Key terms: PowerShell v3, Enter-PSSession, -ComputerName, session, disconnected sessions, reconnect, session’s ID number, WSMan drive, IdleTimeout, MaxConcurrentUsers
18.6 Lab
18.7 Lab answers
18.8 Further exploration
Prompts the reader to look for products in their environment whose PowerShell modules may be accessible through remoting.
- PowerShell-enabled product inventory: Describe checking which PowerShell-enabled products are in your environment and recognizing that many of their modules are accessible through PowerShell remoting.
Why it matters: Your existing products may offer PowerShell commands you can use remotely.
Key terms: PowerShell-enabled products, PowerShell modules, PowerShell remoting
Concepts explained in this chapter
- remoting sessionA session object is a PowerShell object that represents an established remoting session and its connection information.
- Get-PSSessionGet-PSSession is a PowerShell cmdlet that retrieves remoting sessions managed by the current PowerShell session.
- implicit remotingImplicit remoting is a PowerShell technique that exposes commands from a remote session as local proxy commands while executing them on the remote computer.
- Enter-PSSessionEnter-PSSession is a PowerShell cmdlet that creates an interactive session with a local or remote computer.
- Invoke-CommandInvoke-Command is a PowerShell cmdlet that runs commands or script blocks on local or remote computers.
- Exit-PSSessionExit-PSSession is a PowerShell cmdlet that ends the current interactive PowerShell session and returns to the originating shell.
- Import-ModuleImport-Module is a PowerShell cmdlet that loads a module into the current session from a module name or path.
- moduleA module is a packaged unit of reusable code and related resources that can be loaded into a software environment.
- remotingRemoting is the execution of commands or management operations on a computer or service over a network connection.
- PowerShellPowerShell 3 is the third major version of the PowerShell command-line shell.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
