From b71d3a1b7416cd6d0831ba53e1b818ec42e54e17 Mon Sep 17 00:00:00 2001 From: yotsuda Date: Sat, 15 Nov 2025 22:32:06 +0900 Subject: [PATCH 1/4] Correct handling of explicit -UseWindowsPowerShell:$false parameter value in New-PSSession --- .../remoting/commands/newrunspacecommand.cs | 11 +++++- .../engine/Remoting/PSSession.Tests.ps1 | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs b/src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs index 8986def5a6d..60ca0c35e50 100644 --- a/src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs @@ -267,7 +267,16 @@ protected override void ProcessRecord() case NewPSSessionCommand.UseWindowsPowerShellParameterSet: { - remoteRunspaces = CreateRunspacesForUseWindowsPowerShellParameterSet(); + if (UseWindowsPowerShell) + { + remoteRunspaces = CreateRunspacesForUseWindowsPowerShellParameterSet(); + } + else + { + // When -UseWindowsPowerShell:$false is explicitly specified, + // fall back to the default ComputerName parameter set behavior + goto case NewPSSessionCommand.ComputerNameParameterSet; + } } break; diff --git a/test/powershell/engine/Remoting/PSSession.Tests.ps1 b/test/powershell/engine/Remoting/PSSession.Tests.ps1 index 689e587ddb1..7cdf4e30536 100644 --- a/test/powershell/engine/Remoting/PSSession.Tests.ps1 +++ b/test/powershell/engine/Remoting/PSSession.Tests.ps1 @@ -91,3 +91,38 @@ Describe "SkipCACheck and SkipCNCheck PSSession options are required for New-PSS $er.Exception.ErrorCode | Should -Be $expectedErrorCode } } + +Describe "New-PSSession -UseWindowsPowerShell switch parameter" -Tag "CI" { + + BeforeAll { + $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() + + if (-not $IsWindows) { + $PSDefaultParameterValues['it:skip'] = $true + } + } + + AfterAll { + $global:PSDefaultParameterValues = $originalDefaultParameterValues + } + + It "Should respect explicit -UseWindowsPowerShell:`$false parameter value" { + # -UseWindowsPowerShell:$false should behave the same as not specifying the parameter + # When UseWindowsPowerShell is not specified (or explicitly false), it should attempt + # to connect via WinRM to localhost, which will fail if WinRM is not configured + + $errorWithFalse = { New-PSSession -UseWindowsPowerShell:$false -ErrorAction Stop } | + Should -Throw -PassThru + + $errorWithoutParameter = { New-PSSession -ErrorAction Stop } | + Should -Throw -PassThru + + # Both should produce connection errors (since WinRM is not configured in test environment) + # NOT create a Windows PowerShell session + $errorWithFalse.Exception.Message | Should -Match 'connect|WinRM|localhost' + $errorWithoutParameter.Exception.Message | Should -Match 'connect|WinRM|localhost' + + # Both errors should be similar (not exact match due to timing/detail differences) + $errorWithFalse.Exception.GetType() | Should -Be $errorWithoutParameter.Exception.GetType() + } +} From b462581b42a5a4045a5d538ee638ad3f82474c2b Mon Sep 17 00:00:00 2001 From: yotsuda Date: Tue, 18 Nov 2025 13:47:29 +0900 Subject: [PATCH 2/4] Fix test to expect successful WinRM connection instead of exception --- .../engine/Remoting/PSSession.Tests.ps1 | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/test/powershell/engine/Remoting/PSSession.Tests.ps1 b/test/powershell/engine/Remoting/PSSession.Tests.ps1 index 7cdf4e30536..96095e91ea1 100644 --- a/test/powershell/engine/Remoting/PSSession.Tests.ps1 +++ b/test/powershell/engine/Remoting/PSSession.Tests.ps1 @@ -111,18 +111,33 @@ Describe "New-PSSession -UseWindowsPowerShell switch parameter" -Tag "CI" { # When UseWindowsPowerShell is not specified (or explicitly false), it should attempt # to connect via WinRM to localhost, which will fail if WinRM is not configured - $errorWithFalse = { New-PSSession -UseWindowsPowerShell:$false -ErrorAction Stop } | - Should -Throw -PassThru - - $errorWithoutParameter = { New-PSSession -ErrorAction Stop } | - Should -Throw -PassThru - - # Both should produce connection errors (since WinRM is not configured in test environment) - # NOT create a Windows PowerShell session - $errorWithFalse.Exception.Message | Should -Match 'connect|WinRM|localhost' - $errorWithoutParameter.Exception.Message | Should -Match 'connect|WinRM|localhost' - - # Both errors should be similar (not exact match due to timing/detail differences) - $errorWithFalse.Exception.GetType() | Should -Be $errorWithoutParameter.Exception.GetType() + # Test 1: -UseWindowsPowerShell:$true should create a Windows PowerShell 5.1 session + $session = $null + try { + $session = New-PSSession -UseWindowsPowerShell:$true -ErrorAction Stop + $session | Should -Not -BeNullOrEmpty + + # Verify it's Windows PowerShell 5.1 + $version = Invoke-Command -Session $session -ScriptBlock { $PSVersionTable.PSVersion } + $version.Major | Should -Be 5 + $version.Minor | Should -Be 1 + } + finally { + if ($session) { Remove-PSSession $session -ErrorAction SilentlyContinue } + } + + # Test 2: -UseWindowsPowerShell:$false should create a PowerShell Core session via WinRM + $session = $null + try { + $session = New-PSSession -UseWindowsPowerShell:$false -ErrorAction Stop + $session | Should -Not -BeNullOrEmpty + + # Verify it's PowerShell Core (not Windows PowerShell 5.1) + $version = Invoke-Command -Session $session -ScriptBlock { $PSVersionTable.PSVersion } + $version.Major | Should -BeGreaterOrEqual 6 + } + finally { + if ($session) { Remove-PSSession $session -ErrorAction SilentlyContinue } + } } } From c9ab2a268951b50d70ef1c4ec71c237e7025c31d Mon Sep 17 00:00:00 2001 From: yotsuda Date: Thu, 20 Nov 2025 11:44:16 +0900 Subject: [PATCH 3/4] Update test to verify -UseWindowsPowerShell:$false uses WSMan transport - Adopt { ... } | Should -Not -Throw pattern as suggested by @mklement0 - Verify Transport is 'WSMan' (not 'Process') for -UseWindowsPowerShell:$false - Simpler test that is independent of default PowerShell edition/version --- .../engine/Remoting/PSSession.Tests.ps1 | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/test/powershell/engine/Remoting/PSSession.Tests.ps1 b/test/powershell/engine/Remoting/PSSession.Tests.ps1 index 96095e91ea1..ba434069539 100644 --- a/test/powershell/engine/Remoting/PSSession.Tests.ps1 +++ b/test/powershell/engine/Remoting/PSSession.Tests.ps1 @@ -107,37 +107,31 @@ Describe "New-PSSession -UseWindowsPowerShell switch parameter" -Tag "CI" { } It "Should respect explicit -UseWindowsPowerShell:`$false parameter value" { - # -UseWindowsPowerShell:$false should behave the same as not specifying the parameter - # When UseWindowsPowerShell is not specified (or explicitly false), it should attempt - # to connect via WinRM to localhost, which will fail if WinRM is not configured - # Test 1: -UseWindowsPowerShell:$true should create a Windows PowerShell 5.1 session - $session = $null + $session1 = $null try { - $session = New-PSSession -UseWindowsPowerShell:$true -ErrorAction Stop - $session | Should -Not -BeNullOrEmpty + { $script:session1 = New-PSSession -UseWindowsPowerShell:$true } | Should -Not -Throw + $script:session1 | Should -Not -BeNullOrEmpty # Verify it's Windows PowerShell 5.1 - $version = Invoke-Command -Session $session -ScriptBlock { $PSVersionTable.PSVersion } + $version = Invoke-Command -Session $script:session1 -ScriptBlock { $PSVersionTable.PSVersion } $version.Major | Should -Be 5 $version.Minor | Should -Be 1 } finally { - if ($session) { Remove-PSSession $session -ErrorAction SilentlyContinue } + if ($script:session1) { Remove-PSSession $script:session1 -ErrorAction SilentlyContinue } } - # Test 2: -UseWindowsPowerShell:$false should create a PowerShell Core session via WinRM - $session = $null + # Test 2: -UseWindowsPowerShell:$false should use WSMan transport (not Process) + $sessionWithFalse = $null try { - $session = New-PSSession -UseWindowsPowerShell:$false -ErrorAction Stop - $session | Should -Not -BeNullOrEmpty + { $script:sessionWithFalse = New-PSSession -UseWindowsPowerShell:$false } | Should -Not -Throw - # Verify it's PowerShell Core (not Windows PowerShell 5.1) - $version = Invoke-Command -Session $session -ScriptBlock { $PSVersionTable.PSVersion } - $version.Major | Should -BeGreaterOrEqual 6 + # Transport should be WSMan, not Process + $script:sessionWithFalse.Transport | Should -Be 'WSMan' } finally { - if ($session) { Remove-PSSession $session -ErrorAction SilentlyContinue } + if ($script:sessionWithFalse) { Remove-PSSession $script:sessionWithFalse -ErrorAction SilentlyContinue } } } } From f3eb795ceb26cc861dcf3be8a68d0653b6f54a70 Mon Sep 17 00:00:00 2001 From: yotsuda Date: Fri, 21 Nov 2025 16:26:24 +0900 Subject: [PATCH 4/4] Address review feedback from iSazonov and Copilot 1. Rename $session1 to $sessionWithTrue for better symmetry with $sessionWithFalse 2. Add null check for $sessionWithFalse to match the pattern used in Test 1 These changes improve test code readability and robustness. --- test/powershell/engine/Remoting/PSSession.Tests.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/powershell/engine/Remoting/PSSession.Tests.ps1 b/test/powershell/engine/Remoting/PSSession.Tests.ps1 index ba434069539..317ed4af734 100644 --- a/test/powershell/engine/Remoting/PSSession.Tests.ps1 +++ b/test/powershell/engine/Remoting/PSSession.Tests.ps1 @@ -108,24 +108,25 @@ Describe "New-PSSession -UseWindowsPowerShell switch parameter" -Tag "CI" { It "Should respect explicit -UseWindowsPowerShell:`$false parameter value" { # Test 1: -UseWindowsPowerShell:$true should create a Windows PowerShell 5.1 session - $session1 = $null + $sessionWithTrue = $null try { - { $script:session1 = New-PSSession -UseWindowsPowerShell:$true } | Should -Not -Throw - $script:session1 | Should -Not -BeNullOrEmpty + { $script:sessionWithTrue = New-PSSession -UseWindowsPowerShell:$true } | Should -Not -Throw + $script:sessionWithTrue | Should -Not -BeNullOrEmpty # Verify it's Windows PowerShell 5.1 - $version = Invoke-Command -Session $script:session1 -ScriptBlock { $PSVersionTable.PSVersion } + $version = Invoke-Command -Session $script:sessionWithTrue -ScriptBlock { $PSVersionTable.PSVersion } $version.Major | Should -Be 5 $version.Minor | Should -Be 1 } finally { - if ($script:session1) { Remove-PSSession $script:session1 -ErrorAction SilentlyContinue } + if ($script:sessionWithTrue) { Remove-PSSession $script:sessionWithTrue -ErrorAction SilentlyContinue } } # Test 2: -UseWindowsPowerShell:$false should use WSMan transport (not Process) $sessionWithFalse = $null try { { $script:sessionWithFalse = New-PSSession -UseWindowsPowerShell:$false } | Should -Not -Throw + $script:sessionWithFalse | Should -Not -BeNullOrEmpty # Transport should be WSMan, not Process $script:sessionWithFalse.Transport | Should -Be 'WSMan'