diff --git a/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 b/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 index 058c12c..bd62371 100644 --- a/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 +++ b/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 @@ -228,48 +228,54 @@ function Test-TargetResource # If switch should be present, check the switch type if($Ensure -eq 'Present') { - # If the BandwidthReservsationMode is correct, or if $switch.BandwidthReservationMode is $null which means it isn't supported on the OS - Write-Verbose -Message "Checking if Switch $Name has correct BandwidthReservationMode ..." - if($switch.BandwidthReservationMode -eq $BandwidthReservationMode -or $switch.BandwidthReservationMode -eq $null) + ## Only check the BandwidthReservationMode if specified + if($PSBoundParameters.ContainsKey('BandwidthReservationMode')) { - Write-Verbose -Message "Switch $Name has correct BandwidthReservationMode or it does not apply to this OS" + # If the BandwidthReservationMode is correct, or if $switch.BandwidthReservationMode is $null which means it isn't supported on the OS + Write-Verbose -Message "Checking if Switch $Name has correct BandwidthReservationMode ..." + if($switch.BandwidthReservationMode -eq $BandwidthReservationMode -or $switch.BandwidthReservationMode -eq $null) + { + Write-Verbose -Message "Switch $Name has correct BandwidthReservationMode or it does not apply to this OS" + } + else + { + Write-Verbose -Message "Switch $Name does not have correct BandwidthReservationMode " + return $false + } + } - # If switch is the external type, check additional propeties - if($switch.SwitchType -eq 'External') + # If switch is the external type, check additional propeties + if($switch.SwitchType -eq 'External') + { + Write-Verbose -Message "Checking if Switch $Name has correct NetAdapterInterface ..." + if((Get-NetAdapter -Name $NetAdapterName -ErrorAction SilentlyContinue).InterfaceDescription -ne $switch.NetAdapterInterfaceDescription) { - Write-Verbose -Message "Checking if Switch $Name has correct NetAdapterInterface ..." - if((Get-NetAdapter -Name $NetAdapterName -ErrorAction SilentlyContinue).InterfaceDescription -ne $switch.NetAdapterInterfaceDescription) + return $false + } + else + { + Write-Verbose -Message "Switch $Name has correct NetAdapterInterface" + } + + if($PSBoundParameters.ContainsKey("AllowManagementOS")) + { + Write-Verbose -Message "Checking if Switch $Name has AllowManagementOS set correctly..." + if(($switch.AllowManagementOS -ne $AllowManagementOS)) { return $false } else { - Write-Verbose -Message "Switch $Name has correct NetAdapterInterface" - } - - if($PSBoundParameters.ContainsKey("AllowManagementOS")) - { - Write-Verbose -Message "Checking if Switch $Name has AllowManagementOS set correctly..." - if(($switch.AllowManagementOS -ne $AllowManagementOS)) - { - return $false - } - else - { - Write-Verbose -Message "Switch $Name has AllowManagementOS set correctly" - } + Write-Verbose -Message "Switch $Name has AllowManagementOS set correctly" } - return $true - } - else - { - return $true } + return $true } else { - return $false + return $true } + } # If switch should be absent, but is there, return $false else diff --git a/README.md b/README.md index 5f587e7..ab69e4b 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ Please see the Examples section for more details. * MSFT_xVMHyperV: Fixed bug causing Test-TargetResource to fail when VM had snapshots. * MSFT_xVMHyperV: Adds localization support. +* MSFT_xVMSwitch: Fixes bug where virtual switches are duplicated when BandwidthReservationMode is not specified. ### 3.3.0.0 diff --git a/Tests/MSFT_xVMSwitch/xVMSwitch_BandwidthReservationMode.Tests.ps1 b/Tests/MSFT_xVMSwitch/xVMSwitch_BandwidthReservationMode.Tests.ps1 index cb9d1c6..62bb24d 100644 --- a/Tests/MSFT_xVMSwitch/xVMSwitch_BandwidthReservationMode.Tests.ps1 +++ b/Tests/MSFT_xVMSwitch/xVMSwitch_BandwidthReservationMode.Tests.ps1 @@ -278,6 +278,12 @@ Describe 'xVMSwitch' { $targetResource = Test-TargetResource -Name 'SomeSwitch' -BandwidthReservationMode 'NA' -Type 'External' -NetAdapterName 'SomeNIC' -Ensure 'Present' -AllowManagementOS $true $targetResource | Should Be $true } + + It 'Passes when "BandwidthReservationMode" does not match but is not specified (#48)' { + $global:mockedVMSwitch = New-MockedVMSwitch -Name 'SomeSwitch' -BandwidthReservationMode 'Absolute' + $targetResource = Test-TargetResource -Name 'SomeSwitch' -Type 'Internal' -Ensure 'Present' + $targetResource | Should Be $true + } }