Skip to content

Commit

Permalink
Merge pull request #80 from dsccommunity/CMSystemDiscoveryParameterVa…
Browse files Browse the repository at this point in the history
…lidation

CMSystemDiscovery - Adding Parameter Validation
  • Loading branch information
NEllis280 authored Feb 22, 2021
2 parents ce13dd6 + 53f75af commit 7099f8c
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 123 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated CMHeartbeatDiscovery to warn when exceeding max allowed hours or days.
- Updated CMUserDiscovery to warn when exceeding max allowed hours or days or minutes
and when specifying under the minimum allowed minutes.
- Updated CMSystemDiscovery to warn when exceeding max allowed hours or days or minutes
and when specifying under the minimum allowed minutes.

### Removed

Expand Down
224 changes: 107 additions & 117 deletions source/DSCResources/DSC_CMSystemDiscovery/DSC_CMSystemDiscovery.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function Set-TargetResource
$EnableDeltaDiscovery,

[Parameter()]
[ValidateRange(1,60)]
[ValidateRange(5,60)]
[UInt32]
$DeltaDiscoveryMins,

Expand All @@ -174,6 +174,7 @@ function Set-TargetResource
$EnableFilteringExpiredLogon,

[Parameter()]
[ValidateRange(14,720)]
[UInt32]
$TimeSinceLastLogonDays,

Expand All @@ -182,6 +183,7 @@ function Set-TargetResource
$EnableFilteringExpiredPassword,

[Parameter()]
[ValidateRange(30,720)]
[UInt32]
$TimeSinceLastPasswordUpdateDays,

Expand Down Expand Up @@ -265,15 +267,43 @@ function Set-TargetResource

if (-not [string]::IsNullOrEmpty($ScheduleInterval))
{
if ($ScheduleInterval -eq 'Days' -and $ScheduleCount -ge 32)
{
Write-Warning -Message ($script:localizedData.MaxIntervalDays -f $ScheduleCount)
$scheduleCheck = 31
}
elseif ($ScheduleInterval -eq 'Hours' -and $ScheduleCount -ge 24)
{
Write-Warning -Message ($script:localizedData.MaxIntervalHours -f $ScheduleCount)
$scheduleCheck = 23
}
elseif (($ScheduleInterval -eq 'Minutes') -and ($ScheduleCount -ge 60 -or $ScheduleCount -le 4))
{
if ($ScheduleCount -ge 60)
{
Write-Warning -Message ($script:localizedData.MaxIntervalMins -f $ScheduleCount)
$scheduleCheck = 59
}
else
{
Write-Warning -Message ($script:localizedData.MinIntervalMins -f $ScheduleCount)
$scheduleCheck = 5
}
}
else
{
$scheduleCheck = $ScheduleCount
}

if ($ScheduleInterval -ne $state.ScheduleInterval)
{
Write-Verbose -Message ($script:localizedData.SIntervalSet -f $ScheduleInterval)
$setSchedule = $true
}

if (($ScheduleInterval -ne 'None') -and ($ScheduleCount -ne $state.ScheduleCount))
if (($ScheduleInterval -ne 'None') -and ($scheduleCheck -ne $state.ScheduleCount))
{
Write-Verbose -Message ($script:localizedData.SCountSet -f $ScheduleCount)
Write-Verbose -Message ($script:localizedData.SCountSet -f $scheduleCheck)
$setSchedule = $true
}

Expand All @@ -287,7 +317,7 @@ function Set-TargetResource
{
$pScheduleSet = @{
RecurInterval = $ScheduleInterval
RecurCount = $ScheduleCount
RecurCount = $scheduleCheck
}

$pschedule = New-CMSchedule @pScheduleSet
Expand All @@ -299,73 +329,30 @@ function Set-TargetResource
}
}

if (($ADContainers) -or ($ADContainersToInclude))
if ($ADContainers -or $ADContainersToInclude -or $ADContainersToExclude)
{
if ($ADContainers)
{
$includes = $ADContainers
}
else
{
$includes = $ADContainersToInclude
$containersArray = @{
Match = $ADContainers
Include = $ADContainersToInclude
Exclude = $ADContainersToExclude
CurrentState = $state.ADContainers
}

foreach ($adContainer in $includes)
{
if ($state.ADContainers -notcontains $adContainer)
{
Write-Verbose -Message ($script:localizedData.AddADContainer -f $adContainer)
[array]$addAdContainers += $adContainer
}
}
$containersCompare = Compare-MultipleCompares @containersArray

if (-not [string]::IsNullOrEmpty($addAdContainers))
if ($containersCompare.Missing)
{
Write-Verbose -Message ($script:localizedData.ADContainerMissing -f ($containersCompare.Missing | Out-String))
$buildingParams += @{
AddActiveDirectoryContainer = $addAdContainers
AddActiveDirectoryContainer = $containersCompare.Missing
}
}
}

if (($ADContainers) -or ($ADContainersToExclude))
{
if (-not [string]::IsNullOrEmpty($state.ADContainers))
if ($containersCompare.Remove)
{
if ($ADContainers)
{
$excludes = $ADContainers
}
else
{
$excludes = $ADContainersToExclude
}

$compares = Compare-Object -ReferenceObject $state.ADContainers -DifferenceObject $excludes -IncludeEqual
foreach ($compare in $compares)
{
if ($ADContainers)
{
if ($compare.SideIndicator -eq '<=')
{
Write-Verbose -Message ($script:localizedData.RemoveADContainer -f $compare.InputObject)
[array]$removeADContainers += $compare.InputObject
}
}
else
{
if ($compare.SideIndicator -eq '==')
{
Write-Verbose -Message ($script:localizedData.RemoveADContainer -f $compare.InputObject)
[array]$removeADContainers += $compare.InputObject
}
}
}

if (-not [string]::IsNullOrEmpty($removeADContainers))
{
$buildingParams += @{
RemoveActiveDirectoryContainer = $removeADContainers
}
Write-Verbose -Message ($script:localizedData.ADContainerExtra -f ($containersCompare.Remove | Out-String))
$buildingParams += @{
RemoveActiveDirectoryContainer = $containersCompare.Remove
}
}
}
Expand Down Expand Up @@ -462,7 +449,7 @@ function Test-TargetResource
$EnableDeltaDiscovery,

[Parameter()]
[ValidateRange(1,60)]
[ValidateRange(5,60)]
[UInt32]
$DeltaDiscoveryMins,

Expand All @@ -471,6 +458,7 @@ function Test-TargetResource
$EnableFilteringExpiredLogon,

[Parameter()]
[ValidateRange(14,720)]
[UInt32]
$TimeSinceLastLogonDays,

Expand All @@ -479,6 +467,7 @@ function Test-TargetResource
$EnableFilteringExpiredPassword,

[Parameter()]
[ValidateRange(30,720)]
[UInt32]
$TimeSinceLastPasswordUpdateDays,

Expand Down Expand Up @@ -529,15 +518,43 @@ function Test-TargetResource
}
else
{
if ($ScheduleInterval -eq 'Days' -and $ScheduleCount -ge 32)
{
Write-Warning -Message ($script:localizedData.MaxIntervalDays -f $ScheduleCount)
$scheduleCheck = 31
}
elseif ($ScheduleInterval -eq 'Hours' -and $ScheduleCount -ge 24)
{
Write-Warning -Message ($script:localizedData.MaxIntervalHours -f $ScheduleCount)
$scheduleCheck = 23
}
elseif (($ScheduleInterval -eq 'Minutes') -and ($ScheduleCount -ge 60 -or $ScheduleCount -le 4))
{
if ($ScheduleCount -ge 60)
{
Write-Warning -Message ($script:localizedData.MaxIntervalMins -f $ScheduleCount)
$scheduleCheck = 59
}
else
{
Write-Warning -Message ($script:localizedData.MinIntervalMins -f $ScheduleCount)
$scheduleCheck = 5
}
}
else
{
$scheduleCheck = $ScheduleCount
}

if ($ScheduleInterval -ne $state.SCheduleInterval)
{
Write-Verbose -Message ($script:localizedData.SIntervalTest -f $ScheduleInterval, $State.ScheduleInterval)
$result = $false
}

if (($ScheduleInterval -ne 'None') -and ($ScheduleCount -ne $state.ScheduleCount))
if (($ScheduleInterval -ne 'None') -and ($scheduleCheck -ne $state.ScheduleCount))
{
Write-Verbose -Message ($script:localizedData.SCountTest -f $ScheduleCount, $State.ScheduleCount)
Write-Verbose -Message ($script:localizedData.SCountTest -f $scheduleCheck, $State.ScheduleCount)
$result = $false
}
}
Expand All @@ -549,76 +566,49 @@ function Test-TargetResource
Write-Warning -Message $script:localizedData.DeltaNoInterval
}

if ($ADContainersToInclude -and $ADContainersToExclude)
if ($PSBoundParameters.ContainsKey('ADContainers'))
{
if ($PSBoundParameters.ContainsKey('ADContainersToInclude') -or
$PSBoundParameters.ContainsKey('ADContainersToExclude'))
{
Write-Warning -Message $script:localizedData.ADIgnore
}
}
elseif (-not $PSBoundParameters.ContainsKey('ADContainers') -and
$PSBoundParameters.ContainsKey('ADContainersToInclude') -and
$PSBoundParameters.ContainsKey('ADContainersToExclude'))
{
foreach ($item in $ADContainersToInclude)
{
if ($ADContainersToExclude -contains $item)
{
Write-Warning -Message ($script:localizedData.ContainersInEx -f $item)
$result = $false
}
}
}

if (($ADContainers) -or ($ADContainersToInclude))
if ($ADContainers -or $ADContainersToInclude -or $ADContainersToExclude)
{
if ($ADContainers)
{
if ($PSBoundParameters.ContainsKey('ADContainersToInclude') -or
$PSBoundParameters.ContainsKey('ADContainersToExclude'))
{
Write-Warning -Message $script:localizedData.ADIgnore
}
$includes = $ADContainers
}
else
{
$includes = $ADContainersToInclude
$containersArray = @{
Match = $ADContainers
Include = $ADContainersToInclude
Exclude = $ADContainersToExclude
CurrentState = $state.ADContainers
}

foreach ($adContainer in $includes)
$containersCompare = Compare-MultipleCompares @containersArray

if ($containersCompare.Missing)
{
if ($state.ADContainers -notcontains $adContainer)
{
Write-Verbose -Message ($script:localizedData.ExpectedADContainer -f $adContainer)
$result = $false
}
Write-Verbose -Message ($script:localizedData.ADContainerMissing -f ($containersCompare.Missing | Out-String))
$result = $false
}
}

if (($ADContainers) -or ($ADContainersToExclude))
{
if (-not [string]::IsNullOrEmpty($state.ADContainers))
if ($containersCompare.Remove)
{
if ($ADContainers)
{
$excludes = $ADContainers
}
else
{
$excludes = $ADContainersToExclude
}

$compares = Compare-Object -ReferenceObject $state.ADContainers -DifferenceObject $excludes -IncludeEqual
foreach ($compare in $compares)
{
if ($ADContainers)
{
if ($compare.SideIndicator -eq '<=')
{
Write-Verbose -Message ($script:localizedData.ExcludeADContainer -f $compare.InputObject)
$result = $false
}
}
else
{
if ($compare.SideIndicator -eq '==')
{
Write-Verbose -Message ($script:localizedData.ExcludeADContainer -f $compare.InputObject)
$result = $false
}
}
}
Write-Verbose -Message ($script:localizedData.ADContainerExtra -f ($containersCompare.Remove | Out-String))
$result = $false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
ConvertFrom-StringData @'
RetrieveSettingValue = Getting results for Configuration Manager system discovery method.
ExpectedADContainer = Expected AD Container: {0} to be present.
ExcludeADContainer = Expected AD Container: {0} to be absent.
IntervalCount = Invalid parameter usage specifying an Interval and didn't specify count.
SIntervalTest = NOT MATCH: Schedule interval expected: {0} returned {1}.
SCountTest = NOT MATCH: Schedule count expected: {0} returned {1}.
Expand All @@ -10,11 +8,15 @@ ConvertFrom-StringData @'
SetCommonSettings = Setting {0} to desired setting {1}.
SIntervalSet = Setting Schedule interval to {0}.
SCountSet = Setting Schedule count to {0}.
AddADContainer = Adding AD Container: {0}.
RemoveADContainer = Removing AD Container: {0}.
SetDisabled = Setting System Discovery to disabled.
MissingDeltaDiscovery = When changing delta schedule, delta schedule must be enabled.
ADIgnore = ADContainers was specified, ADContainersInclude and ADContainersExclude will be ignored.
ContainersInEx = ADContainersToExclude and ADContainersToInclude contain to same entry {0}, remove from one of the arrays.
ADContainerMissing = AD Container {0} is missing from configuration manager, adding.
ADContainerExtra = AD Container {0} is expected to absent, removing AD Container.
DeltaNoInterval = DeltaDiscoveryMins is not specified, specify DeltaDiscoveryMins when enabling Delta Discovery.
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.
MaxIntervalMins = The value specified for minutes must be between 5 and 59. {0} was specified and will result in the interval being set to 59.
MinIntervalMins = The value specified for minutes must be between 5 and 59. {0} was specified and will result in the interval being set to 5.
'@
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function Set-TargetResource
$EnableDeltaDiscovery,

[Parameter()]
[ValidateRange(1,60)]
[ValidateRange(5,60)]
[UInt32]
$DeltaDiscoveryMins,

Expand Down Expand Up @@ -430,7 +430,7 @@ function Test-TargetResource
$EnableDeltaDiscovery,

[Parameter()]
[ValidateRange(1,60)]
[ValidateRange(5,60)]
[UInt32]
$DeltaDiscoveryMins,

Expand Down
Loading

0 comments on commit 7099f8c

Please sign in to comment.