mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Respect -UseWindowsPowerShell:$false in New-PSSession (#26469)
This commit is contained in:
@@ -267,7 +267,16 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
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;
|
||||
|
||||
@@ -91,3 +91,48 @@ 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" {
|
||||
# Test 1: -UseWindowsPowerShell:$true should create a Windows PowerShell 5.1 session
|
||||
$sessionWithTrue = $null
|
||||
try {
|
||||
{ $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:sessionWithTrue -ScriptBlock { $PSVersionTable.PSVersion }
|
||||
$version.Major | Should -Be 5
|
||||
$version.Minor | Should -Be 1
|
||||
}
|
||||
finally {
|
||||
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'
|
||||
}
|
||||
finally {
|
||||
if ($script:sessionWithFalse) { Remove-PSSession $script:sessionWithFalse -ErrorAction SilentlyContinue }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user