Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
June 25, 2024

Creating PSSession-Based PowerShell Tools

Let's continue our exploration of creating and working with `PSSession` objects built from scratch. While I would normally use `New-PSSession`, using the .NET Framework shaves a few milliseconds off the process because I don't have the overhead of a cmdlet, and I can control the process more directly. Today, I want to continue our exploration, including demonstrating how to make an SSH-based `PSSession`. And I want to give you a sample scripting project. ## Opening the Remote Runspace Before we get to those topics, I want to briefly revisit working with the runspace that is part of the `PSSession` object. I shared this script sample that will create a `PSSession`.

#requires -version 7.4

#mkpssession.ps1

# You could modify to support credentials and/or SSL

Param([string]$ComputerName = $env:COMPUTERNAME)

$ci = [System.Management.Automation.Runspaces.WSManConnectionInfo]::new()

$ci.ComputerName = $ComputerName

$rs = [RunSpaceFactory]::CreateRunspace($ci)

$rs.ApartmentState = 'STA'

$rs.ThreadOptions = 'ReuseThread'

#script output

[System.Management.Automation.Runspaces.PSSession]::Create($rs, 'WSMan', $PSCmdlet)
When you run this, the connection to the remote computer is not open.
PS C:\> $a = c:\scripts\mkpssession.ps1 -computername dom1

PS C:\> $a

 Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability

 -- ----            --------- ------------    ------------    -----         -----------------     ------------

 13 Runspace13      WSMan     dom1            RemoteMachine   BeforeOpen    Microsoft.PowerShell          None

 PS C:\> $a.Runspace

 Id Name            ComputerName    Type          State         Availability

 -- ----            ------------    ----          -----         ------------

 19 Runspace19      dom1            Remote        BeforeOpen    None

 ```

Before I can invoke any code, the runspace must be opened. In the previous article I called the `Open()` method.

```powershell

$a.Runspace.Open()
This blocks your prompt while all the setup and authentication is happening. You can speed this process up by opening the runspace *asynchronously*.
$a.Runspace.OpenAsync()
This will make your code feel more responsive. I will demonstrate how I incorporate this into my PowerShell tooling a little bit later. Because the code I'm using to create the `PSSession` requires PowerShell 7, we might as well see how to setup an SSH-based `PSSession`. I'm going to assume you've already configured PowerShell remoting with SSH in your environment. Creating the SSH-based connection isn't that much different than using WSMan.
Get a premium subscription for full article and archive access

Let's continue our exploration of creating and working with PSSession objects built from scratch. While I would normally use New-PSSession, using the .NET Framework shaves a few milliseconds off the process because I don't have the overhead of a cmdlet, and I can control the process more directly.

Today, I want to continue our exploration, including demonstrating how to make an SSH-based PSSession. And I want to give you a sample scripting project.

Opening the Remote Runspace

Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.