Skip to content

Commit

Permalink
Merge pull request #34 from dsccommunity/CMHeartbeatDiscovery
Browse files Browse the repository at this point in the history
Initial CMHeartbeat Discovery module creation
  • Loading branch information
NEllis280 authored Jun 15, 2020
2 parents 21bb923 + 7497556 commit dc7d108
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added CMFallbackStatusPoint Resource
- Added CMSoftwareUpdatePoint Resource
- Added CMDsitributionPoint Resource
- Added CMHeartbeatDiscovery module
- Added ConvertTo-ScheduleInterval to ResourceHelper

### Changed

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu
the SCCM Software Update Point role.
- **CMDistributionPoint**: Provides a resource for creating and managing
the distribution point role.
- **CMHeartbeatDiscovery**: Provides a resource to manage the Configuration Manager
Heartbeat Discovery method.

### CMAccounts

Expand Down Expand Up @@ -472,3 +474,23 @@ Please check out common DSC Community [contributing guidelines](https://dsccommu

- [CMDistributionPoint_Absent](Source\Examples\Resources\CMDistributionPoint\CMDistributionPoint_Absent.ps1)
- [CMDistributionPoint_Present](Source\Examples\Resources\CMDistributionPoint\CMDistributionPoint_Present.ps1)

### CMHeartbeatDiscovery

- **[String] SiteCode** _(Key)_: Specifies the Site Code for the Configuration
Manager site.
- **[Boolean] Enabled** _(Required)_: Specifies the enablement of the heartbeat
discovery method. If settings is set to $false no other value provided will be
evaluated for compliance.
- **[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.

#### CMHeartbeatDiscovery Examples

- [CMHeartbeatDiscovery_Disabled](Source\Examples\Resources\CMHeartbeatDiscovery\CMHeartbeatDiscovery_Disabled.ps1)
- [CMHeartbeatDiscovery_Enabled](Source\Examples\Resources\CMHeartbeatDiscovery\CMHeartbeatDiscovery_Enabled.ps1)
3 changes: 2 additions & 1 deletion source/ConfigMgrCBDsc.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'CMFallbackStatusPoint'
'CMSoftwareUpdatePoint'
'CMDistributionPoint'
'CMHeartbeatDiscovery'
)

<#
Expand All @@ -68,7 +69,7 @@
# Tags applied to this module. These help with module discovery in online galleries.
Tags = @('DesiredStateConfiguration', 'DSC', 'DSCResourceKit', 'DSCResource', 'ConfigMgrCBDsc','CMAccounts','CMIniFile','Collections',
'Boundaries','ForestDiscovery','ClientStatusSettings','BoundaryGroups','ManagementPoint','AssetIntelligencePoint','FallbackStatusPoint',
'SoftwareUpdatePoint','DistrubtionPoint')
'SoftwareUpdatePoint','DistrubtionPoint','HeartbeatDiscovery')

# A URL to the license for this module.
LicenseUri = 'https://github.com/dsccommunity/ConfigMgrCBDsc/blob/master/LICENSE'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
$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 Enabled
Specifies the enablement of the Heatbeat discovery method.
Not used in Get-TargetResource.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[Boolean]
$Enabled
)

Write-Verbose -Message $script:localizedData.RetrieveSettingValue
Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"

$heartbeat = (Get-CMDiscoveryMethod -Name HeartbeatDiscovery -SiteCode $SiteCode).Props
$state = ($heartbeat | Where-Object -FilterScript {$_.PropertyName -eq 'Enable Heartbeat DDR'}).Value
$heartbeatSchedule = ($heartbeat | Where-Object -FilterScript {$_.PropertyName -eq 'DDR Refresh Interval'}).Value2

$scheduleConvert = ConvertTo-ScheduleInterval -ScheduleString $heartbeatSchedule

return @{
SiteCode = $SiteCode
Enabled = $state
ScheduleInterval = $scheduleConvert.Interval
ScheduleCount = $scheduleConvert.Count
}
}

<#
.SYNOPSIS
This will set the desired state.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
.PARAMETER Enabled
Specifies the enablement of the Heatbeat discovery method.
.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
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[Boolean]
$Enabled,

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

[Parameter()]
[UInt32]
$ScheduleCount
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"

try
{
$state = Get-TargetResource -SiteCode $SiteCode -Enabled $Enabled

if ($Enabled -ne $state.Enabled)
{
Write-Verbose -Message ($script:localizedData.SettingEnable -f $state.Enabled, $Enabled)
$buildingParams = @{
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
}

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

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

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

$pschedule = New-CMSchedule @pScheduleSet

$buildingParams += @{
PollingSchedule = $pSchedule
}
}
}
}

if ($buildingParams)
{
Set-CMDiscoveryMethod -Heartbeat -SiteCode $SiteCode @buildingParams
}
}
catch
{
throw $_
}
finally
{
Set-Location -Path "$env:temp"
}
}

<#
.SYNOPSIS
This will test the desired state.
.PARAMETER SiteCode
Specifies the site code for Configuration Manager site.
.PARAMETER Enabled
Specifies the enablement of the Heatbeat discovery method.
.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
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[String]
$SiteCode,

[Parameter(Mandatory = $true)]
[Boolean]
$Enabled,

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

[Parameter()]
[UInt32]
$ScheduleCount
)

Import-ConfigMgrPowerShellModule -SiteCode $SiteCode
Set-Location -Path "$($SiteCode):\"
$state = Get-TargetResource -SiteCode $SiteCode -Enabled $Enabled
$result = $true

if ($Enabled -ne $state.Enabled)
{
Write-Verbose -Message ($script:localizedData.EnableStatus -f $Enabled, $state.Enabled)
$result = $false
}

if (($PSBoundParameters.ContainsKey('ScheduleInterval') -and -not $PSBoundParameters.ContainsKey('ScheduleCount')) -or
($PSBoundParameters.ContainsKey('ScheduleCount') -and -not $PSBoundParameters.ContainsKey('ScheduleInterval')))
{
Write-Verbose -Message $script:localizedData.IntervalCountTest
$result = $false
}

if ($PSBoundParameters.ContainsKey('ScheduleInterval') -and $PSBoundParameters.ContainsKey('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))
{
Write-Verbose -Message ($script:localizedData.SCountTest -f $ScheduleCount, $State.ScheduleCount)
$result = $false
}
}

Write-Verbose -Message ($script:localizedData.TestState -f $result)
return $result
}

Export-ModuleMember -Function *-TargetResource
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ClassVersion("1.0.0"), FriendlyName("CMHeartbeatDiscovery")]
class DSC_CMHeartbeatDiscovery : OMI_BaseResource
{
[Key, Description("Specifies the SiteCode for the Configuration Manager site.")] String SiteCode;
[Required, Description("Specifies the enablement of the heartbeat discovery method.")] Boolean Enabled;
[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
@@ -0,0 +1,12 @@
ConvertFrom-StringData @'
RetrieveSettingValue = Getting results for Configuration Manager heartbeat discovery method.
EnableStatus = Heartbeat discovery is set to {0} return {1}.
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}.
SettingEnable = Heartbeat discovery is currently {0}, setting to {1}.
IntervalCount = Invalid parameter usage must specify ScheduleInterval and ScheduleCount.
SIntervalSet = Setting Schedule interval to {0}.
SCountSet = Setting Schedule count to {0}.
'@
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<#
.SYNOPSIS
A DSC configuration script to disable heartbeat discovery in Configuration Manager.
#>
Configuration Example
{
Import-DscResource -ModuleName ConfigMgrCBDsc

Node localhost
{
CMHeartbeatDiscovery ExampleSettings
{
SiteCode = 'Lab'
Enabled = $false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<#
.SYNOPSIS
A DSC configuration script to enable heartbeat discovery in Configuration Manager.
#>
Configuration Example
{
Import-DscResource -ModuleName ConfigMgrCBDsc

Node localhost
{
CMHeartbeatDiscovery ExampleSettings
{
SiteCode = 'Lab'
Enabled = $true
ScheduleInterval = 'Days'
ScheduleCount = '4'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'ConvertTo-CimBoundaries'
'Convert-BoundariesIPSubnets'
'Get-BoundaryInfo'
'ConvertTo-ScheduleInterval'
)

# Cmdlets to export from this module
Expand Down
Loading

0 comments on commit dc7d108

Please sign in to comment.