From 05ec4f633777e893191053709dade99cbb4faa5b Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Fri, 18 Sep 2026 10:35:48 -0700 Subject: [PATCH] fix: PowerShell.Create(Runspace) has no such overload on PS 5.1 [System.Management.Automation.PowerShell]::Create(Runspace) doesn't exist as an overload in Windows PowerShell 5.1's System.Management. Automation (v3.0.0.0, GAC-loaded) -- only Create(), Create(RunspaceMode), and Create(InitialSessionState) are available there. Calling it with a Runspace argument throws: MethodException: Cannot find an overload for "Create" and the argument count: "1". Split into Create() followed by assigning the writable Runspace property instead, which works on both PS 5.1 and 7+. Fixes both call sites (New-KoanRunspace and Invoke-Koan) plus the test file's own use of the same broken pattern. Verified locally: New-KoanRunspace.Tests.ps1 + Invoke-Koan.Tests.ps1 pass under both powershell.exe 5.1 and pwsh 7. --- PSKoans/Private/Invoke-Koan.ps1 | 3 ++- PSKoans/Private/New-KoanRunspace.ps1 | 3 ++- tests/Functions/Private/New-KoanRunspace.Tests.ps1 | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/PSKoans/Private/Invoke-Koan.ps1 b/PSKoans/Private/Invoke-Koan.ps1 index fe5f0399b..560c0b5c8 100644 --- a/PSKoans/Private/Invoke-Koan.ps1 +++ b/PSKoans/Private/Invoke-Koan.ps1 @@ -61,7 +61,8 @@ } } - $ps = [powershell]::Create($script:KoanRunspace) + $ps = [powershell]::Create() + $ps.Runspace = $script:KoanRunspace $ps.AddScript($Script, <# useLocalScope: #> $true) > $null $ps.AddParameter('Params', $ParameterSplat) > $null diff --git a/PSKoans/Private/New-KoanRunspace.ps1 b/PSKoans/Private/New-KoanRunspace.ps1 index b39beda97..d564fd449 100644 --- a/PSKoans/Private/New-KoanRunspace.ps1 +++ b/PSKoans/Private/New-KoanRunspace.ps1 @@ -22,7 +22,8 @@ function New-KoanRunspace { $runspace = [runspacefactory]::CreateRunspace() $runspace.Open() $runspace.Name = 'PSKoans.KoanRunspace' - $ps = [powershell]::Create($runspace) + $ps = [powershell]::Create() + $ps.Runspace = $runspace try { $script = { diff --git a/tests/Functions/Private/New-KoanRunspace.Tests.ps1 b/tests/Functions/Private/New-KoanRunspace.Tests.ps1 index a1e378ef7..b1cc8101d 100644 --- a/tests/Functions/Private/New-KoanRunspace.Tests.ps1 +++ b/tests/Functions/Private/New-KoanRunspace.Tests.ps1 @@ -44,7 +44,8 @@ Describe 'New-KoanRunspace' { It 'preloads the runspace with PSKoans' { $runspace = InModuleScope 'PSKoans' { New-KoanRunspace } - $ps = [powershell]::Create($runspace) + $ps = [powershell]::Create() + $ps.Runspace = $runspace try { $ps.AddCommand('Get-Module').AddParameter('Name', 'PSKoans') > $null