diff --git a/CHANGELOG.md b/CHANGELOG.md index 39401b0d..c9b93a78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Updated CHANGELOG.md - Renamed NetworkingDSc to NetworkingDsc in CHANGELOG.md - fixes [Issue #513](https://github.com/dsccommunity/NetworkingDsc/issues/513). +- NetAdapterName + - Added new parameter HyperVNetworkAdapterName to filter if device naming is configured on a Hyper-V network adapter. - CI Pipeline - Updated pipeline files to match current DSC Community patterns - fixes [Issue #528](https://github.com/dsccommunity/NetworkingDsc/issues/528). - Updated HQRM and build steps to use windows-latest image. diff --git a/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.psm1 b/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.psm1 index bab3ad79..a1354e80 100644 --- a/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.psm1 +++ b/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.psm1 @@ -42,6 +42,9 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' .PARAMETER DriverDescription This is the driver description of the network adapter. + .PARAMETER HyperVNetworkAdapterName + This is the name of the network adapter to find if device naming is enabled. + .PARAMETER InterfaceNumber This is the interface number of the network adapter if more than one are returned by the parameters. @@ -96,6 +99,10 @@ function Get-TargetResource [System.String] $DriverDescription, + [Parameter()] + [System.String] + $HyperVNetworkAdapterName, + [Parameter()] [System.UInt32] $InterfaceNumber = 1, @@ -139,6 +146,7 @@ function Get-TargetResource InterfaceIndex = $adapter.InterfaceIndex InterfaceGuid = $adapter.InterfaceGuid DriverDescription = $adapter.DriverDescription + HyperVNetworkAdapterName = $adapter.HyperVNetworkAdapterName InterfaceNumber = $InterfaceNumber IgnoreMultipleMatchingAdapters = $IgnoreMultipleMatchingAdapters } @@ -177,6 +185,9 @@ function Get-TargetResource .PARAMETER DriverDescription This is the driver description of the network adapter. + .PARAMETER HyperVNetworkAdapterName + This is the name of the network adapter to find if device naming is enabled. + .PARAMETER InterfaceNumber This is the interface number of the network adapter if more than one are returned by the parameters. @@ -230,6 +241,10 @@ function Set-TargetResource [System.String] $DriverDescription, + [Parameter()] + [System.String] + $HyperVNetworkAdapterName, + [Parameter()] [System.UInt32] $InterfaceNumber = 1, @@ -293,6 +308,9 @@ function Set-TargetResource .PARAMETER DriverDescription This is the driver description of the network adapter. + .PARAMETER HyperVNetworkAdapterName + This is the name of the network adapter to find if device naming is enabled. + .PARAMETER InterfaceNumber This is the interface number of the network adapter if more than one are returned by the parameters. @@ -347,6 +365,10 @@ function Test-TargetResource [System.String] $DriverDescription, + [Parameter()] + [System.String] + $HyperVNetworkAdapterName, + [Parameter()] [System.UInt32] $InterfaceNumber = 1, diff --git a/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.schema.mof b/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.schema.mof index aa27f022..41ae7d3d 100644 --- a/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.schema.mof +++ b/source/DSCResources/DSC_NetAdapterName/DSC_NetAdapterName.schema.mof @@ -10,6 +10,7 @@ class DSC_NetAdapterName : OMI_BaseResource [Write, Description("This is the interface index of the network adapter to find.")] UInt32 InterfaceIndex; [Write, Description("This is the interface GUID of the network adapter to find.")] String InterfaceGuid; [Write, Description("This is the driver description of the network adapter.")] String DriverDescription; + [Write, Description("This is the name of the network adapter to find if device naming is enabled.")] String HyperVNetworkAdapterName; [Write, Description("This is the interface number of the network adapter if more than one are returned by the parameters.")] UInt32 InterfaceNumber; [Write, Description("This switch will suppress an error occurring if more than one matching adapter matches the parameters passed.")] Boolean IgnoreMultipleMatchingAdapters; }; diff --git a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 index 0b71bc8e..cdcd4b74 100644 --- a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 +++ b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 @@ -158,6 +158,10 @@ function Find-NetworkAdapter [System.UInt32] $InterfaceNumber = 1, + [Parameter()] + [System.String] + $HyperVNetworkAdapterName, + [Parameter()] [System.Boolean] $IgnoreMultipleMatchingAdapters = $false @@ -209,6 +213,11 @@ function Find-NetworkAdapter $adapterFilters += @('($_.DriverDescription -eq $DriverDescription)') } # if + if ($PSBoundParameters.ContainsKey('HyperVNetworkAdapterName')) + { + $adapterFilters += @('((Get-NetAdapterAdvancedProperty -Name $_.Name -RegistryKeyword HyperVNetworkAdapterName -ErrorAction SilentlyContinue).DisplayValue -eq $HyperVNetworkAdapterName)') + } # if + if ($adapterFilters.Count -eq 0) { Write-Verbose -Message ( @("$($MyInvocation.MyCommand): " diff --git a/tests/Unit/DSC_NetAdapterName.Tests.ps1 b/tests/Unit/DSC_NetAdapterName.Tests.ps1 index 700262bb..78c6cc4c 100644 --- a/tests/Unit/DSC_NetAdapterName.Tests.ps1 +++ b/tests/Unit/DSC_NetAdapterName.Tests.ps1 @@ -42,28 +42,31 @@ try $script:adapterInterfaceIndex = 2 $script:adapterInterfaceGuid = '75670D9B-5879-4DBA-BC99-86CDD33EB66A' $script:adapterDriverDescription = 'Hyper-V Virtual Ethernet Adapter' + $script:adapterHyperVNetworkAdapterName = 'hv-res-001' $script:adapterParameters = [PSObject]@{ - Name = $script:adapterName - NewName = $script:newAdapterName - PhysicalMediaType = $script:adapterPhysicalMediaType - Status = $script:adapterStatus - MacAddress = $script:adapterMacAddress - InterfaceDescription = $script:adapterInterfaceDescription - InterfaceIndex = $script:adapterInterfaceIndex - InterfaceGuid = $script:adapterInterfaceGuid - DriverDescription = $script:adapterDriverDescription + Name = $script:adapterName + NewName = $script:newAdapterName + PhysicalMediaType = $script:adapterPhysicalMediaType + Status = $script:adapterStatus + MacAddress = $script:adapterMacAddress + InterfaceDescription = $script:adapterInterfaceDescription + InterfaceIndex = $script:adapterInterfaceIndex + InterfaceGuid = $script:adapterInterfaceGuid + DriverDescription = $script:adapterDriverDescription + HyperVNetworkAdapterName = $script:adapterHyperVNetworkAdapterName } $script:mockAdapter = [PSObject]@{ - Name = $script:adapterName - PhysicalMediaType = $script:adapterPhysicalMediaType - Status = $script:adapterStatus - MacAddress = $script:adapterMacAddress - InterfaceDescription = $script:adapterInterfaceDescription - InterfaceIndex = $script:adapterInterfaceIndex - InterfaceGuid = $script:adapterInterfaceGuid - DriverDescription = $script:adapterDriverDescription + Name = $script:adapterName + PhysicalMediaType = $script:adapterPhysicalMediaType + Status = $script:adapterStatus + MacAddress = $script:adapterMacAddress + InterfaceDescription = $script:adapterInterfaceDescription + InterfaceIndex = $script:adapterInterfaceIndex + InterfaceGuid = $script:adapterInterfaceGuid + DriverDescription = $script:adapterDriverDescription + HyperVNetworkAdapterName = $script:adapterHyperVNetworkAdapterName } $script:mockRenamedAdapter = [PSObject]@{ @@ -141,6 +144,7 @@ try $script:result.InterfaceIndex | Should -Be $script:mockAdapter.InterfaceIndex $script:result.InterfaceGuid | Should -Be $script:mockAdapter.InterfaceGuid $script:result.DriverDescription | Should -Be $script:mockAdapter.DriverDescription + $script:result.HyperVNetworkAdapterName | Should -Be $script:mockAdapter.HyperVNetworkAdapterName } It 'Should call all the mocks' { diff --git a/tests/Unit/NetworkingDsc.Common.tests.ps1 b/tests/Unit/NetworkingDsc.Common.tests.ps1 index 4298a76c..eed43b68 100644 --- a/tests/Unit/NetworkingDsc.Common.tests.ps1 +++ b/tests/Unit/NetworkingDsc.Common.tests.ps1 @@ -77,6 +77,8 @@ InModuleScope $script:subModuleName { $adapterInterfaceIndex = 2 $adapterInterfaceGuid = '75670D9B-5879-4DBA-BC99-86CDD33EB66A' $adapterDriverDescription = 'Hyper-V Virtual Ethernet Adapter' + $adapterHyperVNetworkAdapterName = 'hv-res-001' + $nomatchAdapter = [PSObject]@{ Name = 'No Match Adapter' PhysicalMediaType = '802.11' @@ -380,6 +382,54 @@ InModuleScope $script:subModuleName { } } + Context 'HyperVNetworkAdapterName is passed and one adapter matches' { + Mock ` + -CommandName Get-NetAdapter ` + -MockWith { $adapterArray } + + Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith { + @{ + DisplayValue = $adapterHyperVNetworkAdapterName + } + } -ParameterFilter { $Name -eq $adapterName } + + Mock -CommandName Get-NetAdapterAdvancedProperty -ParameterFilter { $Name -eq 'No Match Adapter' } + + It 'Should not throw exception' { + { + $script:result = Find-NetworkAdapter -HyperVNetworkAdapterName $adapterHyperVNetworkAdapterName -Verbose + } | Should -Not -Throw + } + + It 'Should return expected adapter' { + $script:result.Name | Should -Be $adapterName + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Times 2 + } + } + + Context 'HyperVNetworkAdapterName is passed and no adapters match' { + Mock ` + -CommandName Get-NetAdapter ` + -MockWith { $adapterArray } + Mock -CommandName Get-NetAdapterAdvancedProperty + + $errorRecord = Get-InvalidOperationRecord ` + -Message ($script:localizedData.NetAdapterNotFoundError) + + It 'Should throw the correct exception' { + { $script:result = Find-NetworkAdapter -HyperVNetworkAdapterName 'NOMATCH' -Verbose } | Should -Throw $errorRecord + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-NetAdapter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Times 2 + } + } + Context 'No parameters are passed and multiple Adapters adapters match but IgnoreMultipleMatchingAdapters is not set' { Mock ` -CommandName Get-NetAdapter `