Skip to content

Commit

Permalink
Merge branch 'ADReplicationSiteLink-Fix' of https://github.com/X-Guar…
Browse files Browse the repository at this point in the history
…dian/ActiveDirectoryDsc into ADReplicationSiteLink-Fix
  • Loading branch information
X-Guardian committed Feb 14, 2020
2 parents 5cd07aa + 93def07 commit 05e3e99
Showing 1 changed file with 90 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $script:localizedData = Get-LocalizedData -ResourceName 'MSFT_ADReplicationSiteL

<#
.SYNOPSIS
Gets the current configuration on an AD Replication Site Link.
Gets the current configuration of an AD Replication Site Link.
.PARAMETER Name
Specifies the name of the AD Replication Site Link.
Expand Down Expand Up @@ -105,7 +105,7 @@ function Get-TargetResource

<#
.SYNOPSIS
Sets the desired configuration on an AD Replication Site Link.
Sets the desired configuration of an AD Replication Site Link.
.PARAMETER Name
Specifies the name of the AD Replication Site Link.
Expand Down Expand Up @@ -186,7 +186,11 @@ function Set-TargetResource

if ($Ensure -eq 'Present')
{
# Resource should be Present
# Modify parameters for splatting to New-ADReplicationSiteLink.
$desiredParameters = $PSBoundParameters
$desiredParameters.Remove('Ensure')
$desiredParameters.Remove('SitesExcluded')

$currentADSiteLink = Get-TargetResource -Name $Name

<#
Expand All @@ -195,155 +199,105 @@ function Set-TargetResource
#>
if ( $currentADSiteLink.Ensure -eq 'Absent' )
{
# Resource is Absent

# Modify parameters for splatting to New-ADReplicationSiteLink.
$newADReplicationSiteLinkParameters = @{ } + $PSBoundParameters
$newADReplicationSiteLinkParameters.Remove('Ensure')
$newADReplicationSiteLinkParameters.Remove('SitesExcluded')
$newADReplicationSiteLinkParameters.Remove('OptionChangeNotification')
$newADReplicationSiteLinkParameters.Remove('OptionTwoWaySync')
$newADReplicationSiteLinkParameters.Remove('OptionDisableCompression')
$newADReplicationSiteLinkParameters.Remove('Verbose')

$optionsValue = ConvertTo-EnabledOptions -OptionChangeNotification $optionChangeNotification `
-OptionTwoWaySync $optionTwoWaySync -OptionDisableCompression $optionDisableCompression

if ($optionsValue -gt 0)
{
$newADReplicationSiteLinkParameters['OtherAttributes'] = @{options = $optionsValue }
}

Write-Verbose -Message ($script:localizedData.NewSiteLink -f $Name)
New-ADReplicationSiteLink @newADReplicationSiteLinkParameters
New-ADReplicationSiteLink @desiredParameters
}
else
{
# Resource is Present

$setADReplicationSiteLinkParameters = @{ }
$setADReplicationSiteLinkParameters['Identity'] = $Name
# now we have to determine if we need to add or remove sites from SitesIncluded.
$setParameters = @{
Identity = $Name
}

$replaceParameters = @{ }

# now we have to determine if we need to add or remove sites from SitesIncluded.
if (-not (Test-Members -ExistingMembers $currentADSiteLink.SitesIncluded `
-MembersToInclude $SitesIncluded -MembersToExclude $SitesExcluded))
# build the SitesIncluded hashtable.
$sitesIncludedParameters = @{ }
if ($SitesExcluded)
{
# build the SitesIncluded hashtable.
$sitesIncludedParameters = @{ }
if ($SitesExcluded)
{
Write-Verbose -Message ($script:localizedData.RemovingSites -f $($SitesExcluded -join ', '), $Name)
Write-Verbose -Message ($script:localizedData.RemovingSites -f $($SitesExcluded -join ', '), $Name)

<#
<#
Wrapped in $() as we were getting some weird results without it,
results were not being added into Hashtable as strings.
#>
$sitesIncludedParameters.Add('Remove', $($SitesExcluded))
}
$sitesIncludedParameters.Add('Remove', $($SitesExcluded))
}

if ($SitesIncluded)
{
Write-Verbose -Message ($script:localizedData.AddingSites -f $($SitesIncluded -join ', '), $Name)
if ($SitesIncluded)
{
Write-Verbose -Message ($script:localizedData.AddingSites -f $($SitesIncluded -join ', '), $Name)

<#
<#
Wrapped in $() as we were getting some weird results without it,
results were not being added into Hashtable as strings.
#>
$sitesIncludedParameters.Add('Add', $($SitesIncluded))
}

if ($null -ne $($sitesIncludedParameters.Keys))
{
$setADReplicationSiteLinkParameters['SitesIncluded'] = $sitesIncludedParameters
}
$sitesIncludedParameters.Add('Add', $($SitesIncluded))
}

if ($PSBoundParameters.ContainsKey('Cost') -and `
$Cost -ne $currentADSiteLink.Cost)
if ($null -ne $($sitesIncludedParameters.Keys))
{
Write-Verbose -Message ($script:localizedData.SettingProperty -f
'Cost', $Cost, $Name)
$setADReplicationSiteLinkParameters['Cost'] = $Cost
$setParameters.Add('SitesIncluded', $sitesIncludedParameters)
}

if ($PSBoundParameters.ContainsKey('Description') -and `
$Description -ne $currentADSiteLink.Description)
# Calculate the Options value for Change Notification replication
if ($PSBoundParameters.ContainsKey('OptionChangeNotification'))
{
Write-Verbose -Message ($script:localizedData.SettingProperty -f
'Description', $Description, $Name)
$setADReplicationSiteLinkParameters['Description'] = $Description
}

if ($PSBoundParameters.ContainsKey('ReplicationFrequencyInMinutes') -and `
$ReplicationFrequencyInMinutes -ne $currentADSiteLink.ReplicationFrequencyInMinutes)
{
Write-Verbose -Message ($script:localizedData.SettingProperty -f
'ReplicationFrequencyInMinutes', $ReplicationFrequencyInMinutes, $Name)
$setADReplicationSiteLinkParameters['ReplicationFrequencyInMinutes'] = $ReplicationFrequencyInMinutes
}

if ($PSBoundParameters.ContainsKey('OptionChangeNotification') -and `
$OptionChangeNotification -ne $currentADSiteLink.OptionChangeNotification)
{
Write-Verbose -Message ($script:localizedData.SettingProperty -f
'OptionChangeNotification', $OptionChangeNotification, $Name)
$changeNotification = $OptionChangeNotification
}
else
{
$changeNotification = $currentADSiteLink.OptionChangeNotification
}

if ($PSBoundParameters.ContainsKey('OptionTwoWaySync') -and `
$OptionTwoWaySync -ne $currentADSiteLink.OptionTwoWaySync)
if ($PSBoundParameters.ContainsKey('OptionTwoWaySync'))
{
Write-Verbose -Message ($script:localizedData.SettingProperty -f
'TwoWaySync', $TwoWaySync, $Name)
$twoWaySync = $OptionTwoWaySync
}
else
{
$twoWaySync = $currentADSiteLink.OptionTwoWaySync
}

if ($PSBoundParameters.ContainsKey('OptionDisableCompression') -and `
$OptionDisableCompression -ne $currentADSiteLink.OptionDisableCompression)
if ($PSBoundParameters.ContainsKey('OptionDisableCompression'))
{
Write-Verbose -Message ($script:localizedData.SettingProperty -f
'OptionDisableCompression', $OptionDisableCompression, $Name)
$disableCompression = $OptionDisableCompression
}
else
{
$disableCompression = $currentADSiteLink.OptionDisableCompression
}

$optionsValue = ConvertTo-EnabledOptions -OptionChangeNotification $changeNotification `
-OptionTwoWaySync $twoWaySync -OptionDisableCompression $disableCompression
$optionsValue = ConvertTo-EnabledOptions -OptionChangeNotification $changeNotification -OptionTwoWaySync $twoWaySync -OptionDisableCompression $disableCompression

if ($optionsValue -gt 0)
if ($optionsValue -eq 0)
{
$setADReplicationSiteLinkParameters.Add('Clear', 'Options')
$setParameters.Add('Clear', 'Options')
}
else
{
$replaceParameters.Add('Options', $optionsValue)
}

# Add the rest of the parameters.
foreach ($parameter in $PSBoundParameters.Keys)
{
if ($parameter -notmatch 'SitesIncluded|SitesExcluded|Name|Ensure|OptionChangeNotification|OptionTwoWaySync|OptionDisableCompression')
{
$setParameters.Add($parameter, $PSBoundParameters[$parameter])
}
}

if ($replaceParameters.Count -gt 0)
{
$setADReplicationSiteLinkParameters.Add('Replace', $replaceParameters)
$setParameters.Add('Replace', $replaceParameters)
}

Set-ADReplicationSiteLink @setADReplicationSiteLinkParameters
Set-ADReplicationSiteLink @setParameters
}
}
else
{
# Resource should be absent

Write-Verbose -Message ($script:localizedData.RemoveSiteLink -f $Name)

Remove-ADReplicationSiteLink -Identity $Name
Expand Down Expand Up @@ -600,4 +554,50 @@ function ConvertTo-EnabledOptions
return $returnValue
}

Export-ModuleMember -Function *-TargetResource
.PARAMETER OptionTwoWaySync
Enable/Disable Two Way sync

.PARAMETER OptionDisableCompression
Enable/Disable Compression
#>
function ConvertTo-EnabledOptions
{
[OutputType([System.Int32])]
[CmdletBinding()]
param
(
[Parameter()]
[System.Boolean]
$OptionChangeNotification,

[Parameter()]
[System.Boolean]
$OptionTwoWaySync,

[Parameter()]
[System.Boolean]
$OptionDisableCompression
)

$returnValue = 0

if ($OptionChangeNotification)
{
$returnValue = $returnValue + 1
}

if ($OptionTwoWaySync)
{
$returnValue = $returnValue + 2
}

if ($OptionDisableCompression)
{
$returnValue = $returnValue + 4
}

return $returnValue
}

Export-ModuleMember -Function *-TargetResource

0 comments on commit 05e3e99

Please sign in to comment.