Skip to content

Commit

Permalink
Merge pull request #110 from nyanhp/collectionasmember
Browse files Browse the repository at this point in the history
CMDistributionGroup add collection members
  • Loading branch information
NEllis280 authored Jan 10, 2023
2 parents 7432eca + 4a858b2 commit c567b36
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ version of DSCResource.Common

- Added CMHierarchySetting resource

- CMDistributionGroup added properties to configure collection membership in Distribution Point Group

- Pin old version of ChangeLogManagement module to ensure module build.

## [3.0.0] - 2022-01-03

### Added
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,12 @@ you are using apply and auto correct.
Security Scopes to add to the Distribution Group.
- **[String] SecurityScopesToExclude[]** _(Write)_: Specifies an array of
Security Scopes to remove from the Distribution Group.
- **[String] Collections[]** _(Write)_: Specifies an array of Collections
to match to the Distribution Group.
- **[String] CollectionsToInclude[]** _(Write)_: Specifies an array of
Collections to add to the Distribution Group.
- **[String] CollectionsToExclude[]** _(Write)_: Specifies an array of
Collections to remove from the Distribution Group.
- **[String] Ensure** _(Write)_: Specifies whether the Distribution Group
is present or absent.
- Values include: { Present | Absent }
Expand Down
2 changes: 1 addition & 1 deletion RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Pester = '4.10.1'
Plaster = 'latest'
ModuleBuilder = 'latest'
ChangelogManagement = 'latest'
ChangelogManagement = '2.1.4'
Sampler = 'latest'
'Sampler.GitHubTasks' = 'latest'
MarkdownLinkCheck = 'latest'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function Get-TargetResource
{
$dplist = Get-CMDistributionPoint -DistributionPointGroupName $DistributionGroup
$dpMembers = @()
$scopes = @()
$collections = @()

foreach ($dp in $dplist)
{
Expand All @@ -51,7 +53,13 @@ function Get-TargetResource

foreach ($item in $scopeObject)
{
[array]$scopes += $item.CategoryName
$scopes += $item.CategoryName
}

$collectionObject = Get-CMCollection -DistributionPointGroup $groupStatus
foreach ($collection in $collectionObject)
{
$collections += $collection.Name
}

$group = 'Present'
Expand All @@ -66,6 +74,7 @@ function Get-TargetResource
DistributionGroup = $DistributionGroup
DistributionPoints = $dpMembers
SecurityScopes = $scopes
Collections = $collections
Ensure = $group
}
}
Expand Down Expand Up @@ -98,6 +107,15 @@ function Get-TargetResource
.PARAMETER SecurityScopesToExclude
Specifies an array of Security Scopes to remove from the Distribution Group.
.PARAMETER Collections
Specifies an array of Collection names to match to the Distribution Group.
.PARAMETER CollectionsToInclude
Specifies an array of Collection names to add to the Distribution Group.
.PARAMETER CollectionsToExclude
Specifies an array of Collection names to remove from the Distribution Group.
.PARAMETER Ensure
Specifies if the Distribution Group is to be present or absent.
#>
Expand Down Expand Up @@ -138,6 +156,18 @@ function Set-TargetResource
[String[]]
$SecurityScopesToExclude,

[Parameter()]
[string[]]
$Collections,

[Parameter()]
[String[]]
$CollectionsToInclude,

[Parameter()]
[String[]]
$CollectionsToExclude,

[Parameter()]
[ValidateSet('Present','Absent')]
[String]
Expand Down Expand Up @@ -179,6 +209,19 @@ function Set-TargetResource
}
}

if (-not $PSBoundParameters.ContainsKey('Collections') -and
$PSBoundParameters.ContainsKey('CollectionsToInclude') -and
$PSBoundParameters.ContainsKey('CollectionsToExclude'))
{
foreach ($item in $CollectionsToInclude)
{
if ($CollectionsToExclude -contains $item)
{
throw ($script:localizedData.CollectionInEx -f $item)
}
}
}

if ($state.Ensure -eq 'Absent')
{
Write-Verbose -Message ($script:localizedData.AddGroup -f $DistributionGroup)
Expand Down Expand Up @@ -273,6 +316,47 @@ function Set-TargetResource
}
}
}

if ($Collections -or $CollectionsToInclude -or $CollectionsToExclude)
{
$dgObject = Get-CMDistributionPointGroup -Name $DistributionGroup

$collectionsArray = @{
Match = $Collections
Include = $CollectionsToInclude
Exclude = $CollectionsToExclude
CurrentState = $state.Collections
}

$collectionsCompare = Compare-MultipleCompares @collectionsArray

if ($collectionsCompare.Missing)
{
$collectionError = 'Collections'

foreach ($add in $collectionsCompare.Missing)
{
if (Get-CMCollection -Name $add)
{
Write-Verbose -Message ($script:localizedData.AddCollection -f $add, $DistributionGroup)
Add-CMCollectionToDistributionPointGroup -CollectionName $add -DistributionPointGroup $dgObject
}
else
{
$errorMsg += ($script:localizedData.ErrorGroup -f $collectionError, $add)
}
}
}

if ($collectionsCompare.Remove)
{
foreach ($remove in $collectionsCompare.Remove)
{
Write-Verbose -Message ($script:localizedData.RemoveCollection -f $remove, $DistributionGroup)
Remove-CMCollectionFromDistributionPointGroup -CollectionName $remove -InputObject $dgObject
}
}
}
}
elseif ($state.Ensure -eq 'Present')
{
Expand Down Expand Up @@ -323,6 +407,15 @@ function Set-TargetResource
.PARAMETER SecurityScopesToExclude
Specifies an array of Security Scopes to remove from the Distribution Group.
.PARAMETER Collections
Specifies an array of Collection names to match to the Distribution Group.
.PARAMETER CollectionsToInclude
Specifies an array of Collection names to add to the Distribution Group.
.PARAMETER CollectionsToExclude
Specifies an array of Collection names to remove from the Distribution Group.
.PARAMETER Ensure
Specifies if the Distribution Group is to be present or absent.
#>
Expand Down Expand Up @@ -364,6 +457,18 @@ function Test-TargetResource
[String[]]
$SecurityScopesToExclude,

[Parameter()]
[string[]]
$Collections,

[Parameter()]
[String[]]
$CollectionsToInclude,

[Parameter()]
[String[]]
$CollectionsToExclude,

[Parameter()]
[ValidateSet('Present','Absent')]
[String]
Expand Down Expand Up @@ -415,7 +520,29 @@ function Test-TargetResource
{
if ($SecurityScopesToExclude -contains $item)
{
Write-Warning -Message ($script:localizedData.DistroInEx -f $item)
Write-Warning -Message ($script:localizedData.ScopeInEx -f $item)
$result = $false
}
}
}

if ($PSBoundParameters.ContainsKey('Collections'))
{
if ($PSBoundParameters.ContainsKey('CollectionsToInclude') -or
$PSBoundParameters.ContainsKey('CollectionsToExclude'))
{
Write-Warning -Message $script:localizedData.ParamIgnoreCollections
}
}
elseif (-not $PSBoundParameters.ContainsKey('Collections') -and
$PSBoundParameters.ContainsKey('CollectionsToInclude') -and
$PSBoundParameters.ContainsKey('CollectionsToExclude'))
{
foreach ($item in $CollectionsToInclude)
{
if ($CollectionsToExclude -contains $item)
{
Write-Warning -Message ($script:localizedData.CollectionInEx -f $item)
$result = $false
}
}
Expand Down Expand Up @@ -475,6 +602,30 @@ function Test-TargetResource
$result = $false
}
}

if ($Collections -or $CollectionsToInclude -or $CollectionsToExclude)
{
$collectionArray = @{
Match = $Collections
Include = $CollectionsToInclude
Exclude = $CollectionsToExclude
CurrentState = $state.Collections
}

$collectionCompare = Compare-MultipleCompares @collectionArray

if ($collectionCompare.Missing)
{
Write-Verbose -Message ($script:localizedData.CollectionMissing -f ($collectionCompare.Missing | Out-String))
$result = $false
}

if ($collectionCompare.Remove)
{
Write-Verbose -Message ($script:localizedData.CollectionRemove -f ($collectionCompare.Remove | Out-String))
$result = $false
}
}
}
}
elseif ($state.Ensure -eq 'Present')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ class DSC_CMDistributionGroup : OMI_BaseResource
[Write, Description("Specifies an array of Security Scopes to match to the Distribution Group.")] String SecurityScopes[];
[Write, Description("Specifies an array of Security Scopes to add to the Distribution Group.")] String SecurityScopesToInclude[];
[Write, Description("Specifies an array of Security Scopes to remove from the Distribution Group.")] String SecurityScopesToExclude[];
[Write, Description("Specifies an array of Collection names to match to the Distribution Group.")] String Collections[];
[Write, Description("Specifies an array of Collection names to add to the Distribution Group.")] String CollectionsToInclude[];
[Write, Description("Specifies an array of Collection names to remove from the Distribution Group.")] String CollectionsToExclude[];
[Write, Description("Specifies whether the Distribution Group is present or absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
ConvertFrom-StringData @'
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}.
ScopeMissing = NOTMatch: Distribution Group expected the following Scopes: {0}.
ScopeRemove = NOTMATCH: Distribution Group expected the following Scopes to be absent: {0}.
ParamIgnore = DistributionPoints was specifed, ignoring DistributionPointsToInclude and DistributionPointsToExclude.
ParamIgnoreScopes = SecurityScopes was specifed, ignoring SecurityScopesToInclude and SecurityScopesToExclude.
DistroGroupPresent = NOTMATCH: Distribution Group is present expected absent.
TestState = Test-TargetResource compliance check returned: {0}.
AddGroup = Adding {0} Distribution Group.
AddDistro = Adding {0} Distribution Point to Distribution Group {1}.
RemoveDistro = Removing {0} Distribution Point from Distribution Group {1}.
AddScope = Adding {0} Security Scope to Distribution Group {1}.
RemoveScope = Removing {0} Security Scope from Distribution Group {1}.
RemoveGroup = Removing {0} Distribution Group.
ErrorGroup = {0}: {1} does not exist.
DistroInEx = DistributionPointsToInclude and DistributionPointsToExclude contain to same entry {0}, remove from one of the arrays.
ScopeInEx = SecurityScopesToInclude and SecurityScopesToExclude contain to same entry {0}, remove from one of the arrays.
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}.
ScopeMissing = NOTMatch: Distribution Group expected the following Scopes: {0}.
ScopeRemove = NOTMATCH: Distribution Group expected the following Scopes to be absent: {0}.
CollectionMissing = NOTMatch: Distribution Group expected the following Collections: {0}.
CollectionRemove = NOTMATCH: Distribution Group expected the following Collections to be absent: {0}.
ParamIgnore = DistributionPoints was specifed, ignoring DistributionPointsToInclude and DistributionPointsToExclude.
ParamIgnoreScopes = SecurityScopes was specifed, ignoring SecurityScopesToInclude and SecurityScopesToExclude.
ParamIgnoreCollections = Collections was specifed, ignoring CollectionsToInclude and CollectionsToExclude.
DistroGroupPresent = NOTMATCH: Distribution Group is present expected absent.
TestState = Test-TargetResource compliance check returned: {0}.
AddGroup = Adding {0} Distribution Group.
AddDistro = Adding {0} Distribution Point to Distribution Group {1}.
RemoveDistro = Removing {0} Distribution Point from Distribution Group {1}.
AddScope = Adding {0} Security Scope to Distribution Group {1}.
RemoveScope = Removing {0} Security Scope from Distribution Group {1}.
AddCollection = Adding {0} Collection to Distribution Group {1}.
RemoveCollection = Removing {0} Collection from Distribution Group {1}.
RemoveGroup = Removing {0} Distribution Group.
ErrorGroup = {0}: {1} does not exist.
DistroInEx = DistributionPointsToInclude and DistributionPointsToExclude contain to same entry {0}, remove from one of the arrays.
ScopeInEx = SecurityScopesToInclude and SecurityScopesToExclude contain to same entry {0}, remove from one of the arrays.
CollectionInEx = CollectionsToInclude and CollectionsToExclude contain to same entry {0}, remove from one of the arrays.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Configuration Example
DistributionGroup = 'DistroGroup2'
DistributionPoints = 'DP01.contoso.com','DP02.contoso.com'
SecurityScopes = 'Scope1','Scope2'
Collections = 'Collection 1', 'Collection 2'
Ensure = 'Present'
}

Expand All @@ -30,6 +31,7 @@ Configuration Example
DistributionGroup = 'DistroGroup3'
DistributionPointsToInclude = 'DP01.contoso.com','DP02.contoso.com'
SecurityScopesToInclude = 'Scope1','Scope2'
CollectionsToInclude = 'Collection 1', 'Collection 2'
Ensure = 'Present'
}

Expand All @@ -39,6 +41,7 @@ Configuration Example
DistributionGroup = 'DistroGroup4'
DistributionPointsToExclude = 'DP01.contoso.com'
SecurityScopesToExclude = 'Scope1'
CollectionsToExclude = 'Collection 1', 'Collection 2'
Ensure = 'Present'
}
}
Expand Down
Loading

0 comments on commit c567b36

Please sign in to comment.