diff --git a/source/DSCResources/DSC_VMHyperV/DSC_VMHyperV.psm1 b/source/DSCResources/DSC_VMHyperV/DSC_VMHyperV.psm1 index c829ab8..dabef96 100644 --- a/source/DSCResources/DSC_VMHyperV/DSC_VMHyperV.psm1 +++ b/source/DSCResources/DSC_VMHyperV/DSC_VMHyperV.psm1 @@ -907,13 +907,6 @@ function Test-TargetResource } } - # If IsClustered is set in configuration - if ($PSBoundParameters.ContainsKey('IsClustered') -and -not $vmObj.IsClustered) - { - Write-Verbose -Message ($script:localizedData.NotAddedToCluster -f $vmObj.Name) - return $false - } - return $true } else diff --git a/source/Modules/HyperVDsc.Common/HyperVDsc.Common.psm1 b/source/Modules/HyperVDsc.Common/HyperVDsc.Common.psm1 index 90a7be7..309f733 100644 --- a/source/Modules/HyperVDsc.Common/HyperVDsc.Common.psm1 +++ b/source/Modules/HyperVDsc.Common/HyperVDsc.Common.psm1 @@ -303,6 +303,7 @@ function ConvertFrom-TimeSpan #> function Get-VMHyperV { + [OutputType([System.Collections.ArrayList])] [CmdletBinding()] param ( @@ -321,44 +322,45 @@ function Get-VMHyperV if (Get-Command -Name Get-Cluster -ErrorAction SilentlyContinue) { - [bool] $isClustered = (Get-Cluster -WarningAction SilentlyContinue) -ne $null + [bool] $isClustered = $null -ne (Get-Cluster -ErrorAction SilentlyContinue -WarningAction SilentlyContinue) } + $vms = [System.Collections.ArrayList]::new() + if ($isClustered) { $clusterVm = Get-ClusterGroup -Name $VMName -ErrorAction SilentlyContinue - if (-not $clusterVm -and $ThrowOnEmpty.IsPresent) + + if ($clusterVm) { - $errorMessage = $script:localizedData.NoVMExistsError -f $VMName - New-InvalidResultException -Message $errorMessage + [Microsoft.HyperV.PowerShell.VirtualMachine[]]$clusteredVm = Get-VM @vmParam -ClusterObject $clusterVm } - if (-not $clusterVm) + if ($clusteredVm) { - return + $vms.AddRange($clusteredVm) } - - $vmParam['ClusterObject'] = $clusterVm } - else + + [Microsoft.HyperV.PowerShell.VirtualMachine[]]$nonClusteredVm = Get-VM @vmParam -VMName $VMName | Where-Object IsClustered -eq $false + + if ($nonClusteredVm) { - $vmParam['Name'] = $VMName + $vms.AddRange($nonClusteredVm) } - $vm = Get-VM @vmParam - # Check if 1 or 0 VM with name = $name exist - if ($vm.count -gt 1) + if ($vms.count -gt 1) { $errorMessage = $script:localizedData.MoreThanOneVMExistsError -f $VMName New-InvalidResultException -Message $errorMessage } - if (-not $vm -and $ThrowOnEmpty.IsPresent) + if (-not $vms -and $ThrowOnEmpty.IsPresent) { $errorMessage = $script:localizedData.NoVMExistsError -f $VMName New-InvalidResultException -Message $errorMessage } - return $vm + return $vms } #end function Get-VMHyperV diff --git a/tests/Unit/DSC_VMHyperV.Tests.ps1 b/tests/Unit/DSC_VMHyperV.Tests.ps1 index aea320f..841fb8c 100644 --- a/tests/Unit/DSC_VMHyperV.Tests.ps1 +++ b/tests/Unit/DSC_VMHyperV.Tests.ps1 @@ -503,10 +503,6 @@ try $targetResource = Get-TargetResource -Name 'VMWithAutomaticCheckpoints' -VhdPath $stubVhdxDisk.Path $targetResource.ContainsKey('AutomaticCheckpointsEnabled') | Should -Be $true } - It 'Hash table contains key IsClustered' { - $targetResource = Get-TargetResource -Name 'ClusteredExistingVmInClusterPresent' -VhdPath $stubVhdxDisk.Path - $targetResource.ContainsKey('IsClustered') | Should -Be $true - } It 'throws when Hyper-V Tools are not installed' { # This test needs to be the last in the Context otherwise all subsequent Get-Module checks will fail Mock -CommandName Get-Module -ParameterFilter { ($Name -eq 'Hyper-V') -and ($ListAvailable -eq $true) } diff --git a/tests/Unit/HyperVDsc.Common.Tests.ps1 b/tests/Unit/HyperVDsc.Common.Tests.ps1 index bc9c6f7..afdd1f8 100644 --- a/tests/Unit/HyperVDsc.Common.Tests.ps1 +++ b/tests/Unit/HyperVDsc.Common.Tests.ps1 @@ -300,9 +300,9 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' { It 'Should not throw when one VM is found' { Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' -MockWith { - [PSCustomObject] @{ - Name = $VMName - } + $mockVm = [Microsoft.HyperV.PowerShell.VirtualMachine]::CreateTypeInstance() + $mockVm.Name = $VMName + $mockVm } $result = Get-VMHyperV -VMName $mockVMName @@ -313,12 +313,9 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' { It 'Should throw when more than one VM is found' { Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' -MockWith { @( - [PSCustomObject] @{ - Name = $VMName - }, - [PSCustomObject] @{ - Name = $VMName - } + $mockVm = [Microsoft.HyperV.PowerShell.VirtualMachine]::CreateTypeInstance() + $mockVm.Name = $VMName + @($mockVm, $mockVm) ) } @@ -340,9 +337,11 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' { [Microsoft.FailoverClusters.PowerShell.ClusterObject]::new() } Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' -MockWith { - [PSCustomObject] @{ - Name = $VMName - } + $mockVm = [Microsoft.HyperV.PowerShell.VirtualMachine]::CreateTypeInstance() + $mockVm.Name = $VMName + # Test in case local cluster node is holding the machine object + $mockVm.IsClustered = $true + $mockVm } $result = Get-VMHyperV -VMName $mockVMName @@ -350,11 +349,16 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' { } It 'Should not throw when no clustered VM is found' { + Mock -CommandName Get-ClusterGroup -ModuleName 'HyperVDsc.Common' + Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' + $result = Get-VMHyperV -VMName $mockVMName $result | Should -BeNullOrEmpty } It 'Should throw when no clustered VM is found and caller wants it to' { + Mock -CommandName Get-ClusterGroup -ModuleName 'HyperVDsc.Common' + Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' { Get-VMHyperV -VMName $mockVMName -ThrowOnEmpty -ErrorAction Stop} | Should -Throw -ExpectedMessage "No VM with the name '$mockVMName' exists." } }