File size: 5,382 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# PSSession tests for non-Windows platforms
#
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
param()
function GetRandomString()
{
return [System.IO.Path]::GetFileNameWithoutExtension([System.IO.Path]::GetRandomFileName())
}
Describe "New-PSSessionOption parameters for non-Windows platforms" -Tag "CI" {
BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
if ($IsWindows) {
$PSDefaultParameterValues['it:skip'] = $true
}
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
It "Verifies New-PSSessionOption parameters" {
$cmdInfo = Get-Command New-PSSessionOption
$commonParameterCount = [System.Management.Automation.Internal.CommonParameters].GetProperties().Length
$cmdInfo.Parameters.Count | Should -Be ($commonParameterCount + 2) -Because "Only -SkipCACheck and -SkipCNCheck switch parameters are available"
{ $null = $cmdInfo.ResolveParameter("SkipCACheck") } | Should -Not -Throw -Because "SkipCACheck parameter should be available"
{ $null = $cmdInfo.ResolveParameter("SkipCNCheck") } | Should -Not -Throw -Because "SkipCNCheck parameter should be available"
}
}
Describe "SkipCACheck and SkipCNCheck PSSession options are required for New-PSSession on non-Windows platforms" -Tag "CI" {
BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
# Skip this test for macOS because the latest OS release is incompatible with our shipped libmi for WinRM/OMI.
if ($IsWindows -or $IsMacOS) {
$PSDefaultParameterValues['it:skip'] = $true
}
else {
$userName = "User_$(Get-Random -Maximum 99999)"
$userPassword = GetRandomString
$cred = [pscredential]::new($userName, (ConvertTo-SecureString -String $userPassword -AsPlainText -Force))
$soSkipCA = New-PSSessionOption -SkipCACheck
$soSkipCN = New-PSSessionOption -SkipCNCheck
}
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
$testCases = @(
@{
Name = 'Verifies expected error when session option is missing'
ScriptBlock = { New-PSSession -cn localhost -Credential $cred -Authentication Basic -UseSSL }
ExpectedErrorCode = 825
},
@{
Name = 'Verifies expected error when SkipCACheck option is missing'
ScriptBlock = { New-PSSession -cn localhost -Credential $cred -Authentication Basic -UseSSL -SessionOption $soSkipCN }
ExpectedErrorCode = 825
},
@{
Name = 'Verifies expected error when SkipCNCheck option is missing'
ScriptBlock = { New-PSSession -cn localhost -Credential $cred -Authentication Basic -UseSSL -SessionOption $soSkipCA }
ExpectedErrorCode = 825
}
)
It "<Name>" -TestCases $testCases {
param ($scriptBlock, $expectedErrorCode)
if ( -not (Get-WsManSupport)) {
Set-ItResult -Skipped -Because "MI library not available for this platform"
return
}
$er = { & $scriptBlock } | Should -Throw -ErrorId 'System.Management.Automation.Remoting.PSRemotingDataStructureException,Microsoft.PowerShell.Commands.NewPSSessionCommand' -PassThru
$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 }
}
}
}
|