Skip to content

Commit

Permalink
Merge pull request #76 from dsccommunity/CMUserDiscoveryParameterHand…
Browse files Browse the repository at this point in the history
…ling

CMUserDiscovery - Updated Schedule parameter handling
  • Loading branch information
NEllis280 authored Dec 18, 2020
2 parents 9a9e1e9 + 8da7c04 commit 19bed44
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
added warning messages when exceeding max allowed hours or days and setting the
value to the default highest available value.
- 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.

### Removed

Expand Down
72 changes: 64 additions & 8 deletions source/DSCResources/DSC_CMUserDiscovery/DSC_CMUserDiscovery.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,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 ($ScheduleInterval -eq 'Minutes' -and $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.UIntervalSet -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.UCountSet -f $ScheduleCount)
Write-Verbose -Message ($script:localizedData.UCountSet -f $ScheduleCheck)
$setSchedule = $true
}

Expand All @@ -246,7 +274,7 @@ function Set-TargetResource
{
$pScheduleSet = @{
RecurInterval = $ScheduleInterval
RecurCount = $ScheduleCount
RecurCount = $ScheduleCheck
}

$pschedule = New-CMSchedule @pScheduleSet
Expand Down Expand Up @@ -352,7 +380,7 @@ function Set-TargetResource

<#
.SYNOPSIS
This will set the desired state.
This will test the desired state.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
Expand Down Expand Up @@ -447,20 +475,48 @@ function Test-TargetResource
{
if ($ScheduleInterval -ne 'None' -and -not $PSBoundParameters.ContainsKey('ScheduleCount'))
{
Write-Warning -Message $script:localizedData.IntervalCount
Write-Verbose -Message $script:localizedData.IntervalCount
$result = $false
}
else
{
if ($ScheduleInterval -ne $state.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 ($ScheduleInterval -eq 'Minutes' -and $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.UIntervalTest -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.UCountTest -f $ScheduleCount, $State.ScheduleCount)
Write-Verbose -Message ($script:localizedData.UCountTest -f $scheduleCheck, $State.ScheduleCount)
$result = $false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ ConvertFrom-StringData @'
SetDisabled = Setting User Discovery to disabled.
ContainersInEx = ADContainersToExclude and ADContainersToInclude contain to same entry {0}, remove from one of the arrays.
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.
'@
154 changes: 153 additions & 1 deletion tests/Unit/CMUserDiscovery.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,34 @@ try
Enabled = $false
}

$returnEnabledDaysMax = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Days'
ScheduleCount = 32
}

$returnEnabledHoursMax = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Hours'
ScheduleCount = 25
}

$returnEnabledMinsMax = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Minutes'
ScheduleCount = 60
}

$returnEnabledMinsMin = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Minutes'
ScheduleCount = 4
}

$cmScheduleNull = @{
DayDuration = 0
HourDuration = 0
Expand Down Expand Up @@ -288,6 +316,42 @@ try
MinuteSpan = 0
}

$scheduleConvertDaysMax = @{
DayDuration = 0
DaySpan = 31
HourDuration = 0
HourSpan = 0
MinuteDuration = 0
MinuteSpan = 0
}

$scheduleConvertHoursMax = @{
DayDuration = 0
DaySpan = 0
HourDuration = 0
HourSpan = 23
MinuteDuration = 0
MinuteSpan = 0
}

$scheduleConvertMinutesMax = @{
DayDuration = 0
DaySpan = 0
HourDuration = 0
HourSpan = 0
MinuteDuration = 0
MinuteSpan = 59
}

$scheduleConvertMinutesMin = @{
DayDuration = 0
DaySpan = 0
HourDuration = 0
HourSpan = 0
MinuteDuration = 0
MinuteSpan = 5
}

$adContainersMismatch = 'LDAP://OU=Test2,DC=contoso,DC=com'

$inputParamsADContainersInclude = @{
Expand Down Expand Up @@ -341,7 +405,7 @@ try
}

It 'Should call expected commands schedule mismatch' {
Mock -CommandName New-CMSchedule -MockWith { $cmScheduleHours } -ParameterFilter { $RecurInterval -eq 'Hours' }
Mock -CommandName New-CMSchedule -MockWith { $cmScheduleHours }

Set-TargetResource @inputParamsHours
Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It
Expand All @@ -351,6 +415,50 @@ try
Assert-MockCalled Set-CMDiscoveryMethod -Exactly -Times 1 -Scope It
}

It 'Should call expected commands schedule max days' {
Mock -CommandName New-CMSchedule -MockWith { $scheduleConvertDaysMax }

Set-TargetResource @returnEnabledDaysMax
Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It
Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled New-CMSchedule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-CMDiscoveryMethod -Exactly -Times 1 -Scope It
}

It 'Should call expected commands schedule max hours' {
Mock -CommandName New-CMSchedule -MockWith { $scheduleConvertHoursMax }

Set-TargetResource @returnEnabledHoursMax
Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It
Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled New-CMSchedule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-CMDiscoveryMethod -Exactly -Times 1 -Scope It
}

It 'Should call expected commands schedule max minutes' {
Mock -CommandName New-CMSchedule -MockWith { $scheduleConvertMinutesMax }

Set-TargetResource @returnEnabledMinsMax
Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It
Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled New-CMSchedule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-CMDiscoveryMethod -Exactly -Times 1 -Scope It
}

It 'Should call expected commands schedule min minutes' {
Mock -CommandName New-CMSchedule -MockWith { $scheduleConvertMinutesMin }

Set-TargetResource @returnEnabledMinsMin
Assert-MockCalled Import-ConfigMgrPowerShellModule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-Location -Exactly -Times 2 -Scope It
Assert-MockCalled Get-TargetResource -Exactly -Times 1 -Scope It
Assert-MockCalled New-CMSchedule -Exactly -Times 1 -Scope It
Assert-MockCalled Set-CMDiscoveryMethod -Exactly -Times 1 -Scope It
}

It 'Should call expected commands for mismatch AD container' {
Mock -CommandName New-CMSchedule

Expand Down Expand Up @@ -607,6 +715,34 @@ try
ScheduleCount = 7
}

$returnEnabledDaysMax = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Days'
ScheduleCount = 32
}

$returnEnabledHoursMax = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Hours'
ScheduleCount = 25
}

$returnEnabledMinsMax = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Minutes'
ScheduleCount = 60
}

$returnEnabledMinsMin = @{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Minutes'
ScheduleCount = 4
}

$inputParamsDeltaMismatch = @{
SiteCode = 'Lab'
Enabled = $true
Expand Down Expand Up @@ -661,6 +797,22 @@ try
Test-TargetResource @inputParamsDeltaMismatch | Should -Be $false
}

It 'Should return desired result false schedule days exceed max' {
Test-TargetResource @returnEnabledDaysMax | Should -Be $false
}

It 'Should return desired result false schedule hours exceed max' {
Test-TargetResource @returnEnabledHoursMax | Should -Be $false
}

It 'Should return desired result false schedule mins exceed max' {
Test-TargetResource @returnEnabledMinsMax | Should -Be $false
}

It 'Should return desired result false schedule mins under minimum' {
Test-TargetResource @returnEnabledMinsMin | Should -Be $false
}

It 'Should return desired result false when User Discovery schedules do not match' {
Test-TargetResource @inputParamsHours | Should -Be $false
}
Expand Down

0 comments on commit 19bed44

Please sign in to comment.