MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Remote module tests" -Tags 'Feature','RequireAdminOnWindows' {4 5 BeforeAll {6 $pendingTest = (Test-IsWinWow64)7 $skipTest = !$IsWindows8 9 $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()10 11 if ($pendingTest -or $skipTest)12 {13 if ($skipTest)14 {15 $PSDefaultParameterValues["it:skip"] = $true16 }17 elseif ($pendingTest)18 {19 $PSDefaultParameterValues["it:pending"] = $true20 }21 22 return23 }24 25 $pssession = New-RemoteSession26 # pending https://github.com/PowerShell/PowerShell/issues/481927 # $cimsession = New-RemoteSession -CimSession28 }29 30 AfterAll {31 32 if ($pendingTest -or $skipTest)33 {34 $global:PSDefaultParameterValues = $originalDefaultParameterValues35 return36 }37 38 if ($pssession -ne $null) { Remove-PSSession $pssession -ErrorAction SilentlyContinue }39 }40 41 It "Get-Module fails if not using -ListAvailable with '<parameter>'" -TestCases @(42 @{parameter="PSSession" ; value=$pssession}43 # @{parameter="CIMSession"; value=$cimsession}44 ) {45 param($parameter, $value)46 $parameters = @{$parameter=$value}47 { Get-Module @parameters -ErrorAction Stop } | Should -Throw -ErrorId "RemoteDiscoveryWorksOnlyInListAvailableMode,Microsoft.PowerShell.Commands.GetModuleCommand"48 }49 50 It "Get-Module succeeds using -ListAvailable with '<parameter>'" -TestCases @(51 @{parameter="PSSession" ; value=$pssession},52 @{parameter="PSSession" ; value=$pssession ; name="Pester"}53 # @{parameter="CIMSession"; value=$cimsession},54 # @{parameter="CIMSession"; value=$cimsession; name="pESTER"}55 ) {56 param($parameter, $value, $name)57 $parameters = @{$parameter=$value; ListAvailable=$true; Refresh=$true}58 if ($name) {59 $parameters += @{name=$name}60 }61 $modules = Get-Module @parameters62 $modules | Should -Not -BeNullOrEmpty63 $modules[0] | Should -BeOfType System.Management.Automation.PSModuleInfo64 }65 66 It "Get-Module can be called as an API with '<parameter>' = '<value>'" -TestCases @(67 @{parameter = "Name" ; value = "foo"},68 @{parameter = "FullyQualifiedName"; value = @{ModuleName="foo";ModuleVersion="1.2.3"}},69 @{parameter = "All" ; value = $true},70 @{parameter = "All" ; value = $false},71 @{parameter = "ListAvailable" ; value = $true},72 @{parameter = "ListAvailable" ; value = $false},73 @{parameter = "PSEdition" ; value = "foo"},74 @{parameter = "Refresh" ; value = $true},75 @{parameter = "Refresh" ; value = $false},76 @{parameter = "PSSession" ; value = $pssession},77 # @{parameter = "CimSession" ; value = $cimsession},78 @{parameter = "CimResourceUri" ; value = "http://foo/"},79 @{parameter = "CimNamespace" ; value = "foo"},80 @{parameter = "PSEdition" ; value = "Core"},81 @{parameter = "PSEdition" ; value = "Desktop"}82 ) {83 param($parameter, $value)84 85 $getModuleCommand = [Microsoft.PowerShell.Commands.GetModuleCommand]::new()86 $getModuleCommand.$parameter = $value87 if ($parameter -eq "FullyQualifiedName") {88 $getModuleCommand.FullyQualifiedName | Should -BeOfType Microsoft.PowerShell.Commands.ModuleSpecification89 $getModuleCommand.FullyQualifiedName.Name | Should -BeExactly "foo"90 $getModuleCommand.FullyQualifiedName.Version | Should -Be "1.2.3"91 } else {92 $getModuleCommand.$parameter | Should -Be $value93 }94 }95 96 It "Failure if -Name and -FullyQualifiedName are both specified" {97 { Get-Module -Name foo -FullyQualifiedName @{ModuleName='foo'} -ErrorAction Stop } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetModuleCommand"98 }99 100 It "Get-Module supports pipeline" {101 $module = Get-Module -Name Microsoft.PowerShell.Utility102 Compare-Object $module ($module | Get-Module) | Should -BeNullOrEmpty103 }104 105 It "New-CimSession works" -Pending {106 # enable all CimSession tests once https://github.com/PowerShell/PowerShell/issues/4819 is fixed107 }108}109 