From 47d9636bf664eb40632a333254e45e6734106ae1 Mon Sep 17 00:00:00 2001 From: phbits Date: Tue, 27 Oct 2020 20:21:12 +0000 Subject: [PATCH 01/11] fixes #434, added two functions to common. --- .../DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 94 +++++++------- .../NetworkingDsc.Common.psm1 | 116 ++++++++++++++++++ 2 files changed, 160 insertions(+), 50 deletions(-) diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 064580b6..b8857967 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -78,37 +78,35 @@ function Get-TargetResource <# 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. + adapter is returned then the NetBios setting value will + be evaluated for all matching adapters. If there is a + mismatch, a wrong value is returned to signify the + resource is not in the desired state. #> - foreach ($netAdapterItem in $netAdapter) + if( $netAdapter -is [Array] ) { - $netAdapterConfig = $netAdapterItem | Get-CimAssociatedInstance ` - -ResultClassName Win32_NetworkAdapterConfiguration ` - -ErrorAction Stop + [string[]] $SettingResults = @() - $tcpipNetbiosOptions = $netAdapterConfig.TcpipNetbiosOptions - - if ($tcpipNetbiosOptions) - { - $interfaceSetting = $([NetBiosSetting].GetEnumValues()[$tcpipNetbiosOptions]) - } - else + foreach ( $netAdapterItem in $netAdapter ) { - $interfaceSetting = 'Default' + $SettingResults += Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting + + Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $netAdapterItem.NetConnectionID,$SettingResults[-1]) } - Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $netAdapterItem.Name, $interfaceSetting) + [string[]] $WrongSettings = $SettingResults | Where-Object{ $_ -ne $Setting } - if ($interfaceSetting -ne $Setting) + if([System.String]::IsNullOrEmpty($WrongSettings) -eq $false) { - $Setting = $interfaceSetting - break + [string] $Setting = $WrongSettings[0] } } + else + { + $Setting = Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapter.GUID -Setting $Setting + } + + Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $InterfaceAlias, $Setting) return @{ InterfaceAlias = $InterfaceAlias @@ -160,43 +158,39 @@ function Set-TargetResource -Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias) } - foreach ($netAdapterItem in $netAdapter) + if( $netAdapter -is [Array] ) { - $netAdapterConfig = $netAdapterItem | Get-CimAssociatedInstance ` - -ResultClassName Win32_NetworkAdapterConfiguration ` - -ErrorAction Stop - - if ($Setting -eq [NetBiosSetting]::Default) + foreach( $netAdapterItem in $netAdapter ) { - Write-Verbose -Message ($script:localizedData.ResetToDefaultMessage -f $netAdapterItem.Name) + $CurrentValue = Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting - # 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 - } - else - { - Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $netAdapterItem.Name, $Setting) + # Only make changes if necessary + if( $CurrentValue -ne $Setting ) + { + Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $netAdapterItem.NetConnectionID, $Setting) - $result = $netAdapterConfig | - Invoke-CimMethod ` - -MethodName SetTcpipNetbios ` - -ErrorAction Stop ` - -Arguments @{ - TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__ - } + $netAdapterConfig = $netAdapterItem | Get-CimAssociatedInstance ` + -ResultClassName Win32_NetworkAdapterConfiguration ` + -ErrorAction Stop - if ($result.ReturnValue -ne 0) - { - New-InvalidOperationException ` - -Message ($script:localizedData.FailedUpdatingNetBiosError -f $netAdapterItem.Name, $result.ReturnValue, $Setting) + Set-NetAdapterNetbiosOptions -NetworkAdapterObj $netAdapterConfig ` + -InterfaceAlias $netAdapterItem.NetConnectionID ` + -Setting $Setting } } } + else + { + Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $netAdapter.NetConnectionID, $Setting) + + $netAdapterConfig = $netAdapter | Get-CimAssociatedInstance ` + -ResultClassName Win32_NetworkAdapterConfiguration ` + -ErrorAction Stop + + Set-NetAdapterNetbiosOptions -NetworkAdapterObj $netAdapterConfig ` + -InterfaceAlias $netAdapter.NetConnectionID ` + -Setting $Setting + } } <# diff --git a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 index d9347fa4..a2c66a48 100644 --- a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 +++ b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 @@ -571,6 +571,120 @@ function Format-Win32NetworkAdapterFilterByNetConnectionId return $returnNetAdapaterFilter } +<# +.SYNOPSIS + Returns the NetbiosOptions value for a network adapter. +.DESCRIPTION + Most reliable method of getting this value since network adapters + can be in any number of states (e.g. disabled, disconnected) + which can cause Win32 classes to not report the value. +#> +function Get-NetAdapterNetbiosOptionsFromRegistry +{ + [OutputType([System.String])] + [CmdletBinding()] + [CmdletBinding()] + param + ( + [Parameter(Mandatory=$true)] + [ValidatePattern("^\{[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}\}$")] + [System.String] + # Network Adapter GUID + $NetworkAdapterGUID + , + [Parameter(Mandatory=$true)] + [ValidateSet('Default','Enable','Disable')] + [System.String] + $Setting + ) + + # Changing ErrorActionPreference variable since the switch -ErrorAction isn't supported. + $CurrentErrorActionPreference = $ErrorActionPreference + $ErrorActionPreference = 'SilentlyContinue' + + $RegistryNetbiosOptions = Get-ItemPropertyValue -Name 'NetbiosOptions' ` + -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterGUID)" + + $ErrorActionPreference = $CurrentErrorActionPreference + + if ( $null -eq $RegistryNetbiosOptions ) + { + $RegistryNetbiosOptions = 0 + } + + switch( $RegistryNetbiosOptions ) + { + 0 { return 'Default' } + 1 { return 'Enable' } + 2 { return 'Disable' } + default { + + # Unknown value. Returning invalid setting to trigger Set-TargetResource + [string[]] $InvalidSetting = 'Default','Enable','Disable' | Where-Object{ $_ -ne $Setting } + + return $InvalidSetting[0] + } + } +} # end function Get-NetAdapterNetbiosOptionsFromRegistry + +<# +.SYNOPSIS + Configures Netbios on a Network Adapter. +#> +function Set-NetAdapterNetbiosOptions +{ + [CmdletBinding()] + [CmdletBinding()] + param + ( + [Parameter(Mandatory=$true)] + [System.Object] + # Network Adapter Win32_NetworkAdapterConfiguration Object + $NetworkAdapterObj + , + [Parameter(Mandatory=$true)] + [System.String] + $InterfaceAlias + , + [Parameter(Mandatory=$true)] + [ValidateSet('Default','Enable','Disable')] + [System.String] + $Setting + ) + + Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias,$Setting) + + # Only IPEnabled interfaces can be configured via SetTcpipNetbios method. + if( $NetworkAdapterObj.IPEnabled ) + { + $result = $NetworkAdapterObj | + Invoke-CimMethod ` + -MethodName SetTcpipNetbios ` + -ErrorAction Stop ` + -Arguments @{ + TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__ + } + + if ( $result.ReturnValue -ne 0 ) + { + New-InvalidOperationException ` + -Message ($script:localizedData.FailedUpdatingNetBiosError -f $InterfaceAlias,$result.ReturnValue,$Setting) + } + } + else + { + # IPEnabled=$false can only be configured via registry + # this satisfies disabled and disconnected states + $setItemPropertyParameters = @{ + Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterObj.SettingID)" + Name = 'NetbiosOptions' + Value = [NetBiosSetting]::$Setting.value__ + } + $null = Set-ItemProperty @setItemPropertyParameters + } + +} # end function Set-NetAdapterNetbiosOptions + Export-ModuleMember -Function @( 'Convert-CIDRToSubhetMask' 'Find-NetworkAdapter' @@ -579,4 +693,6 @@ Export-ModuleMember -Function @( 'Set-WinsClientServerStaticAddress' 'Get-IPAddressPrefix' 'Format-Win32NetworkAdapterFilterByNetConnectionId' + 'Get-NetAdapterNetbiosOptionsFromRegistry' + 'Set-NetAdapterNetbiosOptions' ) From 3d71023fef060f402276d9a51ab0c3eff280bd6f Mon Sep 17 00:00:00 2001 From: phbits Date: Fri, 30 Oct 2020 17:28:47 +0000 Subject: [PATCH 02/11] HQRM adds, moved the 2 new common functions to DSC_NetBios.psm1 --- .../DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 130 ++++++++++++++++++ .../NetworkingDsc.Common.psm1 | 116 ---------------- 2 files changed, 130 insertions(+), 116 deletions(-) diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index b8857967..13241365 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -227,4 +227,134 @@ function Test-TargetResource return Test-DscParameterState -CurrentValues $currentState -DesiredValues $PSBoundParameters } +<# +.SYNOPSIS + Returns the NetbiosOptions value for a network adapter. +.DESCRIPTION + Most reliable method of getting this value since network adapters + can be in any number of states (e.g. disabled, disconnected) + which can cause Win32 classes to not report the value. +.PARAMETER NetworkAdapterGUID + Network Adapter GUID +.PARAMETER Setting + Setting value for this resource which should be one of + the following: Default, Enable, Disable +#> +function Get-NetAdapterNetbiosOptionsFromRegistry +{ + [OutputType([System.String])] + [CmdletBinding()] + param + ( + [Parameter(Mandatory=$true)] + [ValidatePattern("^\{[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}\}$")] + [System.String] + # Network Adapter GUID + $NetworkAdapterGUID + , + [Parameter(Mandatory=$true)] + [ValidateSet('Default','Enable','Disable')] + [System.String] + $Setting + ) + + # Changing ErrorActionPreference variable since the switch -ErrorAction isn't supported. + $CurrentErrorActionPreference = $ErrorActionPreference + $ErrorActionPreference = 'SilentlyContinue' + + $RegistryNetbiosOptions = Get-ItemPropertyValue -Name 'NetbiosOptions' ` + -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterGUID)" + + $ErrorActionPreference = $CurrentErrorActionPreference + + if ( $null -eq $RegistryNetbiosOptions ) + { + $RegistryNetbiosOptions = 0 + } + + switch( $RegistryNetbiosOptions ) + { + 0 { return 'Default' } + 1 { return 'Enable' } + 2 { return 'Disable' } + default { + + # Unknown value. Returning invalid setting to trigger Set-TargetResource + [string[]] $InvalidSetting = 'Default','Enable','Disable' | Where-Object{ $_ -ne $Setting } + + return $InvalidSetting[0] + } + } +} # end function Get-NetAdapterNetbiosOptionsFromRegistry + +<# +.SYNOPSIS + Configures Netbios on a Network Adapter. +.DESCRIPTION + Uses two methods for configuring Netbios on a Network Adapter. + If an interface is IPEnabled, the CIMMethod will be invoked. + Otherwise the registry key is configured as this will satisfy + network adapters being in alternative states such as disabled + or disconnected. +.PARAMETER NetworkAdapterObj + Network Adapter Win32_NetworkAdapterConfiguration Object +.PARAMETER InterfaceAlias + Name of the network adapter being configured. Example: Ethernet +.PARAMETER Setting + Setting value for this resource which should be one of + the following: Default, Enable, Disable +#> +function Set-NetAdapterNetbiosOptions +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory=$true)] + [System.Object] + # Network Adapter Win32_NetworkAdapterConfiguration Object + $NetworkAdapterObj + , + [Parameter(Mandatory=$true)] + [System.String] + $InterfaceAlias + , + [Parameter(Mandatory=$true)] + [ValidateSet('Default','Enable','Disable')] + [System.String] + $Setting + ) + + Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias,$Setting) + + # Only IPEnabled interfaces can be configured via SetTcpipNetbios method. + if( $NetworkAdapterObj.IPEnabled ) + { + $result = $NetworkAdapterObj | + Invoke-CimMethod ` + -MethodName SetTcpipNetbios ` + -ErrorAction Stop ` + -Arguments @{ + TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__ + } + + if ( $result.ReturnValue -ne 0 ) + { + New-InvalidOperationException ` + -Message ($script:localizedData.FailedUpdatingNetBiosError -f $InterfaceAlias,$result.ReturnValue,$Setting) + } + } + else + { + # IPEnabled=$false can only be configured via registry + # this satisfies disabled and disconnected states + $setItemPropertyParameters = @{ + Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterObj.SettingID)" + Name = 'NetbiosOptions' + Value = [NetBiosSetting]::$Setting.value__ + } + $null = Set-ItemProperty @setItemPropertyParameters + } + +} # end function Set-NetAdapterNetbiosOptions + Export-ModuleMember -Function *-TargetResource diff --git a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 index a2c66a48..d9347fa4 100644 --- a/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 +++ b/source/Modules/NetworkingDsc.Common/NetworkingDsc.Common.psm1 @@ -571,120 +571,6 @@ function Format-Win32NetworkAdapterFilterByNetConnectionId return $returnNetAdapaterFilter } -<# -.SYNOPSIS - Returns the NetbiosOptions value for a network adapter. -.DESCRIPTION - Most reliable method of getting this value since network adapters - can be in any number of states (e.g. disabled, disconnected) - which can cause Win32 classes to not report the value. -#> -function Get-NetAdapterNetbiosOptionsFromRegistry -{ - [OutputType([System.String])] - [CmdletBinding()] - [CmdletBinding()] - param - ( - [Parameter(Mandatory=$true)] - [ValidatePattern("^\{[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}\}$")] - [System.String] - # Network Adapter GUID - $NetworkAdapterGUID - , - [Parameter(Mandatory=$true)] - [ValidateSet('Default','Enable','Disable')] - [System.String] - $Setting - ) - - # Changing ErrorActionPreference variable since the switch -ErrorAction isn't supported. - $CurrentErrorActionPreference = $ErrorActionPreference - $ErrorActionPreference = 'SilentlyContinue' - - $RegistryNetbiosOptions = Get-ItemPropertyValue -Name 'NetbiosOptions' ` - -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterGUID)" - - $ErrorActionPreference = $CurrentErrorActionPreference - - if ( $null -eq $RegistryNetbiosOptions ) - { - $RegistryNetbiosOptions = 0 - } - - switch( $RegistryNetbiosOptions ) - { - 0 { return 'Default' } - 1 { return 'Enable' } - 2 { return 'Disable' } - default { - - # Unknown value. Returning invalid setting to trigger Set-TargetResource - [string[]] $InvalidSetting = 'Default','Enable','Disable' | Where-Object{ $_ -ne $Setting } - - return $InvalidSetting[0] - } - } -} # end function Get-NetAdapterNetbiosOptionsFromRegistry - -<# -.SYNOPSIS - Configures Netbios on a Network Adapter. -#> -function Set-NetAdapterNetbiosOptions -{ - [CmdletBinding()] - [CmdletBinding()] - param - ( - [Parameter(Mandatory=$true)] - [System.Object] - # Network Adapter Win32_NetworkAdapterConfiguration Object - $NetworkAdapterObj - , - [Parameter(Mandatory=$true)] - [System.String] - $InterfaceAlias - , - [Parameter(Mandatory=$true)] - [ValidateSet('Default','Enable','Disable')] - [System.String] - $Setting - ) - - Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias,$Setting) - - # Only IPEnabled interfaces can be configured via SetTcpipNetbios method. - if( $NetworkAdapterObj.IPEnabled ) - { - $result = $NetworkAdapterObj | - Invoke-CimMethod ` - -MethodName SetTcpipNetbios ` - -ErrorAction Stop ` - -Arguments @{ - TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__ - } - - if ( $result.ReturnValue -ne 0 ) - { - New-InvalidOperationException ` - -Message ($script:localizedData.FailedUpdatingNetBiosError -f $InterfaceAlias,$result.ReturnValue,$Setting) - } - } - else - { - # IPEnabled=$false can only be configured via registry - # this satisfies disabled and disconnected states - $setItemPropertyParameters = @{ - Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterObj.SettingID)" - Name = 'NetbiosOptions' - Value = [NetBiosSetting]::$Setting.value__ - } - $null = Set-ItemProperty @setItemPropertyParameters - } - -} # end function Set-NetAdapterNetbiosOptions - Export-ModuleMember -Function @( 'Convert-CIDRToSubhetMask' 'Find-NetworkAdapter' @@ -693,6 +579,4 @@ Export-ModuleMember -Function @( 'Set-WinsClientServerStaticAddress' 'Get-IPAddressPrefix' 'Format-Win32NetworkAdapterFilterByNetConnectionId' - 'Get-NetAdapterNetbiosOptionsFromRegistry' - 'Set-NetAdapterNetbiosOptions' ) From 4130e31e86295510391b298ac31c29bc312bc32b Mon Sep 17 00:00:00 2001 From: phbits Date: Wed, 4 Nov 2020 17:34:51 +0000 Subject: [PATCH 03/11] HQRM edits --- .../DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 13241365..8a0fbf40 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -83,7 +83,7 @@ function Get-TargetResource mismatch, a wrong value is returned to signify the resource is not in the desired state. #> - if( $netAdapter -is [Array] ) + if ( $netAdapter -is [Array] ) { [string[]] $SettingResults = @() @@ -96,7 +96,7 @@ function Get-TargetResource [string[]] $WrongSettings = $SettingResults | Where-Object{ $_ -ne $Setting } - if([System.String]::IsNullOrEmpty($WrongSettings) -eq $false) + if ([System.String]::IsNullOrEmpty($WrongSettings) -eq $false) { [string] $Setting = $WrongSettings[0] } @@ -158,14 +158,14 @@ function Set-TargetResource -Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias) } - if( $netAdapter -is [Array] ) + if ( $netAdapter -is [Array] ) { foreach( $netAdapterItem in $netAdapter ) { $CurrentValue = Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting # Only make changes if necessary - if( $CurrentValue -ne $Setting ) + if ( $CurrentValue -ne $Setting ) { Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $netAdapterItem.NetConnectionID, $Setting) @@ -272,7 +272,7 @@ function Get-NetAdapterNetbiosOptionsFromRegistry $RegistryNetbiosOptions = 0 } - switch( $RegistryNetbiosOptions ) + switch ( $RegistryNetbiosOptions ) { 0 { return 'Default' } 1 { return 'Enable' } @@ -327,7 +327,7 @@ function Set-NetAdapterNetbiosOptions Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias,$Setting) # Only IPEnabled interfaces can be configured via SetTcpipNetbios method. - if( $NetworkAdapterObj.IPEnabled ) + if ( $NetworkAdapterObj.IPEnabled ) { $result = $NetworkAdapterObj | Invoke-CimMethod ` @@ -345,8 +345,10 @@ function Set-NetAdapterNetbiosOptions } else { - # IPEnabled=$false can only be configured via registry - # this satisfies disabled and disconnected states + <# + IPEnabled=$false can only be configured via registry + this satisfies disabled and disconnected states + #> $setItemPropertyParameters = @{ Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterObj.SettingID)" Name = 'NetbiosOptions' From 129a9cf95a2b206621225fac4649f06cd18beafd Mon Sep 17 00:00:00 2001 From: phbits Date: Fri, 4 Dec 2020 21:41:56 +0000 Subject: [PATCH 04/11] HQRM edits. --- CHANGELOG.md | 7 +++++++ source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 3 +-- .../DSC_NetBios/en-US/DSC_NetBios.strings.psd1 | 1 - 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc9420fd..fc0a00fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [8.2.1] - 2020-12-4 + +### Changed + +- NetBios + - Fixes [Issue #434](https://github.com/dsccommunity/NetworkingDsc/issues/434). + ## [8.2.0] - 2020-10-16 ### Changed diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 8a0fbf40..58be1790 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -160,7 +160,7 @@ function Set-TargetResource if ( $netAdapter -is [Array] ) { - foreach( $netAdapterItem in $netAdapter ) + foreach ( $netAdapterItem in $netAdapter ) { $CurrentValue = Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting @@ -278,7 +278,6 @@ function Get-NetAdapterNetbiosOptionsFromRegistry 1 { return 'Enable' } 2 { return 'Disable' } default { - # Unknown value. Returning invalid setting to trigger Set-TargetResource [string[]] $InvalidSetting = 'Default','Enable','Disable' | Where-Object{ $_ -ne $Setting } diff --git a/source/DSCResources/DSC_NetBios/en-US/DSC_NetBios.strings.psd1 b/source/DSCResources/DSC_NetBios/en-US/DSC_NetBios.strings.psd1 index cb43c73a..1e386935 100644 --- a/source/DSCResources/DSC_NetBios/en-US/DSC_NetBios.strings.psd1 +++ b/source/DSCResources/DSC_NetBios/en-US/DSC_NetBios.strings.psd1 @@ -4,7 +4,6 @@ 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 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 for Interface '{0}' is '{1}'. From 26a8f71839d4c7e725b073f63ab906320697d181 Mon Sep 17 00:00:00 2001 From: phbits Date: Tue, 8 Dec 2020 22:17:54 +0000 Subject: [PATCH 05/11] NetBios: updated unit tests --- tests/Unit/DSC_NetBios.Tests.ps1 | 398 ++++++++++++++++++++++++++----- 1 file changed, 335 insertions(+), 63 deletions(-) diff --git a/tests/Unit/DSC_NetBios.Tests.ps1 b/tests/Unit/DSC_NetBios.Tests.ps1 index 5c268c67..0656c341 100644 --- a/tests/Unit/DSC_NetBios.Tests.ps1 +++ b/tests/Unit/DSC_NetBios.Tests.ps1 @@ -42,6 +42,21 @@ try -MemberType NoteProperty ` -Name Name ` -Value $script:interfaceAliasA ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name NetConnectionID ` + -Value $script:interfaceAliasA ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name 'GUID' ` + -Value '{00000000-0000-0000-0000-000000000001}' ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name InterfaceIndex ` + -Value 1 ` -PassThru $script:networkAdapterBCimInstance = New-Object ` @@ -51,12 +66,31 @@ try -MemberType NoteProperty ` -Name Name ` -Value $script:interfaceAliasB ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name NetConnectionID ` + -Value $script:interfaceAliasB ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name 'GUID' ` + -Value '{00000000-0000-0000-0000-000000000002}' ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name InterfaceIndex ` + -Value 2 ` -PassThru $script:mockNetadapterA = { $script:networkAdapterACimInstance } + $script:mockNetadapterB = { + $script:networkAdapterBCimInstance + } + $script:mockNetadapterMulti = { @( $script:networkAdapterACimInstance, @@ -64,36 +98,25 @@ try ) } - $script:mockNetadapterSettingsDefault = { + $script:mockWin32NetworkAdapterConfiguration = { New-Object ` -TypeName CimInstance ` -ArgumentList 'Win32_NetworkAdapterConfiguration' | Add-Member ` -MemberType NoteProperty ` - -Name TcpipNetbiosOptions ` - -Value 0 ` + -Name IPEnabled ` + -Value $false ` -PassThru } - $script:mockNetadapterSettingsEnable = { - New-Object ` - -TypeName CimInstance ` - -ArgumentList 'Win32_NetworkAdapterConfiguration' | - Add-Member ` - -MemberType NoteProperty ` - -Name TcpipNetbiosOptions ` - -Value 1 ` - -PassThru - } - - $script:mockNetadapterSettingsDisable = { - New-Object ` + $script:mockWin32NetworkAdapterConfigurationIpEnabled = { + New-Object ` -TypeName CimInstance ` -ArgumentList 'Win32_NetworkAdapterConfiguration' | Add-Member ` -MemberType NoteProperty ` - -Name TcpipNetbiosOptions ` - -Value 2 ` + -Name IPEnabled ` + -Value $true ` -PassThru } @@ -129,26 +152,31 @@ try $InputObject.Name -eq $script:interfaceAliasB } + $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter = { + $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_{00000000-0000-0000-0000-000000000001}' -and ` + $Name -eq 'NetbiosOptions' + } + + $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter = { + $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_{00000000-0000-0000-0000-000000000002}' -and ` + $Name -eq 'NetbiosOptions' + } + $script:testCases = @( @{ - Setting = 'Default' + Setting = 'Default' + SettingInt = 0 NotSetting = 'Enable' - SetItemPropertyCalled = 1 - InvokeCimMethodCalled = 0 }, @{ - Setting = 'Enable' + Setting = 'Enable' + SettingInt = 1 NotSetting = 'Disable' - SetProcess = 'Invoke-CimMethod' - SetItemPropertyCalled = 0 - InvokeCimMethodCalled = 1 }, @{ - Setting = 'Disable' + Setting = 'Disable' + SettingInt = 2 NotSetting = 'Default' - SetProcess = 'Invoke-CimMethod' - SetItemPropertyCalled = 0 - InvokeCimMethodCalled = 1 } ) @@ -158,8 +186,9 @@ try { Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA - Mock -CommandName Get-CimAssociatedInstance -MockWith (Get-Variable -Name "mockNetadapterSettings$($testCase.Setting)" -Scope Script).Value - + Mock -CommandName Get-ItemPropertyValue -MockWith { return $testCase.SettingInt } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + It 'Should not throw exception' { { $script:result = Get-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose @@ -176,15 +205,17 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 } } } Context 'When specifying a wildcard network adapter' { - Context "When both NetBios over TCP/IP is set to 'Default' on both and Settting is 'Default'" { + Context "When both NetBios over TCP/IP is set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockNetadapterSettingsDefault + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { @@ -202,14 +233,17 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } - Context "When both NetBios over TCP/IP is set to 'Enable' on both and Settting is 'Default'" { + Context "When both NetBios over TCP/IP is set to 'Enable' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockNetadapterSettingsEnable + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { @@ -227,15 +261,17 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter -Exactly -Times 0 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } - Context "When NetBios over TCP/IP is set to 'Enable' on the first, 'Disable' on the second and Settting is 'Default'" { + Context "When NetBios over TCP/IP is set to 'Enable' on the first, 'Disable' on the second and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -MockWith $script:mockNetadapterSettingsEnable - Mock -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter -MockWith $script:mockNetadapterSettingsDisable + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { @@ -253,15 +289,17 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter -Exactly -Times 0 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } - Context "When NetBios over TCP/IP is set to 'Default' on the first, 'Disable' on the second and Settting is 'Default'" { + Context "When NetBios over TCP/IP is set to 'Default' on the first, 'Disable' on the second and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -MockWith $script:mockNetadapterSettingsEnable - Mock -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter -MockWith $script:mockNetadapterSettingsDisable + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { @@ -274,20 +312,21 @@ try } It "Setting should return 'Enable'" { - $script:result.Setting | Should -Be 'Enable' + $script:result.Setting | Should -Be 'Disable' } It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter -Exactly -Times 0 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } } Context 'When interface does not exist' { Mock -CommandName Get-CimInstance - Mock -CommandName Get-CimAssociatedInstance $errorRecord = Get-InvalidOperationRecord ` -Message ($script:localizedData.InterfaceNotFoundError -f $script:interfaceAliasA) @@ -300,7 +339,6 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 0 } } } @@ -309,10 +347,10 @@ try Describe 'DSC_NetBios\Test-TargetResource' -Tag 'Test' { Context 'When specifying a single network adapter' { foreach ($testCase in $script:testCases) - { - Context 'When NetBios over TCP/IP is set to "Default"' { + { + Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA - Mock -CommandName Get-CimAssociatedInstance -MockWith (Get-Variable -Name "mockNetadapterSettings$($testCase.Setting)" -Scope Script).Value + Mock -CommandName Get-ItemPropertyValue -MockWith { return $testCase.SettingInt } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter It "Should return true when value '$($testCase.Setting)' is set" { Test-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose | Should -BeTrue @@ -324,14 +362,70 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 2 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 2 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 2 + } + } + } + + Context 'When specifying a wildcard network adapter' { + Context "When NetBios set to 'Default' on both and Setting is 'Default'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + + It 'Should return true' { + Test-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose | Should -BeTrue + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + } + } + + Context "When NetBios set to 'Default' on both and Setting is 'Enable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + + It 'Should return false' { + Test-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose | Should -BeFalse + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + } + } + + Context "When NetBios set to 'Default' on first and 'Enable' on second and Setting is 'Enable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + + It 'Should return false' { + Test-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose | Should -BeFalse + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } } Context 'When interface does not exist' { Mock -CommandName Get-CimInstance - Mock -CommandName Get-CimAssociatedInstance $errorRecord = Get-InvalidOperationRecord ` -Message ($script:localizedData.InterfaceNotFoundError -f $script:interfaceAliasA) @@ -344,7 +438,6 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 0 } } } @@ -354,9 +447,32 @@ try Context 'When specifying a single network adapter' { foreach ($testCase in $script:testCases) { - Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)'" { + Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)' and IPEnabled=True" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled + Mock -CommandName Set-ItemProperty + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 0 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 + } + } + } + + foreach ($testCase in $script:testCases) + { + Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)' and IPEnabled=False" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA - Mock -CommandName Get-CimAssociatedInstance -MockWith (Get-Variable -Name "mockNetadapterSettings$($testCase.Setting)" -Scope Script).Value + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration Mock -CommandName Set-ItemProperty Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 @@ -369,8 +485,164 @@ try It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times $testCase.SetItemPropertyCalled - Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times $testCase.InvokeCimMethodCalled + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 + } + } + } + + Context 'When specifying a wildcard network adapter' { + Context "When all Interfaces are IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 2 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 0 + } + } + + Context "When all Interfaces are NOT IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 2 + } + } + + Context "When first Interface is IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Set-ItemProperty + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + } + } + + Context "When second Interface is IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + } + } + + Context "When first Interface is IPEnabled and NetBios set to 'Default' second Interface Netbios set to 'Disable' and Setting is 'Disable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 0 + } + } + + Context "When first Interface is IPEnabled and NetBios set to 'Disable' second Interface Netbios set to 'Default' and Setting is 'Disable'" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty + + It 'Should not throw exception' { + { + Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 + Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 } } } From a02424a5976d7293a557fc2111f054e5e9eed368 Mon Sep 17 00:00:00 2001 From: phbits Date: Wed, 9 Dec 2020 16:40:04 +0000 Subject: [PATCH 06/11] Reviewable HQRM edits --- CHANGELOG.md | 6 +- .../DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 101 +++++++++++------- 2 files changed, 63 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0a00fa..787ed01d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,12 +5,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [8.2.1] - 2020-12-4 +## [8.2.1] - 2020-12-9 -### Changed +### Fixed - NetBios - - Fixes [Issue #434](https://github.com/dsccommunity/NetworkingDsc/issues/434). + - Fixes configuring network adapters in a disconnected or disabled state. Fixes [Issue #434](https://github.com/dsccommunity/NetworkingDsc/issues/434). ## [8.2.0] - 2020-10-16 diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 58be1790..0f31c542 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -83,22 +83,24 @@ function Get-TargetResource mismatch, a wrong value is returned to signify the resource is not in the desired state. #> - if ( $netAdapter -is [Array] ) + if ($netAdapter -is [Array]) { - [string[]] $SettingResults = @() + [System.String[]] $settingResults = @() - foreach ( $netAdapterItem in $netAdapter ) + foreach ($netAdapterItem in $netAdapter) { - $SettingResults += Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting + $settingResults += Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting - Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $netAdapterItem.NetConnectionID,$SettingResults[-1]) + Write-Verbose -Message ($script:localizedData.CurrentNetBiosSettingMessage -f $netAdapterItem.NetConnectionID, $settingResults[-1]) } - [string[]] $WrongSettings = $SettingResults | Where-Object{ $_ -ne $Setting } + [System.String[]] $wrongSettings = $settingResults | Where-Object -FilterScript { + $_ -ne $Setting + } - if ([System.String]::IsNullOrEmpty($WrongSettings) -eq $false) + if (-not [System.String]::IsNullOrEmpty($wrongSettings)) { - [string] $Setting = $WrongSettings[0] + $Setting = $wrongSettings[0] } } else @@ -158,14 +160,14 @@ function Set-TargetResource -Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias) } - if ( $netAdapter -is [Array] ) + if ($netAdapter -is [Array]) { - foreach ( $netAdapterItem in $netAdapter ) + foreach ($netAdapterItem in $netAdapter) { - $CurrentValue = Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting + $currentValue = Get-NetAdapterNetbiosOptionsFromRegistry -NetworkAdapterGUID $netAdapterItem.GUID -Setting $Setting # Only make changes if necessary - if ( $CurrentValue -ne $Setting ) + if ($currentValue -ne $Setting) { Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $netAdapterItem.NetConnectionID, $Setting) @@ -230,12 +232,15 @@ function Test-TargetResource <# .SYNOPSIS Returns the NetbiosOptions value for a network adapter. + .DESCRIPTION Most reliable method of getting this value since network adapters can be in any number of states (e.g. disabled, disconnected) which can cause Win32 classes to not report the value. + .PARAMETER NetworkAdapterGUID Network Adapter GUID + .PARAMETER Setting Setting value for this resource which should be one of the following: Default, Enable, Disable @@ -246,42 +251,53 @@ function Get-NetAdapterNetbiosOptionsFromRegistry [CmdletBinding()] param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidatePattern("^\{[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}\}$")] [System.String] - # Network Adapter GUID - $NetworkAdapterGUID - , - [Parameter(Mandatory=$true)] + $NetworkAdapterGUID, + + [Parameter(Mandatory = $true)] [ValidateSet('Default','Enable','Disable')] [System.String] $Setting ) # Changing ErrorActionPreference variable since the switch -ErrorAction isn't supported. - $CurrentErrorActionPreference = $ErrorActionPreference - $ErrorActionPreference = 'SilentlyContinue' + $currentErrorActionPreference = $ErrorActionPreference + $ErrorActionPreference = 'SilentlyContinue' - $RegistryNetbiosOptions = Get-ItemPropertyValue -Name 'NetbiosOptions' ` + $registryNetbiosOptions = Get-ItemPropertyValue -Name 'NetbiosOptions' ` -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterGUID)" - $ErrorActionPreference = $CurrentErrorActionPreference + $ErrorActionPreference = $currentErrorActionPreference - if ( $null -eq $RegistryNetbiosOptions ) + if ($null -eq $registryNetbiosOptions) { - $RegistryNetbiosOptions = 0 + $registryNetbiosOptions = 0 } - switch ( $RegistryNetbiosOptions ) + switch ($registryNetbiosOptions) { - 0 { return 'Default' } - 1 { return 'Enable' } - 2 { return 'Disable' } - default { + 0 + { + return 'Default' + } + 1 + { + return 'Enable' + } + 2 + { + return 'Disable' + } + default + { # Unknown value. Returning invalid setting to trigger Set-TargetResource - [string[]] $InvalidSetting = 'Default','Enable','Disable' | Where-Object{ $_ -ne $Setting } + [string[]] $invalidSetting = 'Default','Enable','Disable' | Where-Object -FilterScript { + $_ -ne $Setting + } - return $InvalidSetting[0] + return $invalidSetting[0] } } } # end function Get-NetAdapterNetbiosOptionsFromRegistry @@ -289,16 +305,20 @@ function Get-NetAdapterNetbiosOptionsFromRegistry <# .SYNOPSIS Configures Netbios on a Network Adapter. + .DESCRIPTION Uses two methods for configuring Netbios on a Network Adapter. If an interface is IPEnabled, the CIMMethod will be invoked. Otherwise the registry key is configured as this will satisfy network adapters being in alternative states such as disabled or disconnected. + .PARAMETER NetworkAdapterObj Network Adapter Win32_NetworkAdapterConfiguration Object + .PARAMETER InterfaceAlias Name of the network adapter being configured. Example: Ethernet + .PARAMETER Setting Setting value for this resource which should be one of the following: Default, Enable, Disable @@ -308,16 +328,15 @@ function Set-NetAdapterNetbiosOptions [CmdletBinding()] param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [System.Object] - # Network Adapter Win32_NetworkAdapterConfiguration Object - $NetworkAdapterObj - , - [Parameter(Mandatory=$true)] + $NetworkAdapterObj, + + [Parameter(Mandatory = $true)] [System.String] - $InterfaceAlias - , - [Parameter(Mandatory=$true)] + $InterfaceAlias, + + [Parameter(Mandatory = $true)] [ValidateSet('Default','Enable','Disable')] [System.String] $Setting @@ -326,7 +345,7 @@ function Set-NetAdapterNetbiosOptions Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias,$Setting) # Only IPEnabled interfaces can be configured via SetTcpipNetbios method. - if ( $NetworkAdapterObj.IPEnabled ) + if ($NetworkAdapterObj.IPEnabled) { $result = $NetworkAdapterObj | Invoke-CimMethod ` @@ -336,10 +355,10 @@ function Set-NetAdapterNetbiosOptions TcpipNetbiosOptions = [uint32][NetBiosSetting]::$Setting.value__ } - if ( $result.ReturnValue -ne 0 ) + if ($result.ReturnValue -ne 0) { New-InvalidOperationException ` - -Message ($script:localizedData.FailedUpdatingNetBiosError -f $InterfaceAlias,$result.ReturnValue,$Setting) + -Message ($script:localizedData.FailedUpdatingNetBiosError -f $InterfaceAlias, $result.ReturnValue, $Setting) } } else From ec48dafb4c5fd4816e0c80aca37fd99aeb61462a Mon Sep 17 00:00:00 2001 From: phbits Date: Wed, 9 Dec 2020 17:28:58 +0000 Subject: [PATCH 07/11] change to system.array --- source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 0f31c542..9909a8a7 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -83,7 +83,7 @@ function Get-TargetResource mismatch, a wrong value is returned to signify the resource is not in the desired state. #> - if ($netAdapter -is [Array]) + if ($netAdapter -is [System.Array]) { [System.String[]] $settingResults = @() @@ -160,7 +160,7 @@ function Set-TargetResource -Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias) } - if ($netAdapter -is [Array]) + if ($netAdapter -is [System.Array]) { foreach ($netAdapterItem in $netAdapter) { From 6146d556cffa3f8bc8c84005d38598d736a05d73 Mon Sep 17 00:00:00 2001 From: phbits Date: Tue, 15 Dec 2020 01:33:09 +0000 Subject: [PATCH 08/11] Reviewable edits --- CHANGELOG.md | 2 +- .../DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 81 +-- tests/Unit/DSC_NetBios.Tests.ps1 | 641 +++++++++++++----- 3 files changed, 528 insertions(+), 196 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 787ed01d..62b4a5d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - NetBios - - Fixes configuring network adapters in a disconnected or disabled state. Fixes [Issue #434](https://github.com/dsccommunity/NetworkingDsc/issues/434). + - Fixes configuring network adapters in a disconnected or disabled state - fixes [Issue #434](https://github.com/dsccommunity/NetworkingDsc/issues/434). ## [8.2.0] - 2020-10-16 diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 9909a8a7..5516aa0d 100644 --- a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 +++ b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 @@ -10,6 +10,9 @@ Import-Module -Name (Join-Path -Path $modulePath -ChildPath 'DscResource.Common' # Import Localization Strings $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' +# Base registry key path for NetBios settings +$script:hklmInterfacesPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces' + #region check NetBiosSetting enum loaded, if not load try { @@ -175,7 +178,7 @@ function Set-TargetResource -ResultClassName Win32_NetworkAdapterConfiguration ` -ErrorAction Stop - Set-NetAdapterNetbiosOptions -NetworkAdapterObj $netAdapterConfig ` + Set-NetAdapterNetbiosOptions -NetworkAdapterObject $netAdapterConfig ` -InterfaceAlias $netAdapterItem.NetConnectionID ` -Setting $Setting } @@ -189,7 +192,7 @@ function Set-TargetResource -ResultClassName Win32_NetworkAdapterConfiguration ` -ErrorAction Stop - Set-NetAdapterNetbiosOptions -NetworkAdapterObj $netAdapterConfig ` + Set-NetAdapterNetbiosOptions -NetworkAdapterObject $netAdapterConfig ` -InterfaceAlias $netAdapter.NetConnectionID ` -Setting $Setting } @@ -230,20 +233,20 @@ function Test-TargetResource } <# -.SYNOPSIS - Returns the NetbiosOptions value for a network adapter. + .SYNOPSIS + Returns the NetbiosOptions value for a network adapter. -.DESCRIPTION - Most reliable method of getting this value since network adapters - can be in any number of states (e.g. disabled, disconnected) - which can cause Win32 classes to not report the value. + .DESCRIPTION + Most reliable method of getting this value since network adapters + can be in any number of states (e.g. disabled, disconnected) + which can cause Win32 classes to not report the value. -.PARAMETER NetworkAdapterGUID - Network Adapter GUID + .PARAMETER NetworkAdapterGUID + Network Adapter GUID -.PARAMETER Setting - Setting value for this resource which should be one of - the following: Default, Enable, Disable + .PARAMETER Setting + Setting value for this resource which should be one of + the following: Default, Enable, Disable #> function Get-NetAdapterNetbiosOptionsFromRegistry { @@ -267,7 +270,7 @@ function Get-NetAdapterNetbiosOptionsFromRegistry $ErrorActionPreference = 'SilentlyContinue' $registryNetbiosOptions = Get-ItemPropertyValue -Name 'NetbiosOptions' ` - -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterGUID)" + -Path "$($script:hklmInterfacesPath)\Tcpip_$($NetworkAdapterGUID)" $ErrorActionPreference = $currentErrorActionPreference @@ -282,18 +285,21 @@ function Get-NetAdapterNetbiosOptionsFromRegistry { return 'Default' } + 1 { return 'Enable' } + 2 { return 'Disable' } + default { # Unknown value. Returning invalid setting to trigger Set-TargetResource - [string[]] $invalidSetting = 'Default','Enable','Disable' | Where-Object -FilterScript { + [System.String[]] $invalidSetting = 'Default','Enable','Disable' | Where-Object -FilterScript { $_ -ne $Setting } @@ -303,25 +309,25 @@ function Get-NetAdapterNetbiosOptionsFromRegistry } # end function Get-NetAdapterNetbiosOptionsFromRegistry <# -.SYNOPSIS - Configures Netbios on a Network Adapter. + .SYNOPSIS + Configures Netbios on a Network Adapter. -.DESCRIPTION - Uses two methods for configuring Netbios on a Network Adapter. - If an interface is IPEnabled, the CIMMethod will be invoked. - Otherwise the registry key is configured as this will satisfy - network adapters being in alternative states such as disabled - or disconnected. + .DESCRIPTION + Uses two methods for configuring Netbios on a Network Adapter. + If an interface is IPEnabled, the CIMMethod will be invoked. + Otherwise the registry key is configured as this will satisfy + network adapters being in alternative states such as disabled + or disconnected. -.PARAMETER NetworkAdapterObj - Network Adapter Win32_NetworkAdapterConfiguration Object + .PARAMETER NetworkAdapterObject + Network Adapter Win32_NetworkAdapterConfiguration Object -.PARAMETER InterfaceAlias - Name of the network adapter being configured. Example: Ethernet + .PARAMETER InterfaceAlias + Name of the network adapter being configured. Example: Ethernet -.PARAMETER Setting - Setting value for this resource which should be one of - the following: Default, Enable, Disable + .PARAMETER Setting + Setting value for this resource which should be one of + the following: Default, Enable, Disable #> function Set-NetAdapterNetbiosOptions { @@ -330,7 +336,7 @@ function Set-NetAdapterNetbiosOptions ( [Parameter(Mandatory = $true)] [System.Object] - $NetworkAdapterObj, + $NetworkAdapterObject, [Parameter(Mandatory = $true)] [System.String] @@ -342,12 +348,12 @@ function Set-NetAdapterNetbiosOptions $Setting ) - Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias,$Setting) + Write-Verbose -Message ($script:localizedData.SetNetBiosMessage -f $InterfaceAlias, $Setting) # Only IPEnabled interfaces can be configured via SetTcpipNetbios method. - if ($NetworkAdapterObj.IPEnabled) + if ($NetworkAdapterObject.IPEnabled) { - $result = $NetworkAdapterObj | + $result = $NetworkAdapterObject | Invoke-CimMethod ` -MethodName SetTcpipNetbios ` -ErrorAction Stop ` @@ -364,17 +370,16 @@ function Set-NetAdapterNetbiosOptions else { <# - IPEnabled=$false can only be configured via registry - this satisfies disabled and disconnected states + IPEnabled=$false can only be configured via registry + this satisfies disabled and disconnected states #> $setItemPropertyParameters = @{ - Path = "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_$($NetworkAdapterObj.SettingID)" + Path = "$($script:hklmInterfacesPath)\Tcpip_$($NetworkAdapterObject.SettingID)" Name = 'NetbiosOptions' Value = [NetBiosSetting]::$Setting.value__ } $null = Set-ItemProperty @setItemPropertyParameters } - } # end function Set-NetAdapterNetbiosOptions Export-ModuleMember -Function *-TargetResource diff --git a/tests/Unit/DSC_NetBios.Tests.ps1 b/tests/Unit/DSC_NetBios.Tests.ps1 index 0656c341..34a5b2f3 100644 --- a/tests/Unit/DSC_NetBios.Tests.ps1 +++ b/tests/Unit/DSC_NetBios.Tests.ps1 @@ -106,6 +106,11 @@ try -MemberType NoteProperty ` -Name IPEnabled ` -Value $false ` + -PassThru | + Add-Member ` + -MemberType NoteProperty ` + -Name SettingID ` + -Value '{00000000-0000-0000-0000-000000000001}' ` -PassThru } @@ -152,14 +157,35 @@ try $InputObject.Name -eq $script:interfaceAliasB } + # Base registry key path for NetBios settings + $script:hklmInterfacesPath = 'HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces' + $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter = { - $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_{00000000-0000-0000-0000-000000000001}' -and ` - $Name -eq 'NetbiosOptions' + $Path -eq "$($script:hklmInterfacesPath)\Tcpip_{00000000-0000-0000-0000-000000000001}" -and ` + $Name -eq 'NetbiosOptions' } $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter = { - $Path -eq 'HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_{00000000-0000-0000-0000-000000000002}' -and ` - $Name -eq 'NetbiosOptions' + $Path -eq "$($script:hklmInterfacesPath)\Tcpip_{00000000-0000-0000-0000-000000000002}" -and ` + $Name -eq 'NetbiosOptions' + } + + $script:setItemPropertyValue_NetbiosOptions_Default_ParameterFilter = { + $Path -eq "$($script:hklmInterfacesPath)\Tcpip_{00000000-0000-0000-0000-000000000001}" -and ` + $Name -eq 'NetbiosOptions' -and ` + $Value -eq 0 + } + + $script:setItemPropertyValue_NetbiosOptions_Enable_ParameterFilter = { + $Path -eq "$($script:hklmInterfacesPath)\Tcpip_{00000000-0000-0000-0000-000000000001}" -and ` + $Name -eq 'NetbiosOptions' -and ` + $Value -eq 1 + } + + $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter = { + $Path -eq "$($script:hklmInterfacesPath)\Tcpip_{00000000-0000-0000-0000-000000000001}" -and ` + $Name -eq 'NetbiosOptions' -and ` + $Value -eq 2 } $script:testCases = @( @@ -187,11 +213,12 @@ try Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA Mock -CommandName Get-ItemPropertyValue -MockWith { return $testCase.SettingInt } ` - -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + It 'Should not throw exception' { { - $script:result = Get-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose + $script:result = Get-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting $testCase.Setting -Verbose } | Should -Not -Throw } @@ -204,9 +231,12 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 } } } @@ -214,12 +244,15 @@ try Context 'When specifying a wildcard network adapter' { Context "When both NetBios over TCP/IP is set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -232,22 +265,29 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } Context "When both NetBios over TCP/IP is set to 'Enable' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -260,22 +300,29 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } Context "When NetBios over TCP/IP is set to 'Enable' on the first, 'Disable' on the second and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -288,22 +335,29 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } Context "When NetBios over TCP/IP is set to 'Default' on the first, 'Disable' on the second and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should not throw exception' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -316,11 +370,15 @@ try } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } } @@ -333,12 +391,15 @@ try It 'Should throw expected exception' { { - $script:result = Get-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting 'Default' -Verbose } | Should -Throw $errorRecord } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -Exactly -Times 1 } } } @@ -350,20 +411,26 @@ try { Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA - Mock -CommandName Get-ItemPropertyValue -MockWith { return $testCase.SettingInt } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return $testCase.SettingInt } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter It "Should return true when value '$($testCase.Setting)' is set" { - Test-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose | Should -BeTrue + Test-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting $testCase.Setting -Verbose | Should -BeTrue } It "Should return false when value '$($testCase.NotSetting)' is set" { - Test-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.NotSetting -Verbose | Should -BeFalse + Test-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting $testCase.NotSetting -Verbose | Should -BeFalse } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 2 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 2 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -Exactly -Times 2 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 2 } } } @@ -371,55 +438,74 @@ try Context 'When specifying a wildcard network adapter' { Context "When NetBios set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should return true' { Test-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose | Should -BeTrue } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } Context "When NetBios set to 'Default' on both and Setting is 'Enable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should return false' { Test-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose | Should -BeFalse } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } Context "When NetBios set to 'Default' on first and 'Enable' on second and Setting is 'Enable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 1 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter It 'Should return false' { - Test-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose | Should -BeFalse + Test-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose | Should -BeFalse } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 } } } @@ -432,7 +518,8 @@ try It 'Should throw expected exception' { { - Test-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting 'Enable' -Verbose + Test-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting 'Enable' -Verbose } | Should -Throw $errorRecord } @@ -448,21 +535,34 @@ try foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)' and IPEnabled=True" { + $setItemPropertyParameterFilter = (Get-Variable ` + -Name "setItemPropertyValue_NetbiosOptions_$($testCase.Setting)_ParameterFilter" ` + -Scope Script).Value Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled - Mock -CommandName Set-ItemProperty - Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 - + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $setItemPropertyParameterFilter + Mock -CommandName Invoke-CimMethod ` + -MockWith $script:mockInvokeCimMethodError0 + It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting $testCase.Setting -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 0 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimAssociatedInstance ` + -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $setItemPropertyParameterFilter ` + -Exactly -Times 0 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 } } @@ -471,21 +571,33 @@ try foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)' and IPEnabled=False" { + $setItemPropertyParameterFilter = (Get-Variable ` + -Name "setItemPropertyValue_NetbiosOptions_$($testCase.Setting)_ParameterFilter" ` + -Scope Script).Value Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration - Mock -CommandName Set-ItemProperty + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $setItemPropertyParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting $testCase.Setting -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimAssociatedInstance ` + -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $setItemPropertyParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 } } @@ -494,155 +606,227 @@ try Context 'When specifying a wildcard network adapter' { Context "When all Interfaces are IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 - Mock -CommandName Set-ItemProperty + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias '*' -Setting 'Disable' -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 2 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 0 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 0 } } Context "When all Interfaces are NOT IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 - Mock -CommandName Set-ItemProperty + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias '*' -Setting 'Disable' -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 2 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 2 } } Context "When first Interface is IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter - Mock -CommandName Set-ItemProperty + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled ` + -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration ` + -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias '*' -Setting 'Disable' -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 1 } } Context "When second Interface is IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration ` + -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled ` + -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 - Mock -CommandName Set-ItemProperty + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias '*' -Setting 'Disable' -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 1 } } Context "When first Interface is IPEnabled and NetBios set to 'Default' second Interface Netbios set to 'Disable' and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled ` + -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration ` + -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 - Mock -CommandName Set-ItemProperty + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias '*' -Setting 'Disable' -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 0 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 0 } } Context "When first Interface is IPEnabled and NetBios set to 'Disable' second Interface Netbios set to 'Default' and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter - Mock -CommandName Get-CimAssociatedInstance -MockWith $script:mockWin32NetworkAdapterConfiguration -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter - Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled ` + -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration ` + -ParameterFilter $script:getCimAssociatedInstanceBParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 2 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 - Mock -CommandName Set-ItemProperty + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter It 'Should not throw exception' { { - Set-TargetResource -InterfaceAlias '*' -Setting $testCase.Setting -Verbose + Set-TargetResource -InterfaceAlias '*' -Setting 'Disable' -Verbose } | Should -Not -Throw } It 'Should call expected mocks' { - Assert-MockCalled -CommandName Get-CimInstance -ParameterFilter $script:getCimInstanceMultiParameterFilter -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` - -Exactly -Times 1 - Assert-MockCalled -CommandName Get-ItemPropertyValue -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` - -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceMultiParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter ` + -Exactly -Times 1 Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 - Assert-MockCalled -CommandName Set-ItemProperty -Exactly -Times 1 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 1 } } } @@ -671,6 +855,149 @@ try } } } + Describe 'DSC_NetBios\Get-NetAdapterNetbiosOptionsFromRegistry' { + foreach ($testCase in $script:testCases) + { + Context "When interface NetBios is '$($testCase.Setting)'" { + Mock -CommandName Get-ItemPropertyValue -MockWith { return $testCase.SettingInt } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + + It "Should return true when value '$($testCase.Setting)' is set" { + $Result = Get-NetAdapterNetbiosOptionsFromRegistry ` + -NetworkAdapterGUID $script:networkAdapterACimInstance.GUID ` + -Setting $testCase.Setting + $Result -eq $testCase.Setting | Should -BeTrue + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + } + } + } + Context 'When interface Netbios setting missing from registry' { + Mock -CommandName Get-ItemPropertyValue -MockWith { return $null } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + + It 'Should return true' { + $Result = Get-NetAdapterNetbiosOptionsFromRegistry ` + -NetworkAdapterGUID $script:networkAdapterACimInstance.GUID ` + -Setting 'Enable' + $Result -eq 'Default' | Should -BeTrue + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + } + } + Context 'When Netbios registry setting invalid number' { + Mock -CommandName Get-ItemPropertyValue -MockWith { return 5 } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + + It 'Should evaluate true' { + $Result = Get-NetAdapterNetbiosOptionsFromRegistry ` + -NetworkAdapterGUID $script:networkAdapterACimInstance.GUID ` + -Setting 'Enable' + $Result -eq 'Default' | Should -BeTrue + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + } + } + Context 'When Netbios registry setting invalid letters' { + Mock -CommandName Get-ItemPropertyValue -MockWith { return 'invalid' } ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter + + It 'Should evaluate true' { + $Result = Get-NetAdapterNetbiosOptionsFromRegistry ` + -NetworkAdapterGUID $script:networkAdapterACimInstance.GUID ` + -Setting 'Enable' + $Result -eq 'Default' | Should -BeTrue + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Get-ItemPropertyValue ` + -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter ` + -Exactly -Times 1 + } + } + } + Describe 'DSC_NetBios\Set-NetAdapterNetbiosOptions' { + Context "When NetBios over TCP/IP should be set to 'Default' and IPEnabled=True" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfigurationIpEnabled + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Default_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + + It 'Should not throw exception' { + { + $netAdapter = Get-CimInstance ` + -ClassName Win32_NetworkAdapter ` + -Filter 'NetConnectionID="$($script:interfaceAliasA)"' + + $netAdapterConfig = $netAdapter | Get-CimAssociatedInstance ` + -ResultClassName Win32_NetworkAdapterConfiguration ` + -ErrorAction Stop + + Set-NetAdapterNetbiosOptions ` + -NetworkAdapterObject $netAdapterConfig ` + -InterfaceAlias $script:interfaceAliasA ` + -Setting 'Default' -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Default_ParameterFilter ` + -Exactly -Times 0 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 1 + } + } + Context "When NetBios over TCP/IP should be set to 'Default' and IPEnabled=False" { + Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA + Mock -CommandName Get-CimAssociatedInstance ` + -MockWith $script:mockWin32NetworkAdapterConfiguration + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Default_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + + It 'Should not throw exception' { + { + $netAdapter = Get-CimInstance ` + -ClassName Win32_NetworkAdapter ` + -Filter 'NetConnectionID="$($script:interfaceAliasA)"' + + $netAdapterConfig = $netAdapter | Get-CimAssociatedInstance ` + -ResultClassName Win32_NetworkAdapterConfiguration ` + -ErrorAction Stop + + Set-NetAdapterNetbiosOptions ` + -NetworkAdapterObject $netAdapterConfig ` + -InterfaceAlias $script:interfaceAliasA ` + -Setting 'Default' -Verbose + } | Should -Not -Throw + } + + It 'Should call expected mocks' { + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Default_ParameterFilter ` + -Exactly -Times 1 + Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times 0 + Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 + Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 1 + } + } + } } } finally From 3943f923e45bdffd9c0fe44bd08c00b99b7bce9e Mon Sep 17 00:00:00 2001 From: phbits Date: Sat, 26 Dec 2020 17:45:03 +0000 Subject: [PATCH 09/11] Update CHANGELOG.md --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62b4a5d1..05c4ef19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [8.2.1] - 2020-12-9 - ### Fixed - NetBios From 7f3e0152b2ff604efb73ac3a9b9be7b1893eec47 Mon Sep 17 00:00:00 2001 From: phbits Date: Thu, 31 Dec 2020 21:26:57 +0000 Subject: [PATCH 10/11] Reviewable edits. Added blank line before each Describe and Context block --- tests/Unit/DSC_NetBios.Tests.ps1 | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Unit/DSC_NetBios.Tests.ps1 b/tests/Unit/DSC_NetBios.Tests.ps1 index 34a5b2f3..c8b5cbe6 100644 --- a/tests/Unit/DSC_NetBios.Tests.ps1 +++ b/tests/Unit/DSC_NetBios.Tests.ps1 @@ -207,7 +207,9 @@ try ) Describe 'DSC_NetBios\Get-TargetResource' -Tag 'Get' { + Context 'When specifying a single network adapter' { + foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { @@ -242,6 +244,7 @@ try } Context 'When specifying a wildcard network adapter' { + Context "When both NetBios over TCP/IP is set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` @@ -406,7 +409,9 @@ try } Describe 'DSC_NetBios\Test-TargetResource' -Tag 'Test' { + Context 'When specifying a single network adapter' { + foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { @@ -436,6 +441,7 @@ try } Context 'When specifying a wildcard network adapter' { + Context "When NetBios set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` @@ -531,7 +537,9 @@ try } Describe 'DSC_NetBios\Set-TargetResource' -Tag 'Set' { + Context 'When specifying a single network adapter' { + foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)' and IPEnabled=True" { @@ -604,6 +612,7 @@ try } Context 'When specifying a wildcard network adapter' { + Context "When all Interfaces are IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti Mock -CommandName Get-CimAssociatedInstance ` @@ -855,7 +864,9 @@ try } } } + Describe 'DSC_NetBios\Get-NetAdapterNetbiosOptionsFromRegistry' { + foreach ($testCase in $script:testCases) { Context "When interface NetBios is '$($testCase.Setting)'" { @@ -876,6 +887,7 @@ try } } } + Context 'When interface Netbios setting missing from registry' { Mock -CommandName Get-ItemPropertyValue -MockWith { return $null } ` -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter @@ -893,6 +905,7 @@ try -Exactly -Times 1 } } + Context 'When Netbios registry setting invalid number' { Mock -CommandName Get-ItemPropertyValue -MockWith { return 5 } ` -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter @@ -910,6 +923,7 @@ try -Exactly -Times 1 } } + Context 'When Netbios registry setting invalid letters' { Mock -CommandName Get-ItemPropertyValue -MockWith { return 'invalid' } ` -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter @@ -928,7 +942,9 @@ try } } } + Describe 'DSC_NetBios\Set-NetAdapterNetbiosOptions' { + Context "When NetBios over TCP/IP should be set to 'Default' and IPEnabled=True" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA Mock -CommandName Get-CimAssociatedInstance ` @@ -963,6 +979,7 @@ try Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 1 } } + Context "When NetBios over TCP/IP should be set to 'Default' and IPEnabled=False" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA Mock -CommandName Get-CimAssociatedInstance ` From c4a429cdfa2260d067d03aa8b47087cc5db6e5db Mon Sep 17 00:00:00 2001 From: phbits Date: Thu, 31 Dec 2020 22:24:43 +0000 Subject: [PATCH 11/11] removed spaces --- tests/Unit/DSC_NetBios.Tests.ps1 | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/Unit/DSC_NetBios.Tests.ps1 b/tests/Unit/DSC_NetBios.Tests.ps1 index c8b5cbe6..0b120dca 100644 --- a/tests/Unit/DSC_NetBios.Tests.ps1 +++ b/tests/Unit/DSC_NetBios.Tests.ps1 @@ -207,9 +207,9 @@ try ) Describe 'DSC_NetBios\Get-TargetResource' -Tag 'Get' { - + Context 'When specifying a single network adapter' { - + foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { @@ -244,7 +244,7 @@ try } Context 'When specifying a wildcard network adapter' { - + Context "When both NetBios over TCP/IP is set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` @@ -409,9 +409,9 @@ try } Describe 'DSC_NetBios\Test-TargetResource' -Tag 'Test' { - + Context 'When specifying a single network adapter' { - + foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP is set to '$($testCase.Setting)'" { @@ -441,7 +441,7 @@ try } Context 'When specifying a wildcard network adapter' { - + Context "When NetBios set to 'Default' on both and Setting is 'Default'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti Mock -CommandName Get-ItemPropertyValue -MockWith { return 0 } ` @@ -537,9 +537,9 @@ try } Describe 'DSC_NetBios\Set-TargetResource' -Tag 'Set' { - + Context 'When specifying a single network adapter' { - + foreach ($testCase in $script:testCases) { Context "When NetBios over TCP/IP should be set to '$($testCase.Setting)' and IPEnabled=True" { @@ -612,7 +612,7 @@ try } Context 'When specifying a wildcard network adapter' { - + Context "When all Interfaces are IPEnabled and NetBios set to 'Default' on both and Setting is 'Disable'" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterMulti Mock -CommandName Get-CimAssociatedInstance ` @@ -864,9 +864,9 @@ try } } } - + Describe 'DSC_NetBios\Get-NetAdapterNetbiosOptionsFromRegistry' { - + foreach ($testCase in $script:testCases) { Context "When interface NetBios is '$($testCase.Setting)'" { @@ -887,7 +887,7 @@ try } } } - + Context 'When interface Netbios setting missing from registry' { Mock -CommandName Get-ItemPropertyValue -MockWith { return $null } ` -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter @@ -905,7 +905,7 @@ try -Exactly -Times 1 } } - + Context 'When Netbios registry setting invalid number' { Mock -CommandName Get-ItemPropertyValue -MockWith { return 5 } ` -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter @@ -923,7 +923,7 @@ try -Exactly -Times 1 } } - + Context 'When Netbios registry setting invalid letters' { Mock -CommandName Get-ItemPropertyValue -MockWith { return 'invalid' } ` -ParameterFilter $script:getItemPropertyValue_NetbiosOptions_One_ParameterFilter @@ -942,9 +942,9 @@ try } } } - + Describe 'DSC_NetBios\Set-NetAdapterNetbiosOptions' { - + Context "When NetBios over TCP/IP should be set to 'Default' and IPEnabled=True" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA Mock -CommandName Get-CimAssociatedInstance ` @@ -979,7 +979,7 @@ try Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 1 } } - + Context "When NetBios over TCP/IP should be set to 'Default' and IPEnabled=False" { Mock -CommandName Get-CimInstance -MockWith $script:mockNetadapterA Mock -CommandName Get-CimAssociatedInstance `