Skip to content

Commit

Permalink
Merge pull request #50 from dsccommunity/DistributionGroups
Browse files Browse the repository at this point in the history
Custom module to add Distribution Point groups
  • Loading branch information
jeffotterpohl authored Jul 6, 2020
2 parents db12acc + b86437a commit 65a95c3
Show file tree
Hide file tree
Showing 9 changed files with 850 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added CMSiteMaintenance Resource
- Added CMAdministrativeUser Resource
- Added Compare-MultipleCompares to the ResourceHelper
- Added CMDistributionGroup Resource

### Changed

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu
- **CMSiteMaintenance**: Provides a resource for modifying the Site Maintenance tasks.
- **CMAdministrativeUser**: Provides a resource for adding, removing, and configuring
administrative users.
- **CMDistributionGroup**: Provides a resource for creating Distribution Point
Groups and adding Distribution Points to the group.

### xSccmPreReqs

Expand Down Expand Up @@ -892,3 +894,23 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu

- [CMAdministrativeUser_Absent](Source\Examples\Resources\CMAdministrativeUser\CMAdministrativeUser_Absent.ps1)
- [CMAdministrativeUser_Present](Source\Examples\Resources\CMAdministrativeUser\CMAdministrativeUser_Present.ps1)

### CMDistributionGroup

- **[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 Group.
- **[String] DistributionPointsToInclude[]** _(Write)_: Specifies an array of
Distribution Points to add to the Distribution Group.
- **[String] DistributionPointsToExclude[]** _(Write)_: Specifies an array of
Distribution Points to remove from the Distribution Group.
- **[String] Ensure** _(Write)_: Specifies whether the Distribution Group
is present or absent.
- Values include: { Present | Absent }

#### CMDistributionGroup Examples

- [CMDistributionGroup_Present](Source\Examples\Resources\CMDistributionGroup\CMDistributionGroup_Present.ps1)
- [CMDistributionGroup_Absent](Source\Examples\Resources\CMDistributionGroup\CMDistributionGroup_Absent.ps1)
3 changes: 2 additions & 1 deletion source/ConfigMgrCBDsc.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'CMPullDistributionPoint'
'CMSiteMaintenance'
'CMAdministrativeUser'
'CMDistributionGroup'
)

<#
Expand All @@ -83,7 +84,7 @@
'SccmSqlSetup','SCCMInstall','CMIniFile','Collections','Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups',
'ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint','SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery',
'ServiceConnectionPoint','NetworkDiscovery','ReportingServicePoint','SystemDiscovery','PXEDistributionPoint','PullDistributionPoint',
'SiteMaintenance','AdministrativeUser')
'SiteMaintenance','AdministrativeUser','DistributionGroup')

# 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,337 @@
$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 DistributionGroup
Specifies the Distribution Group name.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

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

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

$groupStatus = Get-CMDistributionPointGroup -Name $DistributionGroup

if ($groupStatus)
{
$dplist = Get-CMDistributionPoint -DistributionPointGroupName $DistributionGroup
$dpMembers = @()

foreach ($dp in $dplist)
{
$dpMembers += $dp.NetworkOSPath.SubString(2)
}

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

return @{
SiteCode = $SiteCode
DistributionGroup = $DistributionGroup
DistributionPoints = $dpMembers
Ensure = $group
}
}

<#
.SYNOPSIS
This will set the desired state.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
.PARAMETER DistributionGroup
Specifies the Distribution Group name.
.PARAMETER DistributionPoints
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 Group.
.PARAMETER DistributionPointsToExclude
Specifies an array of Distribution Points to remove from the Distribution Group.
.PARAMETER Ensure
Specifies if the Distribution Group is to be present or absent.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

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

[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 -DistributionGroup $DistributionGroup

if ($Ensure -eq 'Present')
{
if (-not $PSBoundParameters.ContainsKey('DistributionPoints') -and
$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 $DistributionGroup)
New-CMDistributionPointGroup -Name $DistributionGroup
}

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 = $DistributionGroup
}

Write-Verbose -Message ($script:localizedData.AddDistro -f $add, $DistributionGroup)
Add-CMDistributionPointToGroup @addParam
}
else
{
$errorMsg += ($script:localizedData.ErrorGroup -f $add)
}
}
}

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

Write-Verbose -Message ($script:localizedData.RemoveDistro -f $remove, $DistributionGroup)
Remove-CMDistributionPointFromGroup @removeParam
}
}
}
}
elseif ($state.Ensure -eq 'Present')
{
Write-Verbose -Message ($script:localizedData.RemoveGroup -f $DistributionGroup)
Remove-CMDistributionPointGroup -Name $DistributionGroup -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 DistributionGroup
Specifies the Distribution Group name.
.PARAMETER DistributionPoints
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 Group.
.PARAMETER DistributionPointsToExclude
Specifies an array of Distribution Points to remove from the Distribution Group.
.PARAMETER Ensure
Specifies if the Distribution Group is to be present or absent.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

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

[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 -DistributionGroup $DistributionGroup
$result = $true

if ($Ensure -eq 'Present')
{
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)
{
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 $DistributionGroup)
$result = $false
}
elseif ($DistributionPoints -or $DistributionPointsToInclude -or $DistributionPointsToExclude)
{
$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
Loading

0 comments on commit 65a95c3

Please sign in to comment.