-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from dsccommunity/DistributionGroups
Custom module to add Distribution Point groups
- Loading branch information
Showing
9 changed files
with
850 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
337 changes: 337 additions & 0 deletions
337
source/DSCResources/DSC_CMDistributionGroup/DSC_CMDistributionGroup.psm1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.