Skip to content

Commit

Permalink
Merge pull request #52 from dsccommunity/DPGroupMembership
Browse files Browse the repository at this point in the history
DSC_CMDistributionPointGroupMembers Resource merge
  • Loading branch information
jeffotterpohl authored Jul 23, 2020
2 parents 25a5fde + a9ad36f commit 8e6bd86
Show file tree
Hide file tree
Showing 8 changed files with 778 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added CMSiteSystemServer Resource
- Added CMStatusReportingComponent Resource
- Added CMCMCollectionMembershipEvaluationComponent Resource
- Added CMDistributionPointGroupMembers Resource

### Changed

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu
Reporting Component and its properties.
- **CMCollectionMembershipEvaluationComponent**: Provides a resource for modifying
the SCCM Collection Membership Evaluation Component.
- **CMDistributionPointGroupMembers**: Provides a resource for adding Distribution
Groups to Distribution Points. This resource will not create Distribution Points
or Distribution Groups.

### xSccmPreReqs

Expand Down Expand Up @@ -1015,3 +1018,21 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu
#### CMCollectionMembershipEvaluationComponent Examples

- [CMCollectionMembershipEvaluationComponent_Example](Source\Examples\Resources\CMCollectionMembershipEvaluationComponent\CMCollectionMembershipEvaluationComponent_Example.ps1)

### CMDistributionPointGroupMembers

- **[String] DistributionPoint** _(Key)_: Specifies the Distribution Point to modify
Distribution Point Group membership.
- **[String] SiteCode** _(Required)_: Specifies the Site Code for the Configuration
Manager site.
- **[String] DistributionGroups[]** _(Write)_: Specifies an array of Distribution
Groups to match on the Distribution Point.
- **[String] DistributionGroupsToInclude[]** _(Write)_: Specifies an array of
Distribution Groups to add to the Distribution Point.
- **[String] DistributionGroupsToExclude[]** _(Write)_: Specifies an array of
Distribution Groups to remove from the Distribution Point.
- **[String] DPStatus** _(Read)_: Specifies if the DP role is installed.

#### CMDistributionPointGroupMembers Example

- [CMDistributionPointGroupMembers](Source\Examples\Resources\CMDistributionPointGroupMembers\CMDistributionPointGroupMembers.ps1)
4 changes: 3 additions & 1 deletion source/ConfigMgrCBDsc.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
'CMSiteSystemServer'
'CMStatusReportingComponent'
'CMCollectionMembershipEvaluationComponent'
'CMDistributionPointGroupMembers'
)

<#
Expand All @@ -87,7 +88,8 @@
'SccmSqlSetup','SCCMInstall','CMIniFile','Collections','Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups',
'ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint','SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery',
'ServiceConnectionPoint','NetworkDiscovery','ReportingServicePoint','SystemDiscovery','PXEDistributionPoint','PullDistributionPoint',
'SiteMaintenance','AdministrativeUser','DistributionGroup','SiteSystemServer','StatusReportingComponent','CollectionMembershipEvaluationComponent')
'SiteMaintenance','AdministrativeUser','DistributionGroup','SiteSystemServer','StatusReportingComponent','CollectionMembershipEvaluationComponent',
'DistributionPointGroupMembers')

# A URL to the license for this module.
LicenseUri = 'https://github.com/dsccommunity/ConfigMgrCBDsc/blob/master/LICENSE'
Expand Down
Original file line number Diff line number Diff line change
@@ -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 DistributionPoint
Specifies the Distribution Point to modify Distribution Point Group membership.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[String]
$DistributionPoint
)

Write-Verbose -Message $script:localizedData.RetrieveSettingValue
Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"

$status = Get-CMDistributionPoint -SiteCode $SiteCode -SiteSystemServerName $DistributionPoint

if ($status)
{
$dpg = Get-CMDistributionPointGroup
if ($dpg)
{
$groups = @()
foreach ($group in $dpg)
{
$groupname = Get-CMDistributionPoint -DistributionPointGroupName $group.Name
$dp = $groupname | Where-Object -FilterScript {$_.NetworkOSPath -eq "\\$DistributionPoint"}
if ($dp)
{
$groups += $group.Name
}
}
}

$dpInstall = 'Present'
}
else
{
$dpInstall = 'Absent'
}

return @{
SiteCode = $SiteCode
DistributionPoint = $DistributionPoint
DistributionGroups = $groups
DPStatus = $dpInstall
}
}

<#
.SYNOPSIS
This will set the desired state.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
.PARAMETER DistributionPoint
Specifies the Distribution Point to modify Distribution Point Group membership.
.PARAMETER DistributionGroups
Specifies an array of Distribution Groups to match on the Distribution Point.
.PARAMETER DistributionGroupsToInclude
Specifies an array of Distribution Groups to add to the Distribution Point.
.PARAMETER DistributionGroupsToExclude
Specifies an array of Distribution Groups to remove from the Distribution Point.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[String]
$DistributionPoint,

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

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

[Parameter()]
[String[]]
$DistributionGroupsToExclude
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"

try
{
$state = Get-TargetResource -SiteCode $SiteCode -DistributionPoint $DistributionPoint

if ($state.DPStatus -eq 'Absent')
{
throw ($script:localizedData.DistroPointInstall -f $SiteServerName)
}

if (-not $PSBoundParameters.ContainsKey('DistributionGroups') -and
$PSBoundParameters.ContainsKey('DistributionGroupsToInclude') -and
$PSBoundParameters.ContainsKey('DistributionGroupsToExclude'))
{
foreach ($item in $DistributionGroupsToInclude)
{
if ($DistributionGroupsToExclude -contains $item)
{
throw ($script:localizedData.ErrorBoth -f $item)
}
}
}

if ($DistributionGroups -or $DistributionGroupsToInclude -or $DistributionGroupsToExclude)
{
$distroArray = @{
Match = $DistributionGroups
Include = $DistributionGroupsToInclude
Exclude = $DistributionGroupsToExclude
CurrentState = $state.DistributionGroups
}

$distroCompare = Compare-MultipleCompares @distroArray

if ($distroCompare.Missing)
{
foreach ($add in $distroCompare.Missing)
{
if (Get-CMDistributionPointGroup -Name $add)
{
$addParams = @{
DistributionPointName = $DistributionPoint
DistributionPointGroupName = $add
}

Write-Verbose -Message ($script:localizedData.AddDistroGroup -f $add, $DistributionPoint)
Add-CMDistributionPointToGroup @addParams
}
else
{
[array]$errorMsg += $add
}
}
}

if ($distroCompare.Remove)
{
foreach ($remove in $distroCompare.Remove)
{
$removeParam = @{
DistributionPointName = $DistributionPoint
DistributionPointGroupName = $remove
}

Write-Verbose -Message ($script:localizedData.RemoveDistroGroup -f $remove, $DistributionPoint)
Remove-CMDistributionPointFromGroup @removeParam
}
}
}

if ($errorMsg)
{
throw ($script:localizedData.ErrorGroup -f ($errorMsg | Out-String))
}
}
catch
{
throw $_
}
finally
{
Set-Location -Path "$env:temp"
}
}

<#
.SYNOPSIS
This will test the desired state.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
.PARAMETER DistributionPoint
Specifies the Distribution Point to modify Distribution Point Group membership.
.PARAMETER DistributionGroups
Specifies an array of Distribution Groups to match on the Distribution Point.
.PARAMETER DistributionGroupsToInclude
Specifies an array of Distribution Groups to add to the Distribution Point.
.PARAMETER DistributionGroupsToExclude
Specifies an array of Distribution Groups to remove from the Distribution Point.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[String]
$DistributionPoint,

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

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

[Parameter()]
[String[]]
$DistributionGroupsToExclude
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"
$state = Get-TargetResource -SiteCode $SiteCode -DistributionPoint $DistributionPoint
$result = $true

if ($state.DPStatus -eq 'Absent')
{
Write-Warning -Message ($script:localizedData.DistroPointInstall -f $DistributionPoint)
$result = $false
}
else
{
if ($PSBoundParameters.ContainsKey('DistributionGroups'))
{
if ($PSBoundParameters.ContainsKey('DistributionGroupsToInclude') -or
$PSBoundParameters.ContainsKey('DistributionGroupsToExclude'))
{
Write-Warning -Message $script:localizedData.ParamIgnore
}
}
elseif ($PSBoundParameters.ContainsKey('DistributionGroupsToInclude') -and
$PSBoundParameters.ContainsKey('DistributionGroupsToExclude'))
{
foreach ($item in $DistributionGroupsToInclude)
{
if ($DistributionGroupsToExclude -contains $item)
{
Write-Warning -Message ($script:localizedData.ErrorBoth -f $item)
$result = $false
}
}
}

if ($DistributionGroups -or $DistributionGroupsToInclude -or $DistributionGroupsToExclude)
{
$distroArray = @{
Match = $DistributionGroups
Include = $DistributionGroupsToInclude
Exclude = $DistributionGroupsToExclude
CurrentState = $state.DistributionGroups
}

$distroCompare = Compare-MultipleCompares @distroArray

if ($distroCompare.Missing)
{
Write-Verbose -Message ($script:localizedData.GroupMissing -f $DistributionPoint, ($distroCompare.Missing | Out-String))
$result = $false
}

if ($distroCompare.Remove)
{
Write-Verbose -Message ($script:localizedData.GroupExclude -f $DistributionPoint, ($distroCompare.Remove | Out-String))
$result = $false
}
}
}

Write-Verbose -Message ($script:localizedData.TestState -f $result)
Set-Location -Path "$env:temp"
return $result
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[ClassVersion("1.0.0"), FriendlyName("CMDistributionPointGroupMembers")]
class DSC_CMDistributionPointGroupMembers : OMI_BaseResource
{
[Key, Description("Specifies the Distribution Point to modify Distribution Point Group membership.")] String DistributionPoint;
[Required, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode;
[Write, Description("Specifies an array of Distribution Groups to match on the Distribution Point.")] String DistributionGroups[];
[Write, Description("Specifies an array of Distribution Groups to add to the Distribution Point.")] String DistributionGroupsToInclude[];
[Write, Description("Specifies an array of Distribution Groups to remove from the Distribution Point.")] String DistributionGroupsToExclude[];
[Read, Description("Specifies if the Distribution Point role is installed.")] String DPStatus;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ConvertFrom-StringData @'
RetrieveSettingValue = Getting results for Configuration Manager Distribution Point Group members.
DistroPointInstall = The distribution point role on {0} is not installed, run DSC_CMDistibutionPoint to install the role.
GroupMissing = NOTMATCH: {0} is missing Distribution Point Group {1}.
GroupExclude = NOTMATCH: {0} was expecting Distribution Point Group to be absent {1}.
TestState = Test-TargetResource compliance check returned: {0}.
AddDistroGroup = Adding {0} group to Distribution Point {1}.
RemoveDistroGroup = Removing {0} group from Distribution Point {1}.
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.
'@
Loading

0 comments on commit 8e6bd86

Please sign in to comment.