diff --git a/CHANGELOG.md b/CHANGELOG.md index 94a7c47..0d4d15a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/source/DSCResources/DSC_CMUserDiscovery/DSC_CMUserDiscovery.psm1 b/source/DSCResources/DSC_CMUserDiscovery/DSC_CMUserDiscovery.psm1 index c464324..e9c0470 100644 --- a/source/DSCResources/DSC_CMUserDiscovery/DSC_CMUserDiscovery.psm1 +++ b/source/DSCResources/DSC_CMUserDiscovery/DSC_CMUserDiscovery.psm1 @@ -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 } @@ -246,7 +274,7 @@ function Set-TargetResource { $pScheduleSet = @{ RecurInterval = $ScheduleInterval - RecurCount = $ScheduleCount + RecurCount = $ScheduleCheck } $pschedule = New-CMSchedule @pScheduleSet @@ -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. @@ -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 } } diff --git a/source/DSCResources/DSC_CMUserDiscovery/en-US/DSC_CMUserDiscovery.strings.psd1 b/source/DSCResources/DSC_CMUserDiscovery/en-US/DSC_CMUserDiscovery.strings.psd1 index d000ce3..b67538e 100644 --- a/source/DSCResources/DSC_CMUserDiscovery/en-US/DSC_CMUserDiscovery.strings.psd1 +++ b/source/DSCResources/DSC_CMUserDiscovery/en-US/DSC_CMUserDiscovery.strings.psd1 @@ -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. '@ diff --git a/tests/Unit/CMUserDiscovery.tests.ps1 b/tests/Unit/CMUserDiscovery.tests.ps1 index 4d98c73..989b365 100644 --- a/tests/Unit/CMUserDiscovery.tests.ps1 +++ b/tests/Unit/CMUserDiscovery.tests.ps1 @@ -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 @@ -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 = @{ @@ -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 @@ -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 @@ -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 @@ -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 }