Skip to content

Commit

Permalink
Merge pull request #75 from dsccommunity/Update-CMForestDiscovery
Browse files Browse the repository at this point in the history
Update for CMForestDiscovery
  • Loading branch information
jeffotterpohl authored Dec 11, 2020
2 parents 60f7d82 + b927e2d commit fc05d30
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 269 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added DistributionPointInstallIis parameter to CmIniFile #62
- Added an example for a Standalone Primary Site Server and updated required modules
to support.
- Updated CMForestDiscovery to remove the unneeded CimInstance for schedule. Also
added warning messages when exceeding max allowed hours or days and setting the
value to the default highest available value.

### Removed

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,19 @@ you are using apply and auto correct.
- **[Boolean] Enabled** _(Required)_: Specifies the enablement of the forest
discovery method. If settings is set to $false no other value provided will be
evaluated for compliance.
- **[EmbeddedInstance] PollingSchedule** _(Write)_: Contains the polling
schedule for Configuration Manager (RecurInterval, RecurCount).
- **[Boolean] EnableActiveDirectorySiteBoundaryCreation** _(Write)_: Indicates
whether Configuration Manager creates Active Directory boundaries from AD DS
discovery information.
- **[Boolean] EnableSubnetBoundaryCreation** _(Write)_: Indicates whether
Configuration Manager creates IP address range boundaries from AD DS discovery
information.
- **[String] ScheduleInterval** _(Write)_: Specifies the time when the scheduled
event recurs in hours and days.
- Values include: { Hours | Days }
- **[String] ScheduleCount** _(Write)_: Specifies how often the recur interval
is run. If hours are specified the max value is 23. Anything over 23 will result
in 23 to be set. If days are specified the max value is 31. Anything over 31 will
result in 31 to be set.

#### CMForestDiscovery Examples

Expand Down
204 changes: 118 additions & 86 deletions source/DSCResources/DSC_CMForestDiscovery/DSC_CMForestDiscovery.psm1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
$script:dscResourceCommonPath = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\DscResource.Common'
$script:configMgrResourcehelper = Join-Path -Path $PSScriptRoot -ChildPath '..\..\Modules\ConfigMgrCBDsc.ResourceHelper'
$script:dscResourceCommonPath = Join-Path (Join-Path -Path (Split-Path -Parent -Path (Split-Path -Parent -Path $PsScriptRoot)) -ChildPath Modules) -ChildPath DscResource.Common
$script:configMgrResourcehelper = Join-Path (Join-Path -Path (Split-Path -Parent -Path (Split-Path -Parent -Path $PsScriptRoot)) -ChildPath Modules) -ChildPath 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.
Expand Down Expand Up @@ -39,27 +39,23 @@ function Get-TargetResource
Set-Location -Path "$($SiteCode):\"

$forestDiscovery = (Get-CMDiscoveryMethod -Name ActiveDirectoryForestDiscovery -SiteCode $SiteCode).Props
$enabledStatus = (($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Settings'}).Value1 -eq 'Active')
$forestSchedule = ($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Startup Schedule'}).Value1
$subnetBoundary = ($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Enable Subnet Boundary Creation'}).Value
$siteBoundary = ($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Enable AD Site Boundary Creation'}).Value

$convertCimParam = @{
ScheduleString = $forestSchedule
CimClassName = 'DSC_CMForestDiscoveryPollingSchedule'
}
$syncSchedule = ConvertTo-CimCMScheduleString @convertCimParam
$enabledStatus = (($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Settings'}).Value1 -eq 'Active')
$forestSchedule = ($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Startup Schedule'}).Value1
$subnetBoundary = ($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Enable Subnet Boundary Creation'}).Value
$siteBoundary = ($forestDiscovery | Where-Object -FilterScript {$_.PropertyName -eq 'Enable AD Site Boundary Creation'}).Value
$scheduleConvert = ConvertTo-ScheduleInterval -ScheduleString $forestSchedule

return @{
SiteCode = $SiteCode
Enabled = $enabledStatus
PollingSchedule = $syncSchedule
EnableActiveDirectorySiteBoundaryCreation = $siteBoundary
EnableSubnetBoundaryCreation = $subnetBoundary
ScheduleInterval = $scheduleConvert.Interval
ScheduleCount = $scheduleConvert.Count
}
}

<#
<#
.SYNOPSIS
This will set the desired state.
Expand All @@ -70,14 +66,19 @@ function Get-TargetResource
Specifies the enablement of the forest discovery method. If settings is set to $false no other value provided will be
evaluated for compliance.
.PARAMETER PollingSchedule
Specifies a schedule and determines how often Configuration Manager attempts to discover Active Directory forest.
.PARAMETER EnableActiveDirectorySiteBoundaryCreation
Indicates whether Configuration Manager creates Active Directory boundaries from AD DS discovery information.
.PARAMETER EnableSubnetBoundaryCreation
Indicates whether Configuration Manager creates IP address range boundaries from AD DS discovery information.
.PARAMETER ScheduleInterval
Specifies the time when the scheduled event recurs in hours and days.
.PARAMETER ScheduleCount
Specifies how often the recur interval is run. If hours are specified the max value
is 23. Anything over 23 will result in 23 to be set. If days are specified the max value
is 31. Anything over 31 will result in 31 to be set.
#>
function Set-TargetResource
{
Expand All @@ -92,17 +93,22 @@ function Set-TargetResource
[Boolean]
$Enabled,

[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$PollingSchedule,

[Parameter()]
[Boolean]
$EnableActiveDirectorySiteBoundaryCreation,

[Parameter()]
[Boolean]
$EnableSubnetBoundaryCreation
$EnableSubnetBoundaryCreation,

[Parameter()]
[ValidateSet('Hours','Days')]
[String]
$ScheduleInterval,

[Parameter()]
[UInt32]
$ScheduleCount
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Expand All @@ -113,12 +119,18 @@ function Set-TargetResource
$state = Get-TargetResource -SiteCode $SiteCode -Enabled $Enabled
if ($Enabled -eq $true)
{
if (($PSBoundParameters.ContainsKey('ScheduleInterval') -and -not $PSBoundParameters.ContainsKey('ScheduleCount')) -or
($PSBoundParameters.ContainsKey('ScheduleCount') -and -not $PSBoundParameters.ContainsKey('ScheduleInterval')))
{
throw $script:localizedData.IntervalCount
}

$includeList = @('Enabled','EnableActiveDirectorySiteBoundaryCreation','EnableSubnetBoundaryCreation')
foreach ($param in $PSBoundParameters.GetEnumerator())
{
if ($includeList -contains $param.key)
if ($includeList -contains $param.Key)
{
if ($param.Value -ne $state[$param.key])
if ($param.Value -ne $state[$param.Key])
{
Write-Verbose -Message ($script:localizedData.SettingSettings -f $param.Key, $param.Value)
$buildingParams += @{
Expand All @@ -128,38 +140,46 @@ function Set-TargetResource
}
}

if (-not [string]::IsNullOrEmpty($PollingSchedule))
if ($PSBoundParameters.ContainsKey('ScheduleInterval') -and $PSBoundParameters.ContainsKey('ScheduleCount'))
{
$newSchedule = @{
RecurInterval = $PollingSchedule.RecurInterval
RecurCount = $PollingSchedule.RecurCount
if ($ScheduleInterval -eq 'Days' -and $ScheduleCount -ge 32)
{
Write-Warning -Message ($script:localizedData.MaxIntervalDays -f $ScheduleCount)
$scheduleCheck = 31
}

$desiredPollingSchedule = New-CMSchedule @newSchedule

$currentSchedule = @{
RecurInterval = $state.PollingSchedule.RecurInterval
RecurCount = $state.PollingSchedule.RecurCount
elseif ($ScheduleInterval -eq 'Hours' -and $ScheduleCount -ge 24)
{
Write-Warning -Message ($script:localizedData.MaxIntervalHours -f $ScheduleCount)
$scheduleCheck = 23
}
else
{
$scheduleCheck = $ScheduleCount
}

$stateSchedule = New-CMSchedule @currentSchedule

$array = @('DayDuration','DaySpan','HourDuration','HourSpan','IsGMT')
if ($ScheduleInterval -ne $state.ScheduleInterval)
{
Write-Verbose -Message ($script:localizedData.SIntervalSet -f $ScheduleInterval)
$setSchedule = $true
}

foreach ($item in $array)
if ($scheduleCheck -ne $state.ScheduleCount)
{
if ($desiredPollingSchedule.$($item) -ne $stateSchedule.$($item))
{
Write-Verbose -Message ($script:localizedData.ScheduleItem `
-f $item, $($desiredPollingSchedule.$($item)), $($stateSchedule.$($item)))
$setSchedule = $true
}
Write-Verbose -Message ($script:localizedData.SCountSet -f $scheduleCheck)
$setSchedule = $true
}

if ($setSchedule)
if ($setSchedule -eq $true)
{
$pScheduleSet = @{
RecurInterval = $ScheduleInterval
RecurCount = $scheduleCheck
}

$pschedule = New-CMSchedule @pScheduleSet

$buildingParams += @{
PollingSchedule = $desiredPollingSchedule
PollingSchedule = $pSchedule
}
}
}
Expand Down Expand Up @@ -195,14 +215,19 @@ function Set-TargetResource
Specifies the enablement of the forest discovery method. If settings is set to $false no other value provided will be
evaluated for compliance.
.PARAMETER PollingSchedule
Specifies a schedule and determines how often Configuration Manager attempts to discover Active Directory forest.
.PARAMETER EnableActiveDirectorySiteBoundaryCreation
Indicates whether Configuration Manager creates Active Directory boundaries from AD DS discovery information.
.PARAMETER EnableSubnetBoundaryCreation
Indicates whether Configuration Manager creates IP address range boundaries from AD DS discovery information.
.PARAMETER ScheduleInterval
Specifies the time when the scheduled event recurs in hours and days.
.PARAMETER ScheduleCount
Specifies how often the recur interval is run. If hours are specified the max value
is 23. Anything over 23 will result in 23 to be set. If days are specified the max value
is 31. Anything over 31 will result in 31 to be set.
#>
function Test-TargetResource
{
Expand All @@ -218,17 +243,22 @@ function Test-TargetResource
[Boolean]
$Enabled,

[Parameter()]
[Microsoft.Management.Infrastructure.CimInstance]
$PollingSchedule,

[Parameter()]
[Boolean]
$EnableActiveDirectorySiteBoundaryCreation,

[Parameter()]
[Boolean]
$EnableSubnetBoundaryCreation
$EnableSubnetBoundaryCreation,

[Parameter()]
[ValidateSet('Hours','Days')]
[String]
$ScheduleInterval,

[Parameter()]
[UInt32]
$ScheduleCount
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Expand All @@ -238,45 +268,47 @@ function Test-TargetResource

if ($Enabled -eq $true)
{
$includeList = @('Enabled','EnableActiveDirectorySiteBoundaryCreation','EnableSubnetBoundaryCreation')
foreach ($param in $PSBoundParameters.GetEnumerator())
{
if ($includeList -contains $param.key)
{
if ($param.Value -ne $state[$param.key])
{
Write-Verbose -Message ($script:localizedData.TestSetting -f $param.Key, $param.Value, $state[$param.key])
$result = $false
}
}
$testParams = @{
CurrentValues = $state
DesiredValues = $PSBoundParameters
ValuesToCheck = @('Enabled','EnableActiveDirectorySiteBoundaryCreation','EnableSubnetBoundaryCreation')
}

if (-not [string]::IsNullOrEmpty($PollingSchedule))
$result = Test-DscParameterState @testParams -TurnOffTypeChecking -Verbose

if (($PSBoundParameters.ContainsKey('ScheduleInterval') -and -not $PSBoundParameters.ContainsKey('ScheduleCount')) -or
($PSBoundParameters.ContainsKey('ScheduleCount') -and -not $PSBoundParameters.ContainsKey('ScheduleInterval')))
{
Write-Warning -Message $script:localizedData.IntervalCountTest
$result = $false
}
elseif ($PSBoundParameters.ContainsKey('ScheduleInterval') -and $PSBoundParameters.ContainsKey('ScheduleCount'))
{
$newSchedule = @{
RecurInterval = $PollingSchedule.RecurInterval
RecurCount = $PollingSchedule.RecurCount
if ($ScheduleInterval -eq 'Days' -and $ScheduleCount -ge 32)
{
Write-Warning -Message ($script:localizedData.MaxIntervalDays -f $ScheduleCount)
$scheduleCheck = 31
}

$desiredPollingSchedule = New-CMSchedule @newSchedule

$currentSchedule = @{
RecurInterval = $state.PollingSchedule.RecurInterval
RecurCount = $state.PollingSchedule.RecurCount
elseif ($ScheduleInterval -eq 'Hours' -and $ScheduleCount -ge 24)
{
Write-Warning -Message ($script:localizedData.MaxIntervalHours -f $ScheduleCount)
$scheduleCheck = 23
}
else
{
$scheduleCheck = $ScheduleCount
}

$stateSchedule = New-CMSchedule @currentSchedule

$array = @('DayDuration','DaySpan','HourDuration','HourSpan','IsGMT')
if ($ScheduleInterval -ne $state.ScheduleInterval)
{
Write-Verbose -Message ($script:localizedData.SIntervalTest -f $ScheduleInterval, $State.ScheduleInterval)
$result = $false
}

foreach ($item in $array)
if ($scheduleCheck -ne $state.ScheduleCount)
{
if ($desiredPollingSchedule.$($item) -ne $stateSchedule.$($item))
{
Write-Verbose -Message ($script:localizedData.ScheduleItem `
-f $item, $($desiredPollingSchedule.$($item)), $($stateSchedule.$($item)))
$result = $false
}
Write-Verbose -Message ($script:localizedData.SCountTest -f $scheduleCheck, $State.ScheduleCount)
$result = $false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@ class DSC_CMForestDiscovery: OMI_BaseResource
{
[Key, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode;
[Required, Description("Specifies the enablement of the forest discovery method. If settings is set to $false no other value provided will be evaluated for compliance.")] Boolean Enabled;
[Write, EmbeddedInstance("DSC_CMForestDiscoveryPollingSchedule"), Description("Contains the polling schedule for Configuration Manager (RecurInterval, RecurCount)")] String PollingSchedule;
[Write, Description("Indicates whether Configuration Manager creates Active Directory boundaries from AD DS discovery information.")] Boolean EnableActiveDirectorySiteBoundaryCreation;
[Write, Description("Indicates whether Configuration Manager creates IP address range boundaries from AD DS discovery information.")] Boolean EnableSubnetBoundaryCreation;
};

[ClassVersion("1.0.0")]
class DSC_CMForestDiscoveryPollingSchedule
{
[Required, Description("Specifies the recur interval of days, or hours."),ValueMap{"Days","Hours"},Values{"Days","Hours"}] String RecurInterval;
[Required, Description("Specifies how often the recur interval is run.")] UInt32 RecurCount;
[Write, Description("Specifies the recur interval of days, hours"),ValueMap{"Days","Hours"},Values{"Days","Hours"}] String ScheduleInterval;
[Write, Description("Specifies how often the recur interval is run. If hours are specified the max value is 23. Anything over 23 will result in 23 to be set. If days are specified the max value is 31. Anything over 31 will result in 31 to be set.")] UInt32 ScheduleCount;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
ConvertFrom-StringData @'
RetrieveSettingValue = Getting results for Configuration Manager Forest Discovery.
TestState = Test-TargetResource compliance check returned: {0}.
ScheduleItem = Schedule item {0} expected {1} returned {2}.
TestSetting = Setting {0} expected {1} returned {2}.
SettingSettings = Setting {0} to {1}.
RetrieveSettingValue = Getting results for Configuration Manager Forest Discovery.
IntervalCountTest = Missing ScheduleInterval or ScheduleCount unable to evaluate schedule.
SIntervalTest = NOT MATCH: Schedule interval expected: {0} returned {1}.
SCountTest = NOT MATCH: Schedule count expected: {0} returned {1}.
TestState = Test-TargetResource compliance check returned: {0}.
IntervalCount = Invalid parameter usage must specify ScheduleInterval and ScheduleCount.
SettingSettings = Setting {0} to {1}.
SIntervalSet = Setting Schedule interval to {0}.
SCountSet = Setting Schedule count to {0}.
MaxIntervalDays = The maximum allowed interval is 31 for days. {0} was specified and will result in the interval being set to 31.
MaxIntervalHours = The maximum allowed interval is 23 for hours. {0} was specified and will result in the interval being set to 23.
'@
Loading

0 comments on commit fc05d30

Please sign in to comment.