Skip to content

Commit

Permalink
Merge pull request #414 from MKletz/Issue413
Browse files Browse the repository at this point in the history
Proposed fix for Issue #413
  • Loading branch information
PlagueHO authored Aug 13, 2019
2 parents ad720b2 + 4bfa290 commit 37950e3
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Added Comment Based Help for `New-NotImplementedException` common
function - fixes [Issue #411](https://github.com/PowerShell/NetworkingDsc/issues/411).
- Added common function 'Format-Win32NetworkADapterFilterByNetConnectionID'
to properly accept wild cards for Win32_NetworkAdapter filters.
- Updated MSFT_Netbios to use 'Format-Win32NetworkADapterFilterByNetConnectionID'
- fixes [Issue #413](https://github.com/PowerShell/NetworkingDsc/issues/413).

## 7.3.0.0

Expand Down
18 changes: 12 additions & 6 deletions DSCResources/MSFT_NetBios/MSFT_NetBios.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ catch
Returns the current state of the Net Bios on an interface.
.PARAMETER InterfaceAlias
Specifies the alias of a network interface. Supports the use of '*'.
Specifies the alias of a network interface. Supports the use of '*' and '%'.
.PARAMETER Setting
Specifies if NetBIOS should be enabled or disabled or obtained from
Expand All @@ -57,9 +57,11 @@ function Get-TargetResource

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

$win32NetworkAdapterFilter = Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $InterfaceAlias

$netAdapter = Get-CimInstance `
-ClassName Win32_NetworkAdapter `
-Filter ('NetConnectionID="{0}"' -f $InterfaceAlias)
-Filter $win32NetworkAdapterFilter

if ($netAdapter)
{
Expand Down Expand Up @@ -99,7 +101,7 @@ function Get-TargetResource
Sets the state of the Net Bios on an interface.
.PARAMETER InterfaceAlias
Specifies the alias of a network interface. Supports the use of '*'.
Specifies the alias of a network interface. Supports the use of '*' and '%'.
.PARAMETER Setting
Specifies if NetBIOS should be enabled or disabled or obtained from
Expand All @@ -122,9 +124,11 @@ function Set-TargetResource

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

$win32NetworkAdapterFilter = Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $InterfaceAlias

$netAdapter = Get-CimInstance `
-ClassName Win32_NetworkAdapter `
-Filter ('NetConnectionID="{0}"' -f $InterfaceAlias)
-Filter $win32NetworkAdapterFilter

if ($netAdapter)
{
Expand Down Expand Up @@ -177,7 +181,7 @@ function Set-TargetResource
Tests the current state the Net Bios on an interface.
.PARAMETER InterfaceAlias
Specifies the alias of a network interface. Supports the use of '*'.
Specifies the alias of a network interface. Supports the use of '*' and '%'.
.PARAMETER Setting
Specifies if NetBIOS should be enabled or disabled or obtained from
Expand All @@ -201,9 +205,11 @@ function Test-TargetResource

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

$win32NetworkAdapterFilter = Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $InterfaceAlias

$netAdapter = Get-CimInstance `
-ClassName Win32_NetworkAdapter `
-Filter ('NetConnectionID="{0}"' -f $InterfaceAlias)
-Filter $win32NetworkAdapterFilter

if ($netAdapter)
{
Expand Down
2 changes: 1 addition & 1 deletion DSCResources/MSFT_NetBios/MSFT_NetBios.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
[ClassVersion("1.0.0.0"), FriendlyName("NetBios")]
class MSFT_NetBios : OMI_BaseResource
{
[Key, Description("Specifies the alias of a network interface.")] String InterfaceAlias;
[Key, Description("Specifies the alias of a network interface. Supports the use of '*' and '%'")] String InterfaceAlias;
[Required, Description("Specifies if NetBIOS should be enabled or disabled or obtained from the DHCP server (Default). If static IP, Enable NetBIOS."), ValueMap{"Default","Enable","Disable"}, Values{"Default","Enable","Disable"}] String Setting;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<#PSScriptInfo
.VERSION 1.0.0
.GUID b43f8fac-cc5a-45ec-ab7d-344dff0b31e7
.AUTHOR Mike Kletz
.COMPANYNAME
.COPYRIGHT
.TAGS DSCConfiguration
.LICENSEURI https://github.com/PowerShell/NetworkingDsc/blob/master/LICENSE
.PROJECTURI https://github.com/PowerShell/NetworkingDsc
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES First version.
.PRIVATEDATA 2016-Datacenter,2016-Datacenter-Server-Core
#>

#Requires -module NetworkingDsc

<#
.DESCRIPTION
Disable NetBios on all adapters.
#>
Configuration NetBios_DisableNetBios_Config_Wildcard
{
Import-DscResource -ModuleName NetworkingDsc

Node localhost
{
NetBios DisableNetBios
{
InterfaceAlias = '*'
Setting = 'Disable'
}
}
}
40 changes: 39 additions & 1 deletion Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,43 @@ function ConvertTo-HashTable
}
}

<#
.SYNOPSIS
Returns a filter string for the net adapter CIM instances. Wildcards supported.
.PARAMETER InterfaceAlias
Specifies the alias of a network interface. Supports the use of '*' or '%'.
#>
function Format-Win32NetworkAdapterFilterByNetConnectionID
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$InterfaceAlias
)

if ($InterfaceAlias.Contains('*'))
{
$InterfaceAlias = $InterfaceAlias.Replace('*','%')
}

if ($InterfaceAlias.Contains('%'))
{
$operator = ' LIKE '
}
else
{
$operator = '='
}

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

$returnNetAdapaterFilter
}

# Import Localization Strings
$script:localizedData = Get-LocalizedData `
-ResourceName 'NetworkingDsc.Common' `
Expand All @@ -1388,5 +1425,6 @@ Export-ModuleMember -Function @(
'Test-DscParameterState',
'Test-DscObjectHasProperty'
'ConvertTo-HashTable',
'ConvertTo-CimInstance'
'ConvertTo-CimInstance',
'Format-Win32NetworkAdapterFilterByNetConnectionID'
)
41 changes: 41 additions & 0 deletions Tests/Unit/NetworkingDsc.Common.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,47 @@ try
}
}
}

Describe 'NetworkingDsc.Common\Format-Win32NetworkADapterFilterByNetConnectionID'{
Context 'When interface alias has an ''*''' {
$interfaceAlias = 'Ether*'

It 'Should convert the ''*'' to a ''%''' {
(Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias).Contains('%') -eq $True -and
(Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias).Contains('*') -eq $False | Should -Be $True
}

It 'Should change the operator to ''LIKE''' {
(Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias) | Should -BeExactly 'NetConnectionID LIKE "Ether%"'
}

It 'Should look like a usable filter' {
Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias | Should -BeExactly 'NetConnectionID LIKE "Ether%"'
}

}

Context 'When interface alias has a ''%''' {
$interfaceAlias = 'Ether%'

It 'Should change the operator to ''LIKE''' {
(Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias) | Should -BeExactly 'NetConnectionID LIKE "Ether%"'
}

It 'Should look like a usable filter' {
Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias | Should -BeExactly 'NetConnectionID LIKE "Ether%"'
}

}

Context 'When interface alias has no wildcards' {
$interfaceAlias = 'Ethernet'

It 'Should look like a usable filter' {
Format-Win32NetworkADapterFilterByNetConnectionID -InterfaceAlias $interfaceAlias | Should -BeExactly 'NetConnectionID="Ethernet"'
}
}
}
}
}
finally
Expand Down

0 comments on commit 37950e3

Please sign in to comment.