From 3170f9ff6d2dbe512718522f0d53bff7e7a5b30f Mon Sep 17 00:00:00 2001 From: iainbrighton Date: Sat, 20 Feb 2016 15:30:45 +0000 Subject: [PATCH 1/2] Fixes #48 where duplicate switches are created when BandwidthReservationMode is not specified --- .../MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 | 63 ++++++++++--------- README.md | 1 + ...MSwitch_BandwidthReservationMode.Tests.ps1 | 6 ++ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 b/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 index 058c12c..cdc7657 100644 --- a/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 +++ b/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 @@ -228,48 +228,53 @@ 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) - { - Write-Verbose -Message "Switch $Name has correct BandwidthReservationMode or it does not apply to this OS" + ## Only check the BandwidthReservationMode if specified + if ($PSBoundParameters.ContainsKey('BandwidthReservationMode')) { + # 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) + { + return $false + } + else { - Write-Verbose -Message "Checking if Switch $Name has correct NetAdapterInterface ..." - if((Get-NetAdapter -Name $NetAdapterName -ErrorAction SilentlyContinue).InterfaceDescription -ne $switch.NetAdapterInterfaceDescription) + 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" + Write-Verbose -Message "Switch $Name has AllowManagementOS set correctly" } - - 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" - } - } - 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 + } } From 8c7b0c682efaae1fe66b3c4907008307db53e4e5 Mon Sep 17 00:00:00 2001 From: iainbrighton Date: Sat, 20 Feb 2016 15:33:21 +0000 Subject: [PATCH 2/2] Formatting update --- DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 b/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 index cdc7657..bd62371 100644 --- a/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 +++ b/DSCResources/MSFT_xVMSwitch/MSFT_xVMSwitch.psm1 @@ -229,7 +229,8 @@ function Test-TargetResource if($Ensure -eq 'Present') { ## Only check the BandwidthReservationMode if specified - if ($PSBoundParameters.ContainsKey('BandwidthReservationMode')) { + if($PSBoundParameters.ContainsKey('BandwidthReservationMode')) + { # 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)