From c82940284abff74f2982e3b506f44f1403fd7fc1 Mon Sep 17 00:00:00 2001 From: phbits Date: Sun, 21 Feb 2021 21:57:25 -0700 Subject: [PATCH] NetBios: fixes #434, added two functions to common. (#479) --- CHANGELOG.md | 5 + .../DSCResources/DSC_NetBios/DSC_NetBios.psm1 | 249 ++++-- .../en-US/DSC_NetBios.strings.psd1 | 1 - tests/Unit/DSC_NetBios.Tests.ps1 | 776 ++++++++++++++++-- 4 files changed, 900 insertions(+), 131 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f338744..975fa309 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- NetBios + - Fixes configuring network adapters in a disconnected or disabled state - fixes [Issue #434](https://github.com/dsccommunity/NetworkingDsc/issues/434). + ### Changed - NetAdapterLso diff --git a/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 b/source/DSCResources/DSC_NetBios/DSC_NetBios.psm1 index 064580b6..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 { @@ -78,37 +81,37 @@ 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 [System.Array]) { - $netAdapterConfig = $netAdapterItem | Get-CimAssociatedInstance ` - -ResultClassName Win32_NetworkAdapterConfiguration ` - -ErrorAction Stop - - $tcpipNetbiosOptions = $netAdapterConfig.TcpipNetbiosOptions + [System.String[]] $settingResults = @() - 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) + [System.String[]] $wrongSettings = $settingResults | Where-Object -FilterScript { + $_ -ne $Setting + } - if ($interfaceSetting -ne $Setting) + if (-not [System.String]::IsNullOrEmpty($wrongSettings)) { - $Setting = $interfaceSetting - break + $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 +163,39 @@ function Set-TargetResource -Message ($script:localizedData.InterfaceNotFoundError -f $InterfaceAlias) } - foreach ($netAdapterItem in $netAdapter) + if ($netAdapter -is [System.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 -NetworkAdapterObject $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 -NetworkAdapterObject $netAdapterConfig ` + -InterfaceAlias $netAdapter.NetConnectionID ` + -Setting $Setting + } } <# @@ -233,4 +232,154 @@ 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] + $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 "$($script:hklmInterfacesPath)\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 + [System.String[]] $invalidSetting = 'Default','Enable','Disable' | Where-Object -FilterScript { + $_ -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 NetworkAdapterObject + 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] + $NetworkAdapterObject, + + [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 ($NetworkAdapterObject.IPEnabled) + { + $result = $NetworkAdapterObject | + 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 = "$($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/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}'. diff --git a/tests/Unit/DSC_NetBios.Tests.ps1 b/tests/Unit/DSC_NetBios.Tests.ps1 index 5c268c67..0b120dca 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,30 @@ try ) } - $script:mockNetadapterSettingsDefault = { + $script:mockWin32NetworkAdapterConfiguration = { New-Object ` -TypeName CimInstance ` -ArgumentList 'Win32_NetworkAdapterConfiguration' | Add-Member ` -MemberType NoteProperty ` - -Name TcpipNetbiosOptions ` - -Value 0 ` - -PassThru - } - - $script:mockNetadapterSettingsEnable = { - New-Object ` - -TypeName CimInstance ` - -ArgumentList 'Win32_NetworkAdapterConfiguration' | + -Name IPEnabled ` + -Value $false ` + -PassThru | Add-Member ` -MemberType NoteProperty ` - -Name TcpipNetbiosOptions ` - -Value 1 ` + -Name SettingID ` + -Value '{00000000-0000-0000-0000-000000000001}' ` -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,40 +157,70 @@ 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 "$($script:hklmInterfacesPath)\Tcpip_{00000000-0000-0000-0000-000000000001}" -and ` + $Name -eq 'NetbiosOptions' + } + + $script:getItemPropertyValue_NetbiosOptions_Two_ParameterFilter = { + $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 = @( @{ - 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 } ) 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)'" { 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 + $script:result = Get-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting $testCase.Setting -Verbose } | Should -Not -Throw } @@ -175,20 +233,29 @@ 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-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -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' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -201,19 +268,29 @@ 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-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 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' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -226,20 +303,29 @@ 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-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 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' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -252,20 +338,29 @@ 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-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 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' { { - $script:result = Get-TargetResource -InterfaceAlias '*' -Setting 'Default' -Verbose + $script:result = Get-TargetResource -InterfaceAlias '*' ` + -Setting 'Default' -Verbose } | Should -Not -Throw } @@ -274,103 +369,473 @@ 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-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) 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-CimAssociatedInstance -Exactly -Times 0 + Assert-MockCalled -CommandName Get-CimInstance ` + -ParameterFilter $script:getCimInstanceParameterFilter ` + -Exactly -Times 1 } } } } 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 + 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 + } + } + } + + 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:getCimInstanceParameterFilter -Exactly -Times 2 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -ParameterFilter $script:getCimAssociatedInstanceAParameterFilter -Exactly -Times 2 + 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) It 'Should throw expected exception' { { - Test-TargetResource -InterfaceAlias $script:interfaceAliasA -Setting 'Enable' -Verbose + Test-TargetResource -InterfaceAlias $script:interfaceAliasA ` + -Setting 'Enable' -Verbose } | Should -Throw $errorRecord } It 'Should call expected mocks' { Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 1 - Assert-MockCalled -CommandName Get-CimAssociatedInstance -Exactly -Times 0 } } } } 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)'" { + 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 (Get-Variable -Name "mockNetadapterSettings$($testCase.Setting)" -Scope Script).Value - Mock -CommandName Set-ItemProperty + 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 + } | 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 ` + -ParameterFilter $setItemPropertyParameterFilter ` + -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" { + $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 ` + -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 $testCase.SetItemPropertyCalled - Assert-MockCalled -CommandName Invoke-CimMethod -Exactly -Times $testCase.InvokeCimMethodCalled + 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 + } + } + } + + 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 ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter + + It 'Should not throw exception' { + { + 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 Invoke-CimMethod -Exactly -Times 2 + 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 Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter + + It 'Should not throw exception' { + { + 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 Invoke-CimMethod -Exactly -Times 0 + 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 ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter + Mock -CommandName Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + + It 'Should not throw exception' { + { + 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 Invoke-CimMethod -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 Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter + + It 'Should not throw exception' { + { + 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 Invoke-CimMethod -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 Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter + + It 'Should not throw exception' { + { + 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 Invoke-CimMethod -Exactly -Times 1 + 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 Invoke-CimMethod -MockWith $script:mockInvokeCimMethodError0 + Mock -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter + + It 'Should not throw exception' { + { + 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 Invoke-CimMethod -Exactly -Times 0 + Assert-MockCalled -CommandName Set-ItemProperty ` + -ParameterFilter $script:setItemPropertyValue_NetbiosOptions_Disable_ParameterFilter ` + -Exactly -Times 1 } } } @@ -399,6 +864,157 @@ 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