Skip to content

Commit

Permalink
Fix NetBios Wildcard Support - Fixes #444 (#458)
Browse files Browse the repository at this point in the history
* Improve integration tests for NetBios

* Fix integration tests in NetBios

* Improve NetBios integration tests

* Add integration tests for NetBios multiple adapters

* Refactor NetBios

* More verbose

* Fix tests

* Improve integration test

* Updated CHANGELOG.md

* Fix unit tests

* Improve unit tests in NetBios

* Refactored NetBios tests to reduce code

* Complete unit tests on NetBios

* Complete unit tests on NetBios

* Fix typo in NetBios
  • Loading branch information
PlagueHO authored Jun 21, 2020
1 parent 5efc9b3 commit 7da8e5a
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 437 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- ComputerManagementDsc
- NetworkingDsc:
- Added build task `Generate_Conceptual_Help` to generate conceptual help
for the DSC resource.
- Added build task `Generate_Wiki_Content` to generate the wiki content
that can be used to update the GitHub Wiki.
- Common
- Common:
- Added Assert-IPAddress function to reduce code duplication - Fixes
[Issue #408](https://github.com/dsccommunity/NetworkingDsc/issues/408).

### Changed

- NetworkingDsc
- NetworkingDsc:
- Updated to use the common module _DscResource.Common_.
- Fixed build failures caused by changes in `ModuleBuilder` module v1.7.0
by changing `CopyDirectories` to `CopyPaths` - Fixes [Issue #455](https://github.com/dsccommunity/NetworkingDsc/issues/455).
Expand All @@ -43,6 +43,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Change Azure DevOps Pipeline definition to include `source/*` - Fixes [Issue #450](https://github.com/dsccommunity/NetworkingDsc/issues/450).
- Updated pipeline to use `latest` version of `ModuleBuilder` - Fixes [Issue #451](https://github.com/dsccommunity/NetworkingDsc/issues/451).
- Merge `HISTORIC_CHANGELOG.md` into `CHANGELOG.md` - Fixes [Issue #451](https://github.com/dsccommunity/NetworkingDsc/issues/451).
- NetBios:
- Improved integration tests by using loopback adapter.
- Refactored unit tests to reduce code duplication and
increase coverage.
- Fix exception when specifying wildcard '*' in the
`InterfaceAlias` - Fixes [Issue #444](https://github.com/dsccommunity/NetworkingDsc/issues/444).

### Deprecated

Expand Down
133 changes: 69 additions & 64 deletions source/DSCResources/DSC_NetBios/DSC_NetBios.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,63 @@ function Get-TargetResource
$InterfaceAlias,

[Parameter(Mandatory = $true)]
[ValidateSet("Default", "Enable", "Disable")]
[ValidateSet('Default', 'Enable', 'Disable')]
[System.String]
$Setting
)

Write-Verbose -Message ($script:localizedData.GettingNetBiosSettingMessage -f $InterfaceAlias)

$win32NetworkAdapterFilter = Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $InterfaceAlias
$win32NetworkAdapterFilter = Format-Win32NetworkAdapterFilterByNetConnectionId -InterfaceAlias $InterfaceAlias

$netAdapter = Get-CimInstance `
-ClassName Win32_NetworkAdapter `
-Filter $win32NetworkAdapterFilter

if ($netAdapter)
{
Write-Verbose -Message ($script:localizedData.InterfaceDetectedMessage -f $InterfaceAlias, $netAdapter.InterfaceIndex)
Write-Verbose -Message ($script:localizedData.InterfaceDetectedMessage -f $InterfaceAlias, ($netAdapter.InterfaceIndex -Join ','))
}
else
{
New-InvalidOperationException `
-Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias)
}

$netAdapterConfig = $netAdapter | Get-CimAssociatedInstance `
-ResultClassName Win32_NetworkAdapterConfiguration `
-ErrorAction Stop
<#
If a wildcard was specified for the InterfaceAlias then
more than one adapter may be returned. If more than one
adapter is returned then the NetBiosSetting value should
be returned for the first adapter that does not match
the desired value. This is to ensure that when testing
the resource state it will return a mismatch if any adapters
don't have the correct setting.
#>
foreach ($netAdapterItem in $netAdapter)
{
$netAdapterConfig = $netAdapterItem | Get-CimAssociatedInstance `
-ResultClassName Win32_NetworkAdapterConfiguration `
-ErrorAction Stop

$tcpipNetbiosOptions = $netAdapterConfig.TcpipNetbiosOptions
$tcpipNetbiosOptions = $netAdapterConfig.TcpipNetbiosOptions

if ($tcpipNetbiosOptions)
{
$Setting = $([NetBiosSetting].GetEnumValues()[$tcpipNetbiosOptions])
}
else
{
$Setting = 'Default'
}
if ($tcpipNetbiosOptions)
{
$interfaceSetting = $([NetBiosSetting].GetEnumValues()[$tcpipNetbiosOptions])
}
else
{
$interfaceSetting = 'Default'
}

Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $netAdapterItem.Name, $interfaceSetting)

Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $Setting)
if ($interfaceSetting -ne $Setting)
{
$Setting = $interfaceSetting
break
}
}

return @{
InterfaceAlias = $InterfaceAlias
Expand Down Expand Up @@ -119,61 +137,64 @@ function Set-TargetResource
$InterfaceAlias,

[Parameter(Mandatory = $true)]
[ValidateSet("Default", "Enable", "Disable")]
[ValidateSet('Default', 'Enable', 'Disable')]
[System.String]
$Setting
)

Write-Verbose -Message ($script:localizedData.SettingNetBiosSettingMessage -f $InterfaceAlias)

$win32NetworkAdapterFilter = Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $InterfaceAlias
$win32NetworkAdapterFilter = Format-Win32NetworkAdapterFilterByNetConnectionId -InterfaceAlias $InterfaceAlias

$netAdapter = Get-CimInstance `
-ClassName Win32_NetworkAdapter `
-Filter $win32NetworkAdapterFilter

if ($netAdapter)
{
Write-Verbose -Message ($script:localizedData.InterfaceDetectedMessage -f $InterfaceAlias, $netAdapter.InterfaceIndex)
Write-Verbose -Message ($script:localizedData.InterfaceDetectedMessage -f $InterfaceAlias, ($netAdapter.InterfaceIndex -Join ','))
}
else
{
New-InvalidOperationException `
-Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias)
}

$netAdapterConfig = $netAdapter | Get-CimAssociatedInstance `
-ResultClassName Win32_NetworkAdapterConfiguration `
-ErrorAction Stop

if ($Setting -eq [NetBiosSetting]::Default)
foreach ($netAdapterItem in $netAdapter)
{
Write-Verbose -Message $script:localizedData.ResetToDefaultMessage
$netAdapterConfig = $netAdapterItem | Get-CimAssociatedInstance `
-ResultClassName Win32_NetworkAdapterConfiguration `
-ErrorAction Stop

# If DHCP is not enabled, SetTcpipNetbios CIM Method won't take 0 so overwrite registry entry instead.
$setItemPropertyParameters = @{
Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetAdapterConfig.SettingID)"
Name = 'NetbiosOptions'
Value = 0
if ($Setting -eq [NetBiosSetting]::Default)
{
Write-Verbose -Message ($script:localizedData.ResetToDefaultMessage -f $netAdapterItem.Name)

# If DHCP is not enabled, SetTcpipNetbios CIM Method won't take 0 so overwrite registry entry instead.
$setItemPropertyParameters = @{
Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetAdapterConfig.SettingID)"
Name = 'NetbiosOptions'
Value = 0
}
$null = Set-ItemProperty @setItemPropertyParameters
}
$null = Set-ItemProperty @setItemPropertyParameters
}
else
{
Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $Setting)

$result = $netAdapterConfig |
Invoke-CimMethod `
-MethodName SetTcpipNetbios `
-ErrorAction Stop `
-Arguments @{
TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__
}

if ($result.ReturnValue -ne 0)
else
{
New-InvalidOperationException `
-Message ($script:localizedData.FailedUpdatingNetBiosError -f $result.ReturnValue, $Setting)
Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $netAdapterItem.Name, $Setting)

$result = $netAdapterConfig |
Invoke-CimMethod `
-MethodName SetTcpipNetbios `
-ErrorAction Stop `
-Arguments @{
TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__
}

if ($result.ReturnValue -ne 0)
{
New-InvalidOperationException `
-Message ($script:localizedData.FailedUpdatingNetBiosError -f $netAdapterItem.Name, $result.ReturnValue, $Setting)
}
}
}
}
Expand All @@ -200,29 +221,13 @@ function Test-TargetResource
$InterfaceAlias,

[Parameter(Mandatory = $true)]
[ValidateSet("Default", "Enable", "Disable")]
[ValidateSet('Default', 'Enable', 'Disable')]
[System.String]
$Setting
)

Write-Verbose -Message ($script:localizedData.TestingNetBiosSettingMessage -f $InterfaceAlias)

$win32NetworkAdapterFilter = Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $InterfaceAlias

$netAdapter = Get-CimInstance `
-ClassName Win32_NetworkAdapter `
-Filter $win32NetworkAdapterFilter

if ($netAdapter)
{
Write-Verbose -Message ($script:localizedData.InterfaceDetectedMessage -f $InterfaceAlias, $netAdapter.InterfaceIndex)
}
else
{
New-InvalidOperationException `
-Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias)
}

$currentState = Get-TargetResource @PSBoundParameters

return Test-DscParameterState -CurrentValues $currentState -DesiredValues $PSBoundParameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ ConvertFrom-StringData @'
GettingNetBiosSettingMessage = Getting NetBIOS configuration for Interface '{0}'.
InterfaceDetectedMessage = Interface '{0}' detected with Index number {1}.
SettingNetBiosSettingMessage = Setting NetBIOS configuration for Interface '{0}'.
ResetToDefaultMessage = NetBIOS configuration will be reset to default.
SetNetBiosMessage = NetBIOS configuration will be set to '{0}'.
ResetToDefaultMessage = NetBIOS configuration for Interface '{0}' will be reset to default.
SetNetBiosMessage = NetBIOS configuration for Interface '{0}' will be set to '{1}'.
TestingNetBiosSettingMessage = Testing NetBIOS configuration for Interface '{0}'.
CurrentNetBiosSettingMessage = Current NetBIOS configuration is '{0}'.
CurrentNetBiosSettingMessage = Current NetBIOS configuration for Interface '{0}' is '{1}'.
InterfaceNotFoundError = Interface '{0}' was not found.
FailedUpdatingNetBiosError = An error result of '{0}' was returned when attemting to set NetBIOS configuration to '{1}'.
FailedUpdatingNetBiosError = An error result of '{1}' was returned when attemting to set NetBIOS for Interface '{0}' configuration to '{2}'.
'@
8 changes: 4 additions & 4 deletions source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ function Get-IPAddressPrefix
.PARAMETER InterfaceAlias
Specifies the alias of a network interface. Supports the use of '*' or '%'.
#>
function Format-Win32NetworkAdapterFilterByNetConnectionID
function Format-Win32NetworkAdapterFilterByNetConnectionId
{
[CmdletBinding()]
[OutputType([System.String])]
Expand All @@ -566,9 +566,9 @@ function Format-Win32NetworkAdapterFilterByNetConnectionID
$operator = '='
}

$returnNetAdapaterFilter = 'NetConnectionID{0}"{1}"' -f $operator,$InterfaceAlias
$returnNetAdapaterFilter = 'NetConnectionID{0}"{1}"' -f $operator, $InterfaceAlias

$returnNetAdapaterFilter
return $returnNetAdapaterFilter
}

Export-ModuleMember -Function @(
Expand All @@ -578,5 +578,5 @@ Export-ModuleMember -Function @(
'Get-WinsClientServerStaticAddress'
'Set-WinsClientServerStaticAddress'
'Get-IPAddressPrefix'
'Format-Win32NetworkAdapterFilterByNetConnectionID'
'Format-Win32NetworkAdapterFilterByNetConnectionId'
)
Loading

0 comments on commit 7da8e5a

Please sign in to comment.