From 10ef642cb913cdb5cfb2263ecde521cb8ace115a Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Mon, 29 Jun 2020 09:53:55 -0500 Subject: [PATCH 1/7] Initial Module creation --- CHANGELOG.md | 1 + README.md | 21 + source/ConfigMgrCBDsc.psd1 | 3 +- .../DSC_CMDistributionPointGroup.psm1 | 311 +++++++++++++++ .../DSC_CMDistributionPointGroup.schema.mof | 10 + .../DSC_CMDistributionPointGroup.strings.psd1 | 13 + .../CMDistributionPointGroup_Absent.ps1 | 18 + .../CMDistributionPointGroup_Present.ps1 | 42 ++ tests/Unit/CMDistributionPointGroup.tests.ps1 | 368 ++++++++++++++++++ 9 files changed, 786 insertions(+), 1 deletion(-) create mode 100644 source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 create mode 100644 source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof create mode 100644 source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 create mode 100644 source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 create mode 100644 source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 create mode 100644 tests/Unit/CMDistributionPointGroup.tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ca942b..8374acf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added CMPullDistributionPoint Resource - Added ConvertTo-AnyCimInstance to the ResourceHelper - Added CMSiteMaintenance Resource +- Added CMDistributionPointGroup Resource ### Changed diff --git a/README.md b/README.md index b47617c..2d5afa9 100644 --- a/README.md +++ b/README.md @@ -858,3 +858,24 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu - [CMSiteMaintenance_MaintenanceTask_Enabled](Source\Examples\Resources\CMSiteMaintenance\CMSiteMaintenance_MaintenanceTask_Enabled.ps1) - [CMSiteMaintenance_SummaryTask_Enabled](Source\Examples\Resources\CMSiteMaintenance\CMSiteMaintenance_SummaryTask_Enabled.ps1) - [CMSiteMaintenance_UpdateAppCatTablesTask_Enabled](Source\Examples\Resources\CMSiteMaintenance\CMSiteMaintenance_UpdateAppCatTablesTask_Enabled.ps1) + +### CMDistributionPointGroup + +- **[String] DistributionPointGroup** _(Key)_: Specifies the distribution point + group name. +- **[String] SiteCode** _(Required)_: Specifies the Site Code for the Configuration + Manager site. +- **[String] DistributionPoints[]** _(Write)_: Specifies an array of distribution + points to match to the distribution point group. +- **[String] DistributionPointsToInclude[]** _(Write)_: Specifies an array of + distribution points to add to the distribution point group. +- **[String] DistributionPointsToExclude[]** _(Write)_: Specifies an array of + distribution points to remove from the distribution point group. +- **[String] Ensure** _(Write)_: Specifies whether the distribution point group + is present or absent. + - Values include: { Present | Absent } + +#### CMDistributionPointGroup Examples + +- [CMDistributionPointGroup_Present](Source\Examples\Resources\CMDistributionPointGroup\CMDistributionPointGroup_Present.ps1) +- [CMDistributionPointGroup_Absent](Source\Examples\Resources\CMDistributionPointGroup\CMDistributionPointGroup_Absent.ps1) diff --git a/source/ConfigMgrCBDsc.psd1 b/source/ConfigMgrCBDsc.psd1 index 58fd9bf..956d137 100644 --- a/source/ConfigMgrCBDsc.psd1 +++ b/source/ConfigMgrCBDsc.psd1 @@ -66,6 +66,7 @@ 'CMPxeDistributionPoint' 'CMPullDistributionPoint' 'CMSiteMaintenance' + 'CMDistributionPointGroup' ) <# @@ -82,7 +83,7 @@ 'SccmSqlSetup','SCCMInstall','CMIniFile','Collections','Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups', 'ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint','SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery', 'ServiceConnectionPoint','NetworkDiscovery','ReportingServicePoint','SystemDiscovery','PXEDistributionPoint','PullDistributionPoint', - 'SiteMaintenance') + 'SiteMaintenance','DistributionPointGroup') # A URL to the license for this module. LicenseUri = 'https://github.com/dsccommunity/ConfigMgrCBDsc/blob/master/LICENSE' diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 new file mode 100644 index 0000000..23e2543 --- /dev/null +++ b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 @@ -0,0 +1,311 @@ +$script:dscResourceCommonPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common' +$script:configMgrResourcehelper = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\ConfigMgrCBDsc.ResourceHelper' + +Import-Module -Name $script:dscResourceCommonPath +Import-Module -Name $script:configMgrResourcehelper + +$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' + +<# + .SYNOPSIS + This will return a hashtable of results. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + + .PARAMETER DistributionPointGroup + Specifies the Distribution Point Group name. +#> +function Get-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Collections.Hashtable])] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter(Mandatory = $true)] + [String] + $DistributionPointGroup + ) + + Write-Verbose -Message $script:localizedData.RetrieveSettingValue + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + + $groupStatus = Get-CMDistributionPointGroup -Name $DistributionPointGroup + + if ($groupStatus) + { + $dplist = Get-CMDistributionPoint -DistributionPointGroupName $DistributionPointGroup + $dpMembers = @() + + foreach ($dp in $dplist) + { + $dpMembers += $dp.NetworkOSPath.SubString(2) + } + + $group = 'Present' + } + else + { + $group = 'Absent' + } + + return @{ + SiteCode = $SiteCode + DistributionPointGroup = $DistributionPointGroup + DistributionPoints = $dpMembers + Ensure = $group + } +} + +<# + .SYNOPSIS + This will set the desired state. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + + .PARAMETER DistributionPointGroup + Specifies the Distribution Point Group name. + + .PARAMETER DistributionPoints + Specifies an array of distribution points to match to the distribution point group. + + .PARAMETER DistributionPointsToInclude + Specifies an array of distribution points to add to the distribution point group. + + .PARAMETER DistributionPointsToExclude + Specifies an array of distribution points to remove from the distribution point group. + + .PARAMETER Ensure + Specifies if the Distribution Point Group is to be present or absent. +#> +function Set-TargetResource +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter(Mandatory = $true)] + [String] + $DistributionPointGroup, + + [Parameter()] + [String[]] + $DistributionPoints, + + [Parameter()] + [String[]] + $DistributionPointsToInclude, + + [Parameter()] + [String[]] + $DistributionPointsToExclude, + + [Parameter()] + [ValidateSet('Present','Absent')] + [String] + $Ensure = 'Present' + ) + + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + + try + { + $state = Get-TargetResource -SiteCode $SiteCode -DistributionPointGroup $DistributionPointGroup + + if ($Ensure -eq 'Present') + { + if ($state.Ensure -eq 'Absent') + { + Write-Verbose -Message ($script:localizedData.AddGroup -f $DistributionPointGroup) + New-CMDistributionPointGroup -Name $DistributionPointGroup + } + + if ($DistributionPoints -or $DistributionPointsToInclude -or $DistributionPointsToExclude) + { + $distroArray = @{ + Match = $DistributionPoints + Include = $DistributionPointsToInclude + Exclude = $DistributionPointsToExclude + CurrentState = $state.DistributionPoints + } + + $distroCompare = Compare-MultipleCompares @distroArray + + if ($distroCompare.Missing) + { + foreach ($add in $distroCompare.Missing) + { + if (Get-CMDistributionPoint -Name $add) + { + $addParam = @{ + DistributionPointName = $add + DistributionPointGroupName = $DistributionPointGroup + } + + Write-Verbose -Message ($script:localizedData.AddDistro -f $add, $DistributionPointGroup) + Add-CMDistributionPointToGroup @addParam -Force + } + else + { + $errorMsg += ($script:localizedData.ErrorGroup -f $add) + } + } + } + + if ($distroCompare.Remove) + { + foreach ($remove in $distroCompare.Remove) + { + $removeParam = @{ + DistributionPointName = $remove + DistributionPointGroupName = $DistributionPointGroup + } + + Write-Verbose -Message ($script:localizedData.RemoveDistro -f $remove, $DistributionPointGroup) + Remove-CMDistributionPointFromGroup @removeParam -Force + } + } + } + } + elseif ($state.Ensure -eq 'Present') + { + Write-Verbose -Message ($script:localizedData.RemoveGroup -f $DistributionPointGroup) + Remove-CMDistributionPointGroup -Name $DistributionPointGroup -Force + } + + if ($errorMsg) + { + throw $errorMsg + } + } + catch + { + throw $_ + } + finally + { + Set-Location -Path "$env:temp" + } +} + +<# + .SYNOPSIS + This will set the desired state. + + .PARAMETER SiteCode + Specifies the site code for Configuration Manager site. + + .PARAMETER DistributionPointGroup + Specifies the Distribution Point Group name. + + .PARAMETER DistributionPoints + Specifies an array of distribution points to match to the distribution point group. + + .PARAMETER DistributionPointsToInclude + Specifies an array of distribution points to add to the distribution point group. + + .PARAMETER DistributionPointsToExclude + Specifies an array of distribution points to remove from the distribution point group. + + .PARAMETER Ensure + Specifies if the Distribution Point Group is to be present or absent. +#> +function Test-TargetResource +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param + ( + [Parameter(Mandatory = $true)] + [String] + $SiteCode, + + [Parameter(Mandatory = $true)] + [String] + $DistributionPointGroup, + + [Parameter()] + [String[]] + $DistributionPoints, + + [Parameter()] + [String[]] + $DistributionPointsToInclude, + + [Parameter()] + [String[]] + $DistributionPointsToExclude, + + [Parameter()] + [ValidateSet('Present','Absent')] + [String] + $Ensure = 'Present' + ) + + Import-ConfigMgrPowerShellModule -SiteCode $SiteCode + Set-Location -Path "$($SiteCode):\" + $state = Get-TargetResource -SiteCode $SiteCode -DistributionPointGroup $DistributionPointGroup + $result = $true + + if ($Ensure -eq 'Present') + { + if ($state.Ensure -eq 'Absent') + { + Write-Verbose -Message ($script:localizedData.GroupMissing -f $DistributionPointGroup) + $result = $false + } + elseif ($DistributionPoints -or $DistributionPointsToInclude -or $DistributionPointsToExclude) + { + if ($PSBoundParameters.ContainsKey('DistributionPoints')) + { + if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -or + $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) + { + Write-Warning -Message $script:localizedData.ParamIgnore + } + } + + $distroArray = @{ + Match = $DistributionPoints + Include = $DistributionPointsToInclude + Exclude = $DistributionPointsToExclude + CurrentState = $state.DistributionPoints + } + + $distroCompare = Compare-MultipleCompares @distroArray + + if ($distroCompare.Missing) + { + Write-Verbose -Message ($script:localizedData.DistroMissing -f ($distroCompare.Missing | Out-String)) + $result = $false + } + + if ($distroCompare.Remove) + { + Write-Verbose -Message ($script:localizedData.DistroRemove -f ($distroCompare.Remove | Out-String)) + $result = $false + } + } + } + elseif ($state.Ensure -eq 'Present') + { + Write-Verbose -Message $script:localizedData.DistroGroupPresent + $result = $false + } + + Write-Verbose -Message ($script:localizedData.TestState -f $result) + Set-Location -Path "$env:temp" + return $result +} + +Export-ModuleMember -Function *-TargetResource diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof new file mode 100644 index 0000000..bbd6957 --- /dev/null +++ b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof @@ -0,0 +1,10 @@ +[ClassVersion("1.0.0"), FriendlyName("CMDistributionPointGroup")] +class DSC_CMDistributionPointGroup : OMI_BaseResource +{ + [Key, Description("Specifies the Distribution Point Group name.")] String DistributionPointGroup; + [Required, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode; + [Write, Description("Specifies an array of distribution points to match to the distribution point group.")] String DistributionPoints[]; + [Write, Description("Specifies an array of distribution points to add to the distribution point group.")] String DistributionPointsToInclude[]; + [Write, Description("Specifies an array of distribution points to remove from the distribution point group.")] String DistributionPointsToExclude[]; + [Write, Description("Specifies whether the distribution point group is present or absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; +}; diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 b/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 new file mode 100644 index 0000000..3fb8eaf --- /dev/null +++ b/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 @@ -0,0 +1,13 @@ +ConvertFrom-StringData @' + RetrieveSettingValue = Getting results for Configuration Manager Distribution Point Group. + DistroMissing = NOTMATCH: Distribution Point Group is missing {0}. + DistroRemove = NOTMATCH: Distribution Point Group expected {0} to be absent. + ParamIgnore = DistributionPoints was specifed, ignoring DistributionPointsToInclude and DistributionPointsToExclude. + DistroGroupPresent = Distribution Group is present expected absent. + TestState = Test-TargetResource compliance check returned: {0}. + AddGroup = Adding {0} Distribution Point Group. + AddDistro = Adding {0} Distribution Point to Distribution Point Group {1}. + RemoveDistro = Removing {0} Distribution Point from Distribution Point Group {1}. + RemoveGroup = Removing {0} Distribution Point Group. + ErrorGroup = Distribution Point: {0} do not exist. +'@ diff --git a/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 b/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 new file mode 100644 index 0000000..15d3128 --- /dev/null +++ b/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 @@ -0,0 +1,18 @@ +<# + .SYNOPSIS + A DSC configuration script to remove a distribution point group from Configuration Manager. +#> +Configuration Example +{ + Import-DscResource -ModuleName ConfigMgrCBDsc + + Node localhost + { + CMDistributionPointGroup ExampleSettings + { + SiteCode = 'Lab' + DistributionPointGroup = 'DistroGroup1' + Ensure = 'Absent' + } + } +} diff --git a/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 b/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 new file mode 100644 index 0000000..47ff47f --- /dev/null +++ b/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 @@ -0,0 +1,42 @@ +<# + .SYNOPSIS + A DSC configuration script to add a distribution point group from Configuration Manager. +#> +Configuration Example +{ + Import-DscResource -ModuleName ConfigMgrCBDsc + + Node localhost + { + CMDistributionPointGroup ExampleSettingsCreate + { + SiteCode = 'Lab' + DistributionPointGroup = 'DistroGroup1' + Ensure = 'Present' + } + + CMDistributionPointGroup ExampleSettingsMatch + { + SiteCode = 'Lab' + DistributionPointGroup = 'DistroGroup2' + DistributionPoints = 'DP01.contoso.com','DP02.contoso.com' + Ensure = 'Present' + } + + CMDistributionPointGroup ExampleSettingsAdd + { + SiteCode = 'Lab' + DistributionPointGroup = 'DistroGroup3' + DistributionPointsToInclude = 'DP01.contoso.com','DP02.contoso.com' + Ensure = 'Present' + } + + CMDistributionPointGroup ExampleSettingsRemove + { + SiteCode = 'Lab' + DistributionPointGroup = 'DistroGroup4' + DistributionPointsToExclude = 'DP01.contoso.com' + Ensure = 'Present' + } + } +} diff --git a/tests/Unit/CMDistributionPointGroup.tests.ps1 b/tests/Unit/CMDistributionPointGroup.tests.ps1 new file mode 100644 index 0000000..64b818a --- /dev/null +++ b/tests/Unit/CMDistributionPointGroup.tests.ps1 @@ -0,0 +1,368 @@ +[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] +param () + +$script:dscModuleName = 'ConfigMgrCBDsc' +$script:dscResourceName = 'DSC_CMDistributionPointGroup' + +$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) + +Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs\ConfigMgrCBDscStub.psm1') -Force -WarningAction 'SilentlyContinue' + + # Import DscResource.Test Module +try +{ + Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop' +} +catch [System.IO.FileNotFoundException] +{ + throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.' +} + +# Variables used for each Initialize-TestEnvironment +$testEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dscResourceName ` + -TestType Unit + +function Invoke-TestCleanup +{ + Restore-TestEnvironment -TestEnvironment $script:testEnvironment +} + +# Begin Testing +try +{ + InModuleScope $script:dscResourceName { + Describe 'ConfigMgrCBDsc - DSC_CMDistributionPointGroup\Get-TargetResource' -Tag 'Get' { + BeforeAll { + $distributionPointGroup = @{ + MemberCount = 0 + Name = 'Group1' + SourceSite = 'LAB' + } + + $distributionPoint = @( + @{ + NetworkOSPath = '\\DP01.contoso.com' + SiteCode = 'LAB' + } + @{ + NetworkOSPath = '\\DP02.contoso.com' + SiteCode = 'LAB' + } + ) + + $getInput = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + } + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + } + + Context 'When retrieving Collection settings' { + + It 'Should return desired result when group exists and contains Distribution Points' { + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $distributionPointGroup } + Mock -CommandName Get-CMDistributionPoint -MockWith { $distributionPoint } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.DistributionPointGroup | Should -Be -ExpectedValue 'Group1' + $result.DistributionPoints | Should -Be -ExpectedValue 'DP01.contoso.com','DP02.contoso.com' + $result.Ensure | Should -Be -ExpectedValue 'Present' + } + + It 'Should return desired result when group exists and does not contain Distribution Points' { + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $distributionPointGroup } + Mock -CommandName Get-CMDistributionPoint -MockWith { $null } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.DistributionPointGroup | Should -Be -ExpectedValue 'Group1' + $result.DistributionPoints | Should -Be -ExpectedValue $null + $result.Ensure | Should -Be -ExpectedValue 'Present' + } + + It 'Should return desired result when group is absent' { + Mock -CommandName Get-CMDistributionPointGroup -MockWith { $null } + Mock -CommandName Get-CMDistributionPoint -MockWith { $null } + + $result = Get-TargetResource @getInput + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.DistributionPointGroup | Should -Be -ExpectedValue 'Group1' + $result.DistributionPoints | Should -Be -ExpectedValue $null + $result.Ensure | Should -Be -ExpectedValue 'Absent' + } + } + } + + Describe 'ConfigMgrCBDsc - DSC_CMDistributionPointGroup\Set-TargetResource' -Tag 'Set' { + BeforeAll { + $getReturnPresent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = @('DP01.contoso.com','DP02.contoso.com') + Ensure = 'Present' + } + + $groupPresentMatch = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = 'DP03.contoso.com' + } + + $groupPresent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + Ensure = 'Present' + } + + $groupPresentAddMultiple = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPointsToInclude = 'DP03.contoso.com','DP04.contoso.com' + } + + Mock -CommandName Import-ConfigMgrPowerShellModule + Mock -CommandName Set-Location + Mock -CommandName New-CMDistributionPointGroup + Mock -CommandName Add-CMDistributionPointToGroup + Mock -CommandName Remove-CMDistributionPointFromGroup + Mock -CommandName Remove-CMDistributionPointGroup + } + + Context 'When Set-TargetResource runs successfully when get returns absent' { + BeforeEach { + $getReturnAbsent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = $null + Ensure = 'Absent' + } + + Mock -CommandName Get-TargetResource -MockWith { $getReturnAbsent } + } + + It 'Should call expected commands when adding a Distribution Point Group' { + Mock -CommandName Get-CMDistributionPoint + + Set-TargetResource @groupPresent + 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 New-CMDistributionPointGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 0 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It + } + + It 'Should call expected commands when adding a Distribution Point Group and Distribution Points' { + Mock -CommandName Get-CMDistributionPoint -MockWith { $true } + + Set-TargetResource @groupPresentMatch + 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 New-CMDistributionPointGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 1 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It + } + } + + Context 'When Set-TargetResource runs successfully when get returns present' { + BeforeEach { + $groupAbsent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + Ensure = 'Absent' + } + + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + } + + It 'Should call expected commands when adding and removing Distribution Points to groups' { + Mock -CommandName Get-CMDistributionPoint -MockWith { $true } + + Set-TargetResource @groupPresentMatch + 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 New-CMDistributionPointGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 1 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 2 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It + } + + It 'Should call expected commands when removing Distribution Point Group' { + Mock -CommandName Get-CMDistributionPoint + + Set-TargetResource @groupAbsent + 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 New-CMDistributionPointGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 0 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 1 -Scope It + } + + It 'Should call expected commands when adding multiple Distribution Points to the group' { + Mock -CommandName Get-CMDistributionPoint -MockWith { $true } + + Set-TargetResource @groupPresentAddMultiple + 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 New-CMDistributionPointGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 2 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 2 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It + } + } + + Context 'When running Set-TargetResource should throw' { + BeforeEach { + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + } + + It 'Should call expected commands when Set-CMDistributionPoint throws' { + Mock -CommandName Get-CMDistributionPoint -MockWith { $true } -ParameterFilter { $Name -eq 'DP03.Contoso.com' } + Mock -CommandName Get-CMDistributionPoint -MockWith { $null } -ParameterFilter { $Name -eq 'DP04.Contoso.com' } + + { Set-TargetResource @groupPresentAddMultiple } | Should -Throw + 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 New-CMDistributionPointGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 2 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 1 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It + } + } + } + + Describe 'ConfigMgrCBDsc - DSC_CMDistributionPointGroup\Test-TargetResource' -Tag 'Test' { + BeforeAll { + $groupAbsent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + Ensure = 'Absent' + } + + $groupPresent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + Ensure = 'Present' + } + + Mock -CommandName Set-Location + Mock -CommandName Import-ConfigMgrPowerShellModule + } + + Context 'When running Test-TargetResource and get returns present' { + BeforeEach { + $getReturnPresent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = @('DP01.contoso.com','DP02.contoso.com') + Ensure = 'Present' + } + + $groupPresentMatch = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = 'DP03.contoso.com' + } + + $groupPresentInclude = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPointsToInclude = 'DP03.contoso.com' + } + + $groupPresentExclude = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPointsToExclude = 'DP02.contoso.com' + } + + $groupPresentWarningMatch = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = 'DP02.contoso.com' + DistributionPointsToInclude = 'DP03.contoso.com' + } + + $groupPresent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + Ensure = 'Present' + } + + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } + } + + It 'Should return desired result true when setting present' { + Test-TargetResource @groupPresent | Should -Be $true + } + + It 'Should return desired result false when DistributionPoints does not match get' { + Test-TargetResource @groupPresentMatch | Should -Be $false + } + + It 'Should return desired result false when DistributionPointsToInclude does not match get' { + Test-TargetResource @groupPresentInclude | Should -Be $false + } + + It 'Should return desired result false when DistributionPointsToExclude has a match with get' { + Test-TargetResource @groupPresentExclude | Should -Be $false + } + + It 'Should return desired result false when DistributionPointsToInclude and DistributionPoints does not match get' { + Test-TargetResource @groupPresentWarningMatch | Should -Be $false + } + + It 'Should return desired result false when get returns present and expected absent' { + Test-TargetResource @groupAbsent | Should -Be $false + } + } + + Context 'When running Test-TargetResource and get returns absent' { + BeforeEach { + $getReturnAbsent = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPoints = $null + Ensure = 'Absent' + } + + Mock -CommandName Get-TargetResource -MockWith { $getReturnAbsent } + } + + It 'Should return desired result true when get returns absent and expected absent' { + Test-TargetResource @groupAbsent | Should -Be $true + } + + It 'Should return desired result false when get returns absent and expected present' { + Test-TargetResource @groupPresent | Should -Be $false + } + } + } + } +} +finally +{ + Invoke-TestCleanup +} From 3827a12c07cc6f63b074997e15d14545fa7722e7 Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Mon, 29 Jun 2020 11:58:43 -0500 Subject: [PATCH 2/7] Removing -force and adding helper --- CHANGELOG.md | 1 + .../DSC_CMDistributionPointGroup.psm1 | 4 +- .../ConfigMgrCBDsc.ResourceHelper.psd1 | 1 + .../ConfigMgrCBDsc.ResourceHelper.psm1 | 110 ++++++++++++++++++ .../ConfigMgrCBDsc.ResourceHelper.tests.ps1 | 86 ++++++++++++++ 5 files changed, 200 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8374acf..04f71d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added ConvertTo-AnyCimInstance to the ResourceHelper - Added CMSiteMaintenance Resource - Added CMDistributionPointGroup Resource +- Added Compare-MultipleCompares to the ResourceHelper ### Changed diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 index 23e2543..6d21a49 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 +++ b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 @@ -153,7 +153,7 @@ function Set-TargetResource } Write-Verbose -Message ($script:localizedData.AddDistro -f $add, $DistributionPointGroup) - Add-CMDistributionPointToGroup @addParam -Force + Add-CMDistributionPointToGroup @addParam } else { @@ -172,7 +172,7 @@ function Set-TargetResource } Write-Verbose -Message ($script:localizedData.RemoveDistro -f $remove, $DistributionPointGroup) - Remove-CMDistributionPointFromGroup @removeParam -Force + Remove-CMDistributionPointFromGroup @removeParam } } } diff --git a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 index 09b67f4..8f0e798 100644 --- a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 +++ b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psd1 @@ -41,6 +41,7 @@ 'Get-BoundaryInfo' 'ConvertTo-ScheduleInterval' 'ConvertTo-AnyCimInstance' + 'Compare-MultipleCompares' ) # 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 f8455ce..fb7c03f 100644 --- a/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psm1 +++ b/source/Modules/ConfigMgrCBDsc.ResourceHelper/ConfigMgrCBDsc.ResourceHelper.psm1 @@ -407,6 +407,115 @@ function ConvertTo-AnyCimInstance -Property $property -ClientOnly } +<# + .SYNOPSIS + Returns the boundary ID based on Value and Type of boundary specified. + .PARAMETER Match + Specifies an array of values to validate if missing or extra settings compared to current state. + .PARAMETER Include + Specifies an array of values to validate if missing from current state. + .PARAMETER Exclude + Specifies an array of values to validate if extra compared to current state. + .PARAMETER CurrentState + Specifies an array to compare against for match, include, or exclude. +#> +function Compare-MultipleCompares +{ + [CmdletBinding()] + param + ( + [Parameter()] + [AllowEmptyString()] + [String[]] + $Match, + + [Parameter()] + [AllowEmptyString()] + [String[]] + $Include, + + [Parameter()] + [AllowEmptyString()] + [String[]] + $Exclude, + + [Parameter()] + [String[]] + $CurrentState + ) + + $missing = @() + $remove = @() + + if (-not [string]::IsNullOrEmpty($Match)) + { + $type = 'Match' + + if ($null -eq $CurrentState) + { + $missing = $Match + } + else + { + $compares = Compare-Object -ReferenceObject $Match -DifferenceObject $CurrentState + + foreach ($compare in $compares) + { + if ($compare.SideIndicator -eq '<=') + { + $missing += $compare.InputObject + } + else + { + $remove += $compare.InputObject + } + } + } + } + else + { + if (-not [string]::IsNullOrEmpty($Include)) + { + $type = 'Include' + + foreach ($item in $Include) + { + if ($CurrentState -notcontains $item) + { + $missing += $item + } + } + } + + if (-not [string]::IsNullOrEmpty($Exclude)) + { + if ($type -eq 'Include') + { + $type = 'Include, Exclude' + } + else + { + $type = 'Exclude' + } + + foreach ($item in $Exclude) + { + if ($CurrentState -contains $item) + { + $remove += ($CurrentState | Where-Object -FilterScript {$_ -eq $item}) + } + } + } + } + + return @{ + Type = $type + Missing = $missing + Remove = $remove + CurrentState = $CurrentState + } +} + Export-ModuleMember -Function @( 'Import-ConfigMgrPowerShellModule' 'Convert-CidrToIP' @@ -416,4 +525,5 @@ Export-ModuleMember -Function @( 'Get-BoundaryInfo' 'ConvertTo-ScheduleInterval' 'ConvertTo-AnyCimInstance' + 'Compare-MultipleCompares' ) diff --git a/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 b/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 index ccb1792..14604f8 100644 --- a/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 +++ b/tests/Unit/ConfigMgrCBDsc.ResourceHelper.tests.ps1 @@ -483,4 +483,90 @@ InModuleScope $script:subModuleName { } } } + + Describe "$moduleResourceName\Compare-MultipleCompares" { + BeforeAll { + $inputParamMatch = @{ + CurrentState = 'Device1','Device2' + Match = 'Device2','Device3' + Include = $null + Exclude = $null + } + + $inputParamCurrentNull = @{ + CurrentState = $null + Match = 'Device2','Device3' + Include = $null + Exclude = $null + } + + $inputParamInclude = @{ + CurrentState = 'Device1','Device2' + Match = $null + Include = 'Device2','Device3' + Exclude = $null + } + + $inputParamExclude = @{ + CurrentState = 'Device1','Device2' + Match = $null + Include = $null + Exclude = 'Device2','Device3' + } + + $inputParamExcludeInclude = @{ + CurrentState = 'Device1','Device2' + Match = $null + Include = 'Device4' + Exclude = 'Device2','Device3' + } + } + + Context 'When return is as expected' { + It 'Should return desired result for desired results with match' { + $result = Compare-MultipleCompares @inputParamMatch + $result | Should -BeOfType System.Collections.HashTable + $result.Type | Should -Be -ExpectedValue 'Match' + $result.Missing | Should -Be -ExpectedValue 'Device3' + $result.Remove | Should -Be -ExpectedValue 'Device1' + $result.CurrentState | Should -Be -ExpectedValue 'Device1','Device2' + } + + It 'Should return desired result for desired missing settings with match when current state is null' { + $result = Compare-MultipleCompares @inputParamCurrentNull + $result | Should -BeOfType System.Collections.HashTable + $result.Type | Should -Be -ExpectedValue 'Match' + $result.Missing | Should -Be -ExpectedValue 'Device2','Device3' + $result.Remove | Should -Be -ExpectedValue $null + $result.CurrentState | Should -Be -ExpectedValue $null + } + + It 'Should return desired result for desired missing settings with include' { + $result = Compare-MultipleCompares @inputParamInclude + $result | Should -BeOfType System.Collections.HashTable + $result.Type | Should -Be -ExpectedValue 'Include' + $result.Missing | Should -Be -ExpectedValue 'Device3' + $result.Remove | Should -Be -ExpectedValue $null + $result.CurrentState | Should -Be -ExpectedValue 'Device1','Device2' + } + + It 'Should return desired result for desired exclude settings with exclude' { + $result = Compare-MultipleCompares @inputParamExclude + $result | Should -BeOfType System.Collections.HashTable + $result.Type | Should -Be -ExpectedValue 'Exclude' + $result.Missing | Should -Be -ExpectedValue $null + $result.Remove | Should -Be -ExpectedValue 'Device2' + $result.CurrentState | Should -Be -ExpectedValue 'Device1','Device2' + } + + It 'Should return desired result for desired exclude settings with include and exclude' { + $result = Compare-MultipleCompares @inputParamExcludeInclude + $result | Should -BeOfType System.Collections.HashTable + $result.Type | Should -Be -ExpectedValue 'Include, Exclude' + $result.Missing | Should -Be -ExpectedValue 'Device4' + $result.Remove | Should -Be -ExpectedValue 'Device2' + $result.CurrentState | Should -Be -ExpectedValue 'Device1','Device2' + } + } + } } From b33e21192f013d99af95ef8b3c8aae7f6b221374 Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Mon, 29 Jun 2020 12:30:03 -0500 Subject: [PATCH 3/7] Updated Readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2d5afa9..e090f1f 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,8 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu - **CMPullDistributionPoint**: Provides a resource for modifying a distribution point and making the distribution point a Pull Distribution Point. - **CMSiteMaintenance**: Provides a resource for modifying the Site Maintenance tasks. +- **CMDistributionPointGroup**: Provides a resource for creating Distribution Point + Groups and adding Distribution Points to the group. ### xSccmPreReqs From 07e4094aadebb42e3bed163b0b3dadf8c22f847f Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Mon, 29 Jun 2020 14:52:02 -0500 Subject: [PATCH 4/7] Updated strings missing string --- .../en-US/DSC_CMDistributionPointGroup.strings.psd1 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 b/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 index 3fb8eaf..99daa0e 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 +++ b/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 @@ -1,13 +1,14 @@ ConvertFrom-StringData @' RetrieveSettingValue = Getting results for Configuration Manager Distribution Point Group. - DistroMissing = NOTMATCH: Distribution Point Group is missing {0}. - DistroRemove = NOTMATCH: Distribution Point Group expected {0} to be absent. + GroupMissing = NOTMATCH: {0} Distribution Point Group is absent expected present. + DistroMissing = NOTMATCH: Distribution Point Group is missing the following Distribution Point: {0}. + DistroRemove = NOTMATCH: Distribution Point Group expected the following Distribution Point to be absent: {0}. ParamIgnore = DistributionPoints was specifed, ignoring DistributionPointsToInclude and DistributionPointsToExclude. - DistroGroupPresent = Distribution Group is present expected absent. + DistroGroupPresent = NOTMATCH: Distribution Point Group is present expected absent. TestState = Test-TargetResource compliance check returned: {0}. AddGroup = Adding {0} Distribution Point Group. AddDistro = Adding {0} Distribution Point to Distribution Point Group {1}. RemoveDistro = Removing {0} Distribution Point from Distribution Point Group {1}. RemoveGroup = Removing {0} Distribution Point Group. - ErrorGroup = Distribution Point: {0} do not exist. + ErrorGroup = Distribution Point: {0} does not exist. '@ From bc148b577290639a07d394a4381bf34d134e0e6e Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Thu, 2 Jul 2020 16:11:53 -0500 Subject: [PATCH 5/7] At check for include and exclude --- .../DSC_CMDistributionPointGroup.psm1 | 25 +++++++++++++ .../DSC_CMDistributionPointGroup.strings.psd1 | 1 + tests/Unit/CMDistributionPointGroup.tests.ps1 | 35 +++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 index 6d21a49..a124fb6 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 +++ b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 @@ -124,6 +124,18 @@ function Set-TargetResource if ($Ensure -eq 'Present') { + if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -and + $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) + { + foreach ($item in $DistributionPointsToInclude) + { + if ($DistributionPointsToExclude -contains $item) + { + throw ($script:localizedData.DistroInEx -f $item) + } + } + } + if ($state.Ensure -eq 'Absent') { Write-Verbose -Message ($script:localizedData.AddGroup -f $DistributionPointGroup) @@ -259,6 +271,19 @@ function Test-TargetResource if ($Ensure -eq 'Present') { + if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -and + $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) + { + foreach ($item in $DistributionPointsToInclude) + { + if ($DistributionPointsToExclude -contains $item) + { + Write-Warning -Message ($script:localizedData.DistroInEx -f $item) + $result = $false + } + } + } + if ($state.Ensure -eq 'Absent') { Write-Verbose -Message ($script:localizedData.GroupMissing -f $DistributionPointGroup) diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 b/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 index 99daa0e..ba075ad 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 +++ b/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 @@ -11,4 +11,5 @@ ConvertFrom-StringData @' RemoveDistro = Removing {0} Distribution Point from Distribution Point Group {1}. RemoveGroup = Removing {0} Distribution Point Group. ErrorGroup = Distribution Point: {0} does not exist. + DistroInEx = DistributionPointsToInclude and DistributionPointsToExclude contain to same entry {0}, remove from one of the arrays. '@ diff --git a/tests/Unit/CMDistributionPointGroup.tests.ps1 b/tests/Unit/CMDistributionPointGroup.tests.ps1 index 64b818a..04342bc 100644 --- a/tests/Unit/CMDistributionPointGroup.tests.ps1 +++ b/tests/Unit/CMDistributionPointGroup.tests.ps1 @@ -233,6 +233,15 @@ try Context 'When running Set-TargetResource should throw' { BeforeEach { + $includeExclude = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPointsToExclude = 'DP02.contoso.com' + DistributionPointsToInclude = 'DP02.contoso.com' + Ensure = 'Present' + } + + $distroInEx = 'DistributionPointsToInclude and DistributionPointsToExclude contain to same entry DP02.contoso.com, remove from one of the arrays.' Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } } @@ -250,6 +259,20 @@ try Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It } + + It 'Should call expected commands when DistributionPointInclude and Exclude have the same Distribution Point' { + Mock -CommandName Get-CMDistributionPoint + + { Set-TargetResource @includeExclude } | Should -Throw -ExpectedMessage $distroInEx + 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 New-CMDistributionPointGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Get-CMDistributionPoint -Exactly -Times 0 -Scope It + Assert-MockCalled Add-CMDistributionPointToGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointFromGroup -Exactly -Times 0 -Scope It + Assert-MockCalled Remove-CMDistributionPointGroup -Exactly -Times 0 -Scope It + } } } @@ -311,6 +334,14 @@ try Ensure = 'Present' } + $includeExclude = @{ + SiteCode = 'Lab' + DistributionPointGroup = 'Group1' + DistributionPointsToExclude = 'DP02.contoso.com' + DistributionPointsToInclude = 'DP02.contoso.com' + Ensure = 'Present' + } + Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } } @@ -337,6 +368,10 @@ try It 'Should return desired result false when get returns present and expected absent' { Test-TargetResource @groupAbsent | Should -Be $false } + + It 'Shoud return desired result false when DistributionPointToInclude and Exclude contain the same DistributionPoint' { + Test-TargetResource @includeExclude | Should -Be $false + } } Context 'When running Test-TargetResource and get returns absent' { From fc73b187d31ef3708670db337c1f2e054b4bb660 Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Fri, 3 Jul 2020 12:38:48 -0500 Subject: [PATCH 6/7] rename and module and Parameter --- CHANGELOG.md | 2 +- README.md | 21 ++- source/ConfigMgrCBDsc.psd1 | 4 +- .../DSC_CMDistributionGroup.psm1} | 91 ++++++------ .../DSC_CMDistributionGroup.schema.mof | 10 ++ .../DSC_CMDistributionGroup.strings.psd1} | 18 +-- .../DSC_CMDistributionPointGroup.schema.mof | 10 -- .../CMDistributionGroup_Absent.ps1} | 8 +- .../CMDistributionGroup_Present.ps1 | 42 ++++++ .../CMDistributionPointGroup_Present.ps1 | 42 ------ ...ests.ps1 => CMDistributionGroup.tests.ps1} | 130 +++++++++--------- 11 files changed, 189 insertions(+), 189 deletions(-) rename source/DSCResources/{DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 => DSC_CMDistributionGroup/DSC_CMDistributionGroup.psm1} (79%) create mode 100644 source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.schema.mof rename source/DSCResources/{DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 => DSC_CMDistributionGroup/en-US/DSC_CMDistributionGroup.strings.psd1} (50%) delete mode 100644 source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof rename source/Examples/Resources/{CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 => CMDistributionGroup/CMDistributionGroup_Absent.ps1} (56%) create mode 100644 source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Present.ps1 delete mode 100644 source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 rename tests/Unit/{CMDistributionPointGroup.tests.ps1 => CMDistributionGroup.tests.ps1} (78%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16aebb5..676ea3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,7 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added CMPullDistributionPoint Resource - Added ConvertTo-AnyCimInstance to the ResourceHelper - Added CMSiteMaintenance Resource -- Added CMDistributionPointGroup Resource +- Added CMDistributionGroup Resource - Added Compare-MultipleCompares to the ResourceHelper ### Changed diff --git a/README.md b/README.md index e090f1f..905507e 100644 --- a/README.md +++ b/README.md @@ -861,23 +861,22 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu - [CMSiteMaintenance_SummaryTask_Enabled](Source\Examples\Resources\CMSiteMaintenance\CMSiteMaintenance_SummaryTask_Enabled.ps1) - [CMSiteMaintenance_UpdateAppCatTablesTask_Enabled](Source\Examples\Resources\CMSiteMaintenance\CMSiteMaintenance_UpdateAppCatTablesTask_Enabled.ps1) -### CMDistributionPointGroup +### CMDistributionGroup -- **[String] DistributionPointGroup** _(Key)_: Specifies the distribution point - group name. +- **[String] DistributionGroup** _(Key)_: Specifies the Distribution Group name. - **[String] SiteCode** _(Required)_: Specifies the Site Code for the Configuration Manager site. -- **[String] DistributionPoints[]** _(Write)_: Specifies an array of distribution - points to match to the distribution point group. +- **[String] DistributionPoints[]** _(Write)_: Specifies an array of Distribution + Points to match to the Distribution Group. - **[String] DistributionPointsToInclude[]** _(Write)_: Specifies an array of - distribution points to add to the distribution point group. + Distribution Points to add to the Distribution Group. - **[String] DistributionPointsToExclude[]** _(Write)_: Specifies an array of - distribution points to remove from the distribution point group. -- **[String] Ensure** _(Write)_: Specifies whether the distribution point group + Distribution Points to remove from the Distribution Group. +- **[String] Ensure** _(Write)_: Specifies whether the Distribution Group is present or absent. - Values include: { Present | Absent } -#### CMDistributionPointGroup Examples +#### CMDistributionGroup Examples -- [CMDistributionPointGroup_Present](Source\Examples\Resources\CMDistributionPointGroup\CMDistributionPointGroup_Present.ps1) -- [CMDistributionPointGroup_Absent](Source\Examples\Resources\CMDistributionPointGroup\CMDistributionPointGroup_Absent.ps1) +- [CMDistributionGroup_Present](Source\Examples\Resources\CMDistributionGroup\CMDistributionGroup_Present.ps1) +- [CMDistributionGroup_Absent](Source\Examples\Resources\CMDistributionGroup\CMDistributionGroup_Absent.ps1) diff --git a/source/ConfigMgrCBDsc.psd1 b/source/ConfigMgrCBDsc.psd1 index 956d137..289283e 100644 --- a/source/ConfigMgrCBDsc.psd1 +++ b/source/ConfigMgrCBDsc.psd1 @@ -66,7 +66,7 @@ 'CMPxeDistributionPoint' 'CMPullDistributionPoint' 'CMSiteMaintenance' - 'CMDistributionPointGroup' + 'CMDistributionGroup' ) <# @@ -83,7 +83,7 @@ 'SccmSqlSetup','SCCMInstall','CMIniFile','Collections','Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups', 'ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint','SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery', 'ServiceConnectionPoint','NetworkDiscovery','ReportingServicePoint','SystemDiscovery','PXEDistributionPoint','PullDistributionPoint', - 'SiteMaintenance','DistributionPointGroup') + 'SiteMaintenance','DistributionGroup') # A URL to the license for this module. LicenseUri = 'https://github.com/dsccommunity/ConfigMgrCBDsc/blob/master/LICENSE' diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 b/source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.psm1 similarity index 79% rename from source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 rename to source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.psm1 index a124fb6..af6ee12 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.psm1 +++ b/source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.psm1 @@ -13,8 +13,8 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US' .PARAMETER SiteCode Specifies the site code for Configuration Manager site. - .PARAMETER DistributionPointGroup - Specifies the Distribution Point Group name. + .PARAMETER DistributionGroup + Specifies the Distribution Group name. #> function Get-TargetResource { @@ -28,18 +28,18 @@ function Get-TargetResource [Parameter(Mandatory = $true)] [String] - $DistributionPointGroup + $DistributionGroup ) Write-Verbose -Message $script:localizedData.RetrieveSettingValue Import-ConfigMgrPowerShellModule -SiteCode $SiteCode Set-Location -Path "$($SiteCode):\" - $groupStatus = Get-CMDistributionPointGroup -Name $DistributionPointGroup + $groupStatus = Get-CMDistributionPointGroup -Name $DistributionGroup if ($groupStatus) { - $dplist = Get-CMDistributionPoint -DistributionPointGroupName $DistributionPointGroup + $dplist = Get-CMDistributionPoint -DistributionPointGroupName $DistributionGroup $dpMembers = @() foreach ($dp in $dplist) @@ -55,10 +55,10 @@ function Get-TargetResource } return @{ - SiteCode = $SiteCode - DistributionPointGroup = $DistributionPointGroup - DistributionPoints = $dpMembers - Ensure = $group + SiteCode = $SiteCode + DistributionGroup = $DistributionGroup + DistributionPoints = $dpMembers + Ensure = $group } } @@ -69,20 +69,20 @@ function Get-TargetResource .PARAMETER SiteCode Specifies the site code for Configuration Manager site. - .PARAMETER DistributionPointGroup - Specifies the Distribution Point Group name. + .PARAMETER DistributionGroup + Specifies the Distribution Group name. .PARAMETER DistributionPoints - Specifies an array of distribution points to match to the distribution point group. + Specifies an array of Distribution Points to match to the Distribution Group. .PARAMETER DistributionPointsToInclude - Specifies an array of distribution points to add to the distribution point group. + Specifies an array of Distribution Points to add to the Distribution Group. .PARAMETER DistributionPointsToExclude - Specifies an array of distribution points to remove from the distribution point group. + Specifies an array of Distribution Points to remove from the Distribution Group. .PARAMETER Ensure - Specifies if the Distribution Point Group is to be present or absent. + Specifies if the Distribution Group is to be present or absent. #> function Set-TargetResource { @@ -95,7 +95,7 @@ function Set-TargetResource [Parameter(Mandatory = $true)] [String] - $DistributionPointGroup, + $DistributionGroup, [Parameter()] [String[]] @@ -120,11 +120,12 @@ function Set-TargetResource try { - $state = Get-TargetResource -SiteCode $SiteCode -DistributionPointGroup $DistributionPointGroup + $state = Get-TargetResource -SiteCode $SiteCode -DistributionGroup $DistributionGroup if ($Ensure -eq 'Present') { - if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -and + if (-not $PSBoundParameters.ContainsKey('DistributionPoints') -and + $PSBoundParameters.ContainsKey('DistributionPointsToInclude') -and $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) { foreach ($item in $DistributionPointsToInclude) @@ -138,8 +139,8 @@ function Set-TargetResource if ($state.Ensure -eq 'Absent') { - Write-Verbose -Message ($script:localizedData.AddGroup -f $DistributionPointGroup) - New-CMDistributionPointGroup -Name $DistributionPointGroup + Write-Verbose -Message ($script:localizedData.AddGroup -f $DistributionGroup) + New-CMDistributionPointGroup -Name $DistributionGroup } if ($DistributionPoints -or $DistributionPointsToInclude -or $DistributionPointsToExclude) @@ -161,10 +162,10 @@ function Set-TargetResource { $addParam = @{ DistributionPointName = $add - DistributionPointGroupName = $DistributionPointGroup + DistributionPointGroupName = $DistributionGroup } - Write-Verbose -Message ($script:localizedData.AddDistro -f $add, $DistributionPointGroup) + Write-Verbose -Message ($script:localizedData.AddDistro -f $add, $DistributionGroup) Add-CMDistributionPointToGroup @addParam } else @@ -180,10 +181,10 @@ function Set-TargetResource { $removeParam = @{ DistributionPointName = $remove - DistributionPointGroupName = $DistributionPointGroup + DistributionPointGroupName = $DistributionGroup } - Write-Verbose -Message ($script:localizedData.RemoveDistro -f $remove, $DistributionPointGroup) + Write-Verbose -Message ($script:localizedData.RemoveDistro -f $remove, $DistributionGroup) Remove-CMDistributionPointFromGroup @removeParam } } @@ -191,8 +192,8 @@ function Set-TargetResource } elseif ($state.Ensure -eq 'Present') { - Write-Verbose -Message ($script:localizedData.RemoveGroup -f $DistributionPointGroup) - Remove-CMDistributionPointGroup -Name $DistributionPointGroup -Force + Write-Verbose -Message ($script:localizedData.RemoveGroup -f $DistributionGroup) + Remove-CMDistributionPointGroup -Name $DistributionGroup -Force } if ($errorMsg) @@ -217,20 +218,20 @@ function Set-TargetResource .PARAMETER SiteCode Specifies the site code for Configuration Manager site. - .PARAMETER DistributionPointGroup - Specifies the Distribution Point Group name. + .PARAMETER DistributionGroup + Specifies the Distribution Group name. .PARAMETER DistributionPoints - Specifies an array of distribution points to match to the distribution point group. + Specifies an array of Distribution Points to match to the Distribution Group. .PARAMETER DistributionPointsToInclude - Specifies an array of distribution points to add to the distribution point group. + Specifies an array of Distribution Points to add to the Distribution Group. .PARAMETER DistributionPointsToExclude - Specifies an array of distribution points to remove from the distribution point group. + Specifies an array of Distribution Points to remove from the Distribution Group. .PARAMETER Ensure - Specifies if the Distribution Point Group is to be present or absent. + Specifies if the Distribution Group is to be present or absent. #> function Test-TargetResource { @@ -244,7 +245,7 @@ function Test-TargetResource [Parameter(Mandatory = $true)] [String] - $DistributionPointGroup, + $DistributionGroup, [Parameter()] [String[]] @@ -266,12 +267,21 @@ function Test-TargetResource Import-ConfigMgrPowerShellModule -SiteCode $SiteCode Set-Location -Path "$($SiteCode):\" - $state = Get-TargetResource -SiteCode $SiteCode -DistributionPointGroup $DistributionPointGroup + $state = Get-TargetResource -SiteCode $SiteCode -DistributionGroup $DistributionGroup $result = $true if ($Ensure -eq 'Present') { - if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -and + if ($PSBoundParameters.ContainsKey('DistributionPoints')) + { + if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -or + $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) + { + Write-Warning -Message $script:localizedData.ParamIgnore + } + } + elseif (-not $PSBoundParameters.ContainsKey('DistributionPoints') -and + $PSBoundParameters.ContainsKey('DistributionPointsToInclude') -and $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) { foreach ($item in $DistributionPointsToInclude) @@ -286,20 +296,11 @@ function Test-TargetResource if ($state.Ensure -eq 'Absent') { - Write-Verbose -Message ($script:localizedData.GroupMissing -f $DistributionPointGroup) + Write-Verbose -Message ($script:localizedData.GroupMissing -f $DistributionGroup) $result = $false } elseif ($DistributionPoints -or $DistributionPointsToInclude -or $DistributionPointsToExclude) { - if ($PSBoundParameters.ContainsKey('DistributionPoints')) - { - if ($PSBoundParameters.ContainsKey('DistributionPointsToInclude') -or - $PSBoundParameters.ContainsKey('DistributionPointsToExclude')) - { - Write-Warning -Message $script:localizedData.ParamIgnore - } - } - $distroArray = @{ Match = $DistributionPoints Include = $DistributionPointsToInclude diff --git a/source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.schema.mof b/source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.schema.mof new file mode 100644 index 0000000..ca5a9b7 --- /dev/null +++ b/source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.schema.mof @@ -0,0 +1,10 @@ +[ClassVersion("1.0.0"), FriendlyName("CMDistributionGroup")] +class DSC_CMDistributionGroup : OMI_BaseResource +{ + [Key, Description("Specifies the Distribution Group name.")] String DistributionGroup; + [Required, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode; + [Write, Description("Specifies an array of Distribution Points to match to the Distribution Group.")] String DistributionPoints[]; + [Write, Description("Specifies an array of Distribution Points to add to the Distribution Group.")] String DistributionPointsToInclude[]; + [Write, Description("Specifies an array of Distribution Points to remove from the Distribution Group.")] String DistributionPointsToExclude[]; + [Write, Description("Specifies whether the Distribution Group is present or absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; +}; diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 b/source/DSCResources/DSC_CMDistributionGroup/en-US/DSC_CMDistributionGroup.strings.psd1 similarity index 50% rename from source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 rename to source/DSCResources/DSC_CMDistributionGroup/en-US/DSC_CMDistributionGroup.strings.psd1 index ba075ad..90f8683 100644 --- a/source/DSCResources/DSC_CMDistributionPointGroup/en-US/DSC_CMDistributionPointGroup.strings.psd1 +++ b/source/DSCResources/DSC_CMDistributionGroup/en-US/DSC_CMDistributionGroup.strings.psd1 @@ -1,15 +1,15 @@ ConvertFrom-StringData @' - RetrieveSettingValue = Getting results for Configuration Manager Distribution Point Group. - GroupMissing = NOTMATCH: {0} Distribution Point Group is absent expected present. - DistroMissing = NOTMATCH: Distribution Point Group is missing the following Distribution Point: {0}. - DistroRemove = NOTMATCH: Distribution Point Group expected the following Distribution Point to be absent: {0}. + RetrieveSettingValue = Getting results for Configuration Manager Distribution Group. + GroupMissing = NOTMATCH: {0} Distribution Group is absent expected present. + DistroMissing = NOTMATCH: Distribution Group is missing the following Distribution Point: {0}. + DistroRemove = NOTMATCH: Distribution Group expected the following Distribution Point to be absent: {0}. ParamIgnore = DistributionPoints was specifed, ignoring DistributionPointsToInclude and DistributionPointsToExclude. - DistroGroupPresent = NOTMATCH: Distribution Point Group is present expected absent. + DistroGroupPresent = NOTMATCH: Distribution Group is present expected absent. TestState = Test-TargetResource compliance check returned: {0}. - AddGroup = Adding {0} Distribution Point Group. - AddDistro = Adding {0} Distribution Point to Distribution Point Group {1}. - RemoveDistro = Removing {0} Distribution Point from Distribution Point Group {1}. - RemoveGroup = Removing {0} Distribution Point Group. + AddGroup = Adding {0} Distribution Group. + AddDistro = Adding {0} Distribution Point to Distribution Group {1}. + RemoveDistro = Removing {0} Distribution Point from Distribution Group {1}. + RemoveGroup = Removing {0} Distribution Group. ErrorGroup = Distribution Point: {0} does not exist. DistroInEx = DistributionPointsToInclude and DistributionPointsToExclude contain to same entry {0}, remove from one of the arrays. '@ diff --git a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof b/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof deleted file mode 100644 index bbd6957..0000000 --- a/source/DSCResources/DSC_CMDistributionPointGroup/DSC_CMDistributionPointGroup.schema.mof +++ /dev/null @@ -1,10 +0,0 @@ -[ClassVersion("1.0.0"), FriendlyName("CMDistributionPointGroup")] -class DSC_CMDistributionPointGroup : OMI_BaseResource -{ - [Key, Description("Specifies the Distribution Point Group name.")] String DistributionPointGroup; - [Required, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode; - [Write, Description("Specifies an array of distribution points to match to the distribution point group.")] String DistributionPoints[]; - [Write, Description("Specifies an array of distribution points to add to the distribution point group.")] String DistributionPointsToInclude[]; - [Write, Description("Specifies an array of distribution points to remove from the distribution point group.")] String DistributionPointsToExclude[]; - [Write, Description("Specifies whether the distribution point group is present or absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure; -}; diff --git a/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 b/source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Absent.ps1 similarity index 56% rename from source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 rename to source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Absent.ps1 index 15d3128..87ab009 100644 --- a/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Absent.ps1 +++ b/source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Absent.ps1 @@ -8,11 +8,11 @@ Configuration Example Node localhost { - CMDistributionPointGroup ExampleSettings + CMDistributionGroup ExampleSettings { - SiteCode = 'Lab' - DistributionPointGroup = 'DistroGroup1' - Ensure = 'Absent' + SiteCode = 'Lab' + DistributionGroup = 'DistroGroup1' + Ensure = 'Absent' } } } diff --git a/source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Present.ps1 b/source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Present.ps1 new file mode 100644 index 0000000..f6ed7be --- /dev/null +++ b/source/Examples/Resources/CMDistributionGroup/CMDistributionGroup_Present.ps1 @@ -0,0 +1,42 @@ +<# + .SYNOPSIS + A DSC configuration script to add a distribution point group from Configuration Manager. +#> +Configuration Example +{ + Import-DscResource -ModuleName ConfigMgrCBDsc + + Node localhost + { + CMDistributionGroup ExampleSettingsCreate + { + SiteCode = 'Lab' + DistributionGroup = 'DistroGroup1' + Ensure = 'Present' + } + + CMDistributionGroup ExampleSettingsMatch + { + SiteCode = 'Lab' + DistributionGroup = 'DistroGroup2' + DistributionPoints = 'DP01.contoso.com','DP02.contoso.com' + Ensure = 'Present' + } + + CMDistributionGroup ExampleSettingsAdd + { + SiteCode = 'Lab' + DistributionGroup = 'DistroGroup3' + DistributionPointsToInclude = 'DP01.contoso.com','DP02.contoso.com' + Ensure = 'Present' + } + + CMDistributionGroup ExampleSettingsRemove + { + SiteCode = 'Lab' + DistributionGroup = 'DistroGroup4' + DistributionPointsToExclude = 'DP01.contoso.com' + Ensure = 'Present' + } + } +} diff --git a/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 b/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 deleted file mode 100644 index 47ff47f..0000000 --- a/source/Examples/Resources/CMDistributionPointGroup/CMDistributionPointGroup_Present.ps1 +++ /dev/null @@ -1,42 +0,0 @@ -<# - .SYNOPSIS - A DSC configuration script to add a distribution point group from Configuration Manager. -#> -Configuration Example -{ - Import-DscResource -ModuleName ConfigMgrCBDsc - - Node localhost - { - CMDistributionPointGroup ExampleSettingsCreate - { - SiteCode = 'Lab' - DistributionPointGroup = 'DistroGroup1' - Ensure = 'Present' - } - - CMDistributionPointGroup ExampleSettingsMatch - { - SiteCode = 'Lab' - DistributionPointGroup = 'DistroGroup2' - DistributionPoints = 'DP01.contoso.com','DP02.contoso.com' - Ensure = 'Present' - } - - CMDistributionPointGroup ExampleSettingsAdd - { - SiteCode = 'Lab' - DistributionPointGroup = 'DistroGroup3' - DistributionPointsToInclude = 'DP01.contoso.com','DP02.contoso.com' - Ensure = 'Present' - } - - CMDistributionPointGroup ExampleSettingsRemove - { - SiteCode = 'Lab' - DistributionPointGroup = 'DistroGroup4' - DistributionPointsToExclude = 'DP01.contoso.com' - Ensure = 'Present' - } - } -} diff --git a/tests/Unit/CMDistributionPointGroup.tests.ps1 b/tests/Unit/CMDistributionGroup.tests.ps1 similarity index 78% rename from tests/Unit/CMDistributionPointGroup.tests.ps1 rename to tests/Unit/CMDistributionGroup.tests.ps1 index 04342bc..19c2b4c 100644 --- a/tests/Unit/CMDistributionPointGroup.tests.ps1 +++ b/tests/Unit/CMDistributionGroup.tests.ps1 @@ -2,7 +2,7 @@ param () $script:dscModuleName = 'ConfigMgrCBDsc' -$script:dscResourceName = 'DSC_CMDistributionPointGroup' +$script:dscResourceName = 'DSC_CMDistributionGroup' $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) @@ -33,7 +33,7 @@ function Invoke-TestCleanup try { InModuleScope $script:dscResourceName { - Describe 'ConfigMgrCBDsc - DSC_CMDistributionPointGroup\Get-TargetResource' -Tag 'Get' { + Describe 'ConfigMgrCBDsc - DSC_CMDistributionGroup\Get-TargetResource' -Tag 'Get' { BeforeAll { $distributionPointGroup = @{ MemberCount = 0 @@ -53,26 +53,26 @@ try ) $getInput = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + SiteCode = 'Lab' + DistributionGroup = 'Group1' } Mock -CommandName Import-ConfigMgrPowerShellModule Mock -CommandName Set-Location } - Context 'When retrieving Collection settings' { + Context 'When retrieving Distribution Group settings' { It 'Should return desired result when group exists and contains Distribution Points' { Mock -CommandName Get-CMDistributionPointGroup -MockWith { $distributionPointGroup } Mock -CommandName Get-CMDistributionPoint -MockWith { $distributionPoint } $result = Get-TargetResource @getInput - $result | Should -BeOfType System.Collections.HashTable - $result.SiteCode | Should -Be -ExpectedValue 'Lab' - $result.DistributionPointGroup | Should -Be -ExpectedValue 'Group1' - $result.DistributionPoints | Should -Be -ExpectedValue 'DP01.contoso.com','DP02.contoso.com' - $result.Ensure | Should -Be -ExpectedValue 'Present' + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.DistributionGroup | Should -Be -ExpectedValue 'Group1' + $result.DistributionPoints | Should -Be -ExpectedValue 'DP01.contoso.com','DP02.contoso.com' + $result.Ensure | Should -Be -ExpectedValue 'Present' } It 'Should return desired result when group exists and does not contain Distribution Points' { @@ -80,11 +80,11 @@ try Mock -CommandName Get-CMDistributionPoint -MockWith { $null } $result = Get-TargetResource @getInput - $result | Should -BeOfType System.Collections.HashTable - $result.SiteCode | Should -Be -ExpectedValue 'Lab' - $result.DistributionPointGroup | Should -Be -ExpectedValue 'Group1' - $result.DistributionPoints | Should -Be -ExpectedValue $null - $result.Ensure | Should -Be -ExpectedValue 'Present' + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.DistributionGroup | Should -Be -ExpectedValue 'Group1' + $result.DistributionPoints | Should -Be -ExpectedValue $null + $result.Ensure | Should -Be -ExpectedValue 'Present' } It 'Should return desired result when group is absent' { @@ -92,39 +92,39 @@ try Mock -CommandName Get-CMDistributionPoint -MockWith { $null } $result = Get-TargetResource @getInput - $result | Should -BeOfType System.Collections.HashTable - $result.SiteCode | Should -Be -ExpectedValue 'Lab' - $result.DistributionPointGroup | Should -Be -ExpectedValue 'Group1' - $result.DistributionPoints | Should -Be -ExpectedValue $null - $result.Ensure | Should -Be -ExpectedValue 'Absent' + $result | Should -BeOfType System.Collections.HashTable + $result.SiteCode | Should -Be -ExpectedValue 'Lab' + $result.DistributionGroup | Should -Be -ExpectedValue 'Group1' + $result.DistributionPoints | Should -Be -ExpectedValue $null + $result.Ensure | Should -Be -ExpectedValue 'Absent' } } } - Describe 'ConfigMgrCBDsc - DSC_CMDistributionPointGroup\Set-TargetResource' -Tag 'Set' { + Describe 'ConfigMgrCBDsc - DSC_CMDistributionGroup\Set-TargetResource' -Tag 'Set' { BeforeAll { $getReturnPresent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - DistributionPoints = @('DP01.contoso.com','DP02.contoso.com') - Ensure = 'Present' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + DistributionPoints = @('DP01.contoso.com','DP02.contoso.com') + Ensure = 'Present' } $groupPresentMatch = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - DistributionPoints = 'DP03.contoso.com' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + DistributionPoints = 'DP03.contoso.com' } $groupPresent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - Ensure = 'Present' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + Ensure = 'Present' } $groupPresentAddMultiple = @{ SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + DistributionGroup = 'Group1' DistributionPointsToInclude = 'DP03.contoso.com','DP04.contoso.com' } @@ -139,10 +139,10 @@ try Context 'When Set-TargetResource runs successfully when get returns absent' { BeforeEach { $getReturnAbsent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - DistributionPoints = $null - Ensure = 'Absent' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + DistributionPoints = $null + Ensure = 'Absent' } Mock -CommandName Get-TargetResource -MockWith { $getReturnAbsent } @@ -180,9 +180,9 @@ try Context 'When Set-TargetResource runs successfully when get returns present' { BeforeEach { $groupAbsent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - Ensure = 'Absent' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + Ensure = 'Absent' } Mock -CommandName Get-TargetResource -MockWith { $getReturnPresent } @@ -235,7 +235,7 @@ try BeforeEach { $includeExclude = @{ SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + DistributionGroup = 'Group1' DistributionPointsToExclude = 'DP02.contoso.com' DistributionPointsToInclude = 'DP02.contoso.com' Ensure = 'Present' @@ -276,18 +276,18 @@ try } } - Describe 'ConfigMgrCBDsc - DSC_CMDistributionPointGroup\Test-TargetResource' -Tag 'Test' { + Describe 'ConfigMgrCBDsc - DSC_CMDistributionGroup\Test-TargetResource' -Tag 'Test' { BeforeAll { $groupAbsent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - Ensure = 'Absent' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + Ensure = 'Absent' } $groupPresent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - Ensure = 'Present' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + Ensure = 'Present' } Mock -CommandName Set-Location @@ -297,46 +297,46 @@ try Context 'When running Test-TargetResource and get returns present' { BeforeEach { $getReturnPresent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - DistributionPoints = @('DP01.contoso.com','DP02.contoso.com') - Ensure = 'Present' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + DistributionPoints = @('DP01.contoso.com','DP02.contoso.com') + Ensure = 'Present' } $groupPresentMatch = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - DistributionPoints = 'DP03.contoso.com' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + DistributionPoints = 'DP03.contoso.com' } $groupPresentInclude = @{ SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + DistributionGroup = 'Group1' DistributionPointsToInclude = 'DP03.contoso.com' } $groupPresentExclude = @{ SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + DistributionGroup = 'Group1' DistributionPointsToExclude = 'DP02.contoso.com' } $groupPresentWarningMatch = @{ SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + DistributionGroup = 'Group1' DistributionPoints = 'DP02.contoso.com' DistributionPointsToInclude = 'DP03.contoso.com' } $groupPresent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - Ensure = 'Present' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + Ensure = 'Present' } $includeExclude = @{ SiteCode = 'Lab' - DistributionPointGroup = 'Group1' + DistributionGroup = 'Group1' DistributionPointsToExclude = 'DP02.contoso.com' DistributionPointsToInclude = 'DP02.contoso.com' Ensure = 'Present' @@ -377,10 +377,10 @@ try Context 'When running Test-TargetResource and get returns absent' { BeforeEach { $getReturnAbsent = @{ - SiteCode = 'Lab' - DistributionPointGroup = 'Group1' - DistributionPoints = $null - Ensure = 'Absent' + SiteCode = 'Lab' + DistributionGroup = 'Group1' + DistributionPoints = $null + Ensure = 'Absent' } Mock -CommandName Get-TargetResource -MockWith { $getReturnAbsent } From e93f1c397a7c24a2dcf542016374e60d12639c23 Mon Sep 17 00:00:00 2001 From: Easyreturns <20781445+jeffotterpohl@users.noreply.github.com> Date: Mon, 6 Jul 2020 09:37:09 -0500 Subject: [PATCH 7/7] Updated Per PR comments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 905507e..bcc32f1 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu - **CMPullDistributionPoint**: Provides a resource for modifying a distribution point and making the distribution point a Pull Distribution Point. - **CMSiteMaintenance**: Provides a resource for modifying the Site Maintenance tasks. -- **CMDistributionPointGroup**: Provides a resource for creating Distribution Point +- **CMDistributionGroup**: Provides a resource for creating Distribution Point Groups and adding Distribution Points to the group. ### xSccmPreReqs