Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
June 18, 2024

Native PSSession PowerShell Scripting

The exploration of CIM and using the `CimSession` .NET class in PowerShell scripting got me thinking about another remoting option, `PSSession`. This could be another option for creating PowerShell scripts with remoting baked in. I'd like to have a function that doesn't rely on parameter sets to distinguish between a computer name and an existing PSSession. However, unlike `CimSession`, I can't easily create a `PSSession` object from a computer name. There is a little more involved, but it isn't too difficult. I thought I'd demonstrate how to crate and use a `PSSession` object from scratch. If nothing else, hopefully, you'll learn a little more about how PowerShell remoting works under the hood and maybe pickup a scripting idea or two. What I want to demonstrate requires PowerShell 7. I'm going to rely on a static method that doesn't exist in Windows PowerShell. But remember, you can still remotely connect to and manage systems running Windows PowerShell from a PowerShell 7 desktop. I am also leveraging traditional PowerShell remoting which means a Windows platform. Although I will show you how to setup an SSH PowerShell remoting session from scratch. There are several pieces to this puzzle. Creating a `PSSession` object is not as easy as what I showed you with `CimSession`. We know we ultimately need to create a `System.Management.Automation.Runspaces.PSSession` object. It was easy enough to create a session using `New-PSSession` and getting the type name with `Get-Member`. I'll dig deeper using [`Get-TypeMember`](https://bit.ly/3JVpGLB).

PSSession TypeMember
figure 1
In PowerShell 7 I can use the `Create` method.
PS C:\> Get-TypeMember System.Management.Automation.Runspaces.PSSession -Name Create | Select -ExpandProperty Syntax

$obj.Create([Runspace]runspace,[String]transportName,[PSCmdlet]psCmdlet)
So I need a runspace object. Let's see what that entails. To create a runspace I can use the `[RunspaceFactory]` class.
RunspaceFactory TypeMember
figure 2
The `CreateRunspace` method looks promising.
PS C:\> Get-TypeMember RunspaceFactory -Name CreateRunspace | Select-Object -ExpandProperty Syntax

$obj.CreateRunspace()

$obj.CreateRunspace([PSHost]host)

$obj.CreateRunspace([InitialSessionState]initialSessionState)

$obj.CreateRunspace([PSHost]host,[InitialSessionState]initialSessionState)

$obj.CreateRunspace([RunspaceConnectionInfo]connectionInfo,[PSHost]host,[TypeTable]typeTable)

$obj.CreateRunspace([RunspaceConnectionInfo]connectionInfo,[PSHost]host,[TypeTable]typeTable,[PSPrimitiveDictionary]applicationArguments)

$obj.CreateRunspace([RunspaceConnectionInfo]connectionInfo,[PSHost]host,[TypeTable]typeTable,[PSPrimitiveDictionary]applicationArguments,[String]name)

$obj.CreateRunspace([PSHost]host,[RunspaceConnectionInfo]connectionInfo)

$obj.CreateRunspace([RunspaceConnectionInfo]connectionInfo)
Because I know I will be writing a function that I want to use with remoting, I bet I need to look into how to create a `RunspaceConnectionInfo` object. I'm guessing this is part of the `Runspaces` namespace. I'll start typing the class name in PowerShell.
PS C:\>[System.Management.Automation.Runspaces.
After the period, I can press `Ctrl+Tab` and get a list of all possible completions.
Get a premium subscription for full article and archive access

The exploration of CIM and using the CimSession .NET class in PowerShell scripting got me thinking about another remoting option, PSSession. This could be another option for creating PowerShell scripts with remoting baked in. I'd like to have a function that doesn't rely on parameter sets to distinguish between a computer name and an existing PSSession.

However, unlike CimSession, I can't easily create a PSSession object from a computer name. There is a little more involved, but it isn't too difficult. I thought I'd demonstrate how to crate and use a PSSession object from scratch. If nothing else, hopefully, you'll learn a little more about how PowerShell remoting works under the hood and maybe pickup a scripting idea or two.

What I want to demonstrate requires PowerShell 7. I'm going to rely on a static method that doesn't exist in Windows PowerShell. But remember, you can still remotely connect to and manage systems running Windows PowerShell from a PowerShell 7 desktop. I am also leveraging traditional PowerShell remoting which means a Windows platform. Although I will show you how to setup an SSH PowerShell remoting session from scratch.

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