From cf7c758064aaa4914d9aa37469427b9b6b874f7c Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Tue, 8 Sep 2020 07:54:49 -0500 Subject: [PATCH 1/2] BugFix for Adding DP to Groups --- CHANGELOG.md | 4 ++ .../DSC_CMDistributionPointGroupMembers.psm1 | 7 ++- ...DistributionPointGroupMembers.strings.psd1 | 1 + .../ConfigMgrCBDsc.ResourceHelper.psd1 | 1 + .../ConfigMgrCBDsc.ResourceHelper.psm1 | 55 +++++++++++++++++++ ...ConfigMgrCBDsc.ResourceHelper.strings.psd1 | 2 + .../CMDistributionPointGroupMembers.tests.ps1 | 37 ++++++++++--- .../ConfigMgrCBDsc.ResourceHelper.tests.ps1 | 29 ++++++++++ 8 files changed, 126 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 468e39d..018933a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added CMSecurityScopes Resource - Added CMUserDiscovery Resource - Added CMSecurityRoles Resource +- Added Add-DPToDPGroup to the ResourceHelper ### Changed @@ -80,3 +81,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed Historic Changelog ### Fixed + +- Fixed issue when adding a Distribution Point to Distribution Group immediately + after adding the Distribution Point would error saying invalid Distribution Point. diff --git a/source/DSCResources/DSC_CMDistributionPointGroupMembers/DSC_CMDistributionPointGroupMembers.psm1 b/source/DSCResources/DSC_CMDistributionPointGroupMembers/DSC_CMDistributionPointGroupMembers.psm1 index 0bd3bf8..992f79f 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroupMembers/DSC_CMDistributionPointGroupMembers.psm1 +++ b/source/DSCResources/DSC_CMDistributionPointGroupMembers/DSC_CMDistributionPointGroupMembers.psm1 @@ -162,7 +162,12 @@ function Set-TargetResource } Write-Verbose -Message ($script:localizedData.AddDistroGroup -f $add, $DistributionPoint) - Add-CMDistributionPointToGroup @addParams + $dpGroupAdd = Add-DPToDPGroup @addParams + + if ($dpGroupAdd -eq $false) + { + $errorMsg += ($script:localizedData.GroupAddError -f $DistributionPoint, $add) + } } else { diff --git a/source/DSCResources/DSC_CMDistributionPointGroupMembers/en-US/DSC_CMDistributionPointGroupMembers.strings.psd1 b/source/DSCResources/DSC_CMDistributionPointGroupMembers/en-US/DSC_CMDistributionPointGroupMembers.strings.psd1 index eddb119..52515e6 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroupMembers/en-US/DSC_CMDistributionPointGroupMembers.strings.psd1 +++ b/source/DSCResources/DSC_CMDistributionPointGroupMembers/en-US/DSC_CMDistributionPointGroupMembers.strings.psd1 @@ -9,4 +9,5 @@ ConvertFrom-StringData @' ParamIgnore = DistributionGroups was specifed, ignoring DistributionGroupsToInclude and DistributionGroupsToExclude. ErrorGroup = Distribution Groups: {0} does not exist. ErrorBoth = Distribution Group: {0} is a member of the include group and exclude group. + GroupAddError = Unable to add the Distribution Point: {0} to Group: {1}. '@ diff --git a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 index 8f0e798..80c1c71 100644 --- a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 +++ b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 @@ -42,6 +42,7 @@ 'ConvertTo-ScheduleInterval' 'ConvertTo-AnyCimInstance' 'Compare-MultipleCompares' + 'Add-DPToDPGroup' ) # Cmdlets to export from this module diff --git a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psm1 b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psm1 index ca0b8ac..f047e2f 100644 --- a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psm1 +++ b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psm1 @@ -7,6 +7,11 @@ data LocalizedData '@ } +$script:dscResourceCommonPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common' +Import-Module -Name $script:dscResourceCommonPath + +$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' + <# .SYNOPSIS Import Configuration Manager module commands. @@ -520,6 +525,55 @@ function Compare-MultipleCompares } } +<# + .SYNOPSIS + Adds the Distribution Point to the Distribution Point Group. + + .PARAMETER DistributionPointName + Specifies the Distribution Point to modify Distribution Point Group membership. + + .PARAMETER DistributionPointGroupName + Specifies a Distribution Group to add to the Distribution Point. +#> +function Add-DPToDPGroup +{ + [OutputType([System.Boolean])] + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $DistributionPointName, + + [Parameter(Mandatory = $true)] + [String] + $DistributionPointGroupName + ) + + $count = 0 + $success = $false + + do + { + try + { + Write-Verbose -Message ($script:localizedData.AddDP -f $DistributionPointName, $DistributionPointGroupName) -Verbose + Add-CMDistributionPointToGroup -DistributionPointName $DistributionPointName -DistributionPointGroupName $DistributionPointGroupName + $success = $true + $count = 12 + } + catch + { + Write-Warning -Message ($script:localizedData.Wait -f $DistributionPointName) -Verbose + Start-Sleep -Seconds 10 + $count ++ + } + } + until ($count -eq 12) + + return $success +} + Export-ModuleMember -Function @( 'Import-ConfigMgrPowerShellModule' 'Convert-CidrToIP' @@ -530,4 +584,5 @@ Export-ModuleMember -Function @( 'ConvertTo-ScheduleInterval' 'ConvertTo-AnyCimInstance' 'Compare-MultipleCompares' + 'Add-DPToDPGroup' ) diff --git a/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 b/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 index 3b35d66..0ac0a66 100644 --- a/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 +++ b/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 @@ -1,2 +1,4 @@ ConvertFrom-StringData @' + AddDP = Distribution Point Name {0} Distribution Point Group Name: {1}. + Wait = Waiting 10 seconds to for the distribution point {0} to fully provision. '@ diff --git a/tests/Unit/CMDistributionPointGroupMembers.tests.ps1 b/tests/Unit/CMDistributionPointGroupMembers.tests.ps1 index 7fe2f91..e9b44e9 100644 --- a/tests/Unit/CMDistributionPointGroupMembers.tests.ps1 +++ b/tests/Unit/CMDistributionPointGroupMembers.tests.ps1 @@ -157,7 +157,6 @@ try Mock -CommandName Import-ConfigMgrPowerShellModule Mock -CommandName Set-Location - Mock -CommandName Add-CMDistributionPointToGroup Mock -CommandName Remove-CMDistributionPointFromGroup } @@ -175,28 +174,31 @@ try DistributionGroupsToExclude = 'TestGroup1' } + Mock -CommandName Add-DPToDPGroup -MockWith { $true } Mock -CommandName Get-CMDistributionPointGroup -MockWith { $true } } It 'Should call expected commands when groups match' { Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent } + Set-TargetResource @groupInputMatch Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It } It 'Should call expected commands when groups do not match' { Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent } + Set-TargetResource @groupInput Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 1 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It } @@ -208,7 +210,7 @@ try Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 1 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It } @@ -220,7 +222,7 @@ try Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It } } @@ -234,10 +236,12 @@ try DistributionGroupsToExclude = 'TestGroup1' } - Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null } + $dpGroupAddError = "Unable to add the Distribution Point: DP01.contoso.com to Group: TestGroup2." + Mock -CommandName Add-DPToDPGroup } It 'Should call expected commands when DP is absent and throws' { + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null } Mock -CommandName Get-TargetResource -MockWith { $dpAbsent } { Set-TargetResource @groupInputMatch } | Should -Throw @@ -245,11 +249,12 @@ try Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It } It 'Should call expected commands when Include and Exclude contain same group and throws' { + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null } Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent } { Set-TargetResource @includeExclude } | Should -Throw @@ -257,11 +262,12 @@ try Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 0 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It } It 'Should call expected commands when mismatch and group does not exist' { + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null } Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent } { Set-TargetResource @groupInput } | Should -Throw @@ -269,10 +275,23 @@ try Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It - Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 0 -Scope It Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It } + It 'Should call expected commands when DP errors while adding to group' { + Mock -CommandName Get-TargetResource -MockWith { $dpReturnPresent } + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $true } + Mock -CommandName Add-DPToDPGroup -MockWith { $false } + + { Set-TargetResource @groupInput } | Should -Throw -ExpectedMessage $dpGroupAddError + Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It + Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It + Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It + Assert-MockCalled Get-CMDistributionPointGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Add-DPToDPGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 1 -Scope It + } } } diff --git a/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 b/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 index 14604f8..3cc3747 100644 --- a/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 +++ b/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 @@ -569,4 +569,33 @@ InModuleScope $script:subModuleName { } } } + + Describe "ConfigMgrCBDsc - ConfigMgrCBDsc.ResourceHelper\Add-DPToDPGroup" { + BeforeAll { + $inputParam = @{ + DistributionPointName = 'DP01.contoso.com' + DistributionPointGroupName = 'TestGroup1' + } + + Mock -CommandName Start-Sleep + } + + Context 'When return is as expected' { + It 'Should return desired result for when DP is added to the group' { + Mock -CommandName Add-CMDistributionPointToGroup + + Add-DPToDPGroup @inputParam | Should -Be $true + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Start-Sleep -Exactly -Times 0 -Scope It + } + + It 'Should return desired result for when DP is added to the group' { + Mock -CommandName Add-CMDistributionPointToGroup -MockWith { throw } + + Add-DPToDPGroup @inputParam | Should -Be $false + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 12 -Scope It + Assert-MockCalled Start-Sleep -Exactly -Times 12 -Scope It + } + } + } } From f4a7ca12f641a77a0e4599c706beb491c1d9c6e5 Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Tue, 8 Sep 2020 14:48:02 -0500 Subject: [PATCH 2/2] Update Per PR feedback --- .../en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 b/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 index 0ac0a66..f71302c 100644 --- a/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 +++ b/source/Modules/ConfigMgrCBDsc.ResourceHelper/en-US/ConfigMgrCBDsc.ResourceHelper.strings.psd1 @@ -1,4 +1,4 @@ ConvertFrom-StringData @' - AddDP = Distribution Point Name {0} Distribution Point Group Name: {1}. - Wait = Waiting 10 seconds to for the distribution point {0} to fully provision. + AddDP = Distribution Point Name {0} Distribution Point Group Name: {1}. + Wait = Waiting 10 seconds to for the Distribution Point {0} to fully provision. '@