DSCResources/MSFT_xDFSReplicationGroup/MSFT_xDFSReplicationGroup.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'

# Import the Certificate Resource Helper Module
Import-Module -Name (Join-Path -Path $modulePath `
                               -ChildPath (Join-Path -Path 'DFSDsc.ResourceHelper' `
                                                     -ChildPath 'DFSDsc.ResourceHelper.psm1'))

# Import Localization Strings
$localizedData = Get-LocalizedData `
    -ResourceName 'MSFT_xDFSReplicationGroup' `
    -ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)

<#
    .SYNOPSIS
    Returns the current state of a DFS Replication Group.
 
    .PARAMETER GroupName
    The name of the DFS Replication Group.
 
    .PARAMETER Ensure
    Specifies whether the DSF Replication Group should exist.
 
    .PARAMETER DomainName
    The AD domain the Replication Group should created in.
#>

function Get-TargetResource
{
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $GroupName,

        [Parameter(Mandatory = $true)]
        [ValidateSet('Present','Absent')]
        [System.String]
        $Ensure,

        [Parameter()]
        [System.String]
        $DomainName
    )

    Write-Verbose -Message ( @(
        "$($MyInvocation.MyCommand): "
        $($LocalizedData.GettingReplicationGroupMessage) `
            -f $GroupName
        ) -join '' )

    # Lookup the existing Replication Group
    $replicationGroupParameters = @{
        GroupName = $GroupName
    }

    $returnValue = $replicationGroupParameters.Clone()

    if ($DomainName)
    {
        $replicationGroupParameters += @{
            DomainName = $DomainName
        }
    } # if

    $replicationGroup = Get-DfsReplicationGroup @replicationGroupParameters `
        -ErrorAction Stop

    if ($replicationGroup)
    {
        Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.ReplicationGroupExistsMessage) `
                -f $GroupName
            ) -join '' )

        <#
            Array paramters are disabled until this issue is resolved:
            https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11088807-get-dscconfiguration-fails-with-embedded-cim-type
            When resolved these parameters can be added into return value
            Members = @((Get-DfsrMember @replicationGroupParameters -ErrorAction Stop).ComputerName)
            Folders = @((Get-DfsReplicatedFolder @replicationGroupParameters -ErrorAction Stop).FolderName)
        #>

        $returnValue += @{
            Ensure = 'Present'
            Description = $replicationGroup.Description
            DomainName = $replicationGroup.DomainName
            Members = @()
            Folders = @()
            ContentPaths = @()
        }
    }
    else
    {
        Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.ReplicationGroupDoesNotExistMessage) `
                -f $GroupName
            ) -join '' )

        $returnValue += @{
            Ensure = 'Absent'
        }
    } # if

    return $returnValue
} # Get-TargetResource

<#
    .SYNOPSIS
    Sets the current state of a DFS Replication Group.
 
    .PARAMETER GroupName
    The name of the DFS Replication Group.
 
    .PARAMETER Ensure
    Specifies whether the DSF Replication Group should exist.
 
    .PARAMETER Description
    A description for the DFS Replication Group.
 
    .PARAMETER Members
    A list of computers that are members of this Replication Group.
    These can be specified using either the ComputerName or FQDN name
    for each member. If an FQDN name is used and the DomainName
    parameter is set, the FQDN domain name must match.
 
    .PARAMETER Folders
    A list of folders that are replicated in this Replication Group.
 
    .PARAMETER Topology
    This allows a replication topology to assign to the Replication Group.
    It defaults to Manual, which will not automatically create a topology.
    If set to Fullmesh, a full mesh topology between all members will be
    created.
 
    .PARAMETER ContentPaths
    An array of DFS Replication Group Content Paths to use for each of the
    Folders. This can have one entry for each Folder in the Folders
    parameter and should be set in th same order. If any entry is not blank
    then the Content Paths will need to be set manually by
    using the xDFSReplicationGroupMembership resource.
 
    .PARAMETER DomainName
    The AD domain the Replication Group should created in.
#>

function Set-TargetResource
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $GroupName,

        [Parameter(Mandatory = $true)]
        [ValidateSet('Present','Absent')]
        [System.String]
        $Ensure,

        [Parameter()]
        [System.String]
        $Description,

        [Parameter()]
        [System.String[]]
        $Members,

        [Parameter()]
        [System.String[]]
        $Folders,

        [Parameter()]
        [ValidateSet('Fullmesh','Manual')]
        [System.String]
        $Topology = 'Manual',

        [Parameter()]
        [System.String[]]
        $ContentPaths,

        [Parameter()]
        [System.String]
        $DomainName
    )

    Write-Verbose -Message ( @(
        "$($MyInvocation.MyCommand): "
        $($LocalizedData.SettingRegGroupMessage) `
            -f $GroupName
        ) -join '' )

    # Lookup the existing Replication Group
    $replicationGroupParameters = @{
        GroupName = $GroupName
    }

    if ($DomainName)
    {
        $replicationGroupParameters += @{
            DomainName = $DomainName
        }
    } # if

    $replicationGroup = Get-DfsReplicationGroup @replicationGroupParameters `
        -ErrorAction Stop

    if ($Ensure -eq 'Present')
    {
        # The rep group should exist
        Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.EnsureReplicationGroupExistsMessage) `
                -f $GroupName
            ) -join '' )

        if ($Description)
        {
            $replicationGroupParameters += @{
                Description = $Description
            }
        } # if

        if ($replicationGroup)
        {
            # The RG exists already - Check the existing RG and members
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.ReplicationGroupExistsMessage) `
                    -f $GroupName
                ) -join '' )

            # Check the description
            if (($Description) -and ($replicationGroup.Description -ne $Description))
            {
                Set-DfsReplicationGroup @replicationGroupParameters -ErrorAction Stop

                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupDescriptionUpdatedMessage) `
                        -f $GroupName
                    ) -join '' )
            } # if
        }
        else
        {
            # Ths Rep Groups doesn't exist - Create it
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.ReplicationGroupDoesNotExistMessage) `
                    -f $GroupName
                ) -join '' )

            New-DfsReplicationGroup @replicationGroupParameters -ErrorAction Stop

            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.ReplicationGroupCreatedMessage) `
                    -f $GroupName
                ) -join '' )
        } # if

        # Clean up the splat so we can use it in the next cmdlets
        $replicationGroupParameters.Remove('Description')

        # Create an array of FQDN Members from the Members Array
        $replicationGroupParameters += @{
            ComputerName = ''
        }

        foreach ($member in $Members)
        {
            $replicationGroupParameters.ComputerName = $member
            $fqdnMembers += @(  
                Get-FQDNMemberName @replicationGroupParameters
            )
        } # foreach

        $replicationGroupParameters.Remove('ComputerName')

        # Get the existing members of this DFS Rep Group
        $existingMembers = (Get-DfsrMember @replicationGroupParameters -ErrorAction Stop).DnsName

        # Add any missing members
        foreach ($Member in $fqdnMembers)
        {
            if ($member -notin $existingMembers)
            {
                # Member is missing - add it
                Add-DfsrMember @replicationGroupParameters `
                    -ComputerName $member `
                    -ErrorAction Stop

                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupMemberAddedMessage) `
                        -f $GroupName,$member
                    ) -join '' )
            } # if
        } # foreach

        # Remove any members that shouldn't exist
        foreach ($existingMember in $existingMembers)
        {
            if ($existingMember -notin $fqdnMembers)
            {
                # Member exists but shouldn't - remove it
                Remove-DfsrMember @replicationGroupParameters `
                    -ComputerName $existingMember `
                    -Force `
                    -ErrorAction Stop

                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupMemberRemovedMessage) `
                        -f $GroupName,$existingMember
                    ) -join '' )
            } # if
        } # foreach

        # Get the existing folders of this DFS Rep Group
        $existingFolders = (Get-DfsReplicatedFolder @replicationGroupParameters -ErrorAction Stop).FolderName

        # Add any missing folders
        foreach ($folder in $Folders)
        {
            if ($folder -notin $existingFolders)
            {
                # Folder is missing - add it
                New-DfsReplicatedFolder @replicationGroupParameters `
                    -FolderName $folder `
                    -ErrorAction Stop

                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupFolderAddedMessage) `
                        -f $GroupName,$folder
                    ) -join '' )
            } # if
        } # foreach

        # Remove any folders that shouldn't exist
        foreach ($existingFolder in $existingFolders)
        {
            if ($existingFolder -notin $Folders)
            {
                # Folder exists but shouldn't - remove it
                Remove-DfsReplicatedFolder @replicationGroupParameters `
                    -Folder $existingFolder `
                    -Force `
                    -ErrorAction Stop

                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupFolderRemovedMessage) `
                        -f $GroupName,$existingFolder
                    ) -join '' )
            } # if
        } # foreach

        # Set the content paths (if any were passed in the array)
        if ($ContentPaths)
        {
            # Get the current memberships for this rep group
            $memberships = Get-DfsrMembership @replicationGroupParameters `
                -ErrorAction Stop

            # Scan through the content paths array
            for ($i=0; $i -lt $Folders.Count; $i++)
            {
                $contentPath = $ContentPaths[$i]
                if ($contentPath)
                {
                     foreach ($membership in $memberships)
                     {
                        [System.String] $fqdnMemberName = Get-FQDNMemberName `
                            @replicationGroupParameters `
                            -ComputerName $membership.ComputerName

                        [System.Boolean] $primaryMember = ($fqdnMemberName -eq $fqdnMembers[0])

                        if (($membership.FolderName -ne $Folders[$i]) `
                            -or (($membership.ContentPath -eq $ContentPath) `
                            -and ($membership.PrimaryMember -eq $primaryMember)))
                        {
                            # Don't update this membership
                            continue
                        }

                        # The Content Path for this member needs to be set
                        Set-DfsrMembership @replicationGroupParameters `
                            -FolderName $membership.FolderName `
                            -ComputerName $membership.ComputerName `
                            -PrimaryMember $primaryMember `
                            -ContentPath $ContentPath

                        Write-Verbose -Message ( @(
                            "$($MyInvocation.MyCommand): "
                            $($LocalizedData.ReplicationGroupContentPathUpdatedMessage) `
                                -f $GroupName,$membership.ComputerName
                            ) -join '' )
                    } # foreach
                } # if
            } # foreach
        } # if

        # If the topology is not manual, automatically configure the connections
        switch ($Topology)
        {
            'Fullmesh'
            {
                $replicationGroupParameters += @{
                    SourceComputerName = ''
                    DestinationComputerName = ''
                }

                # Scan through the combination of connections
                foreach ($source in $fqdnMembers)
                {
                    foreach ($destination in $fqdnMembers)
                    {
                        if ($source -eq $destination)
                        {
                            continue
                        } # if

                        $replicationGroupParameters.SourceComputerName = $source
                        $replicationGroupParameters.DestinationComputerName = $destination

                        $replicationGroupConnection = Get-DfsrConnection @replicationGroupParameters `
                            -ErrorAction Stop

                        if ($replicationGroupConnection) {
                            if (-not $replicationGroupConnection.Enabled) {
                                Set-DfsrConnection @replicationGroupParameters `
                                    -DisableConnection $false `
                                    -ErrorAction Stop

                                Write-Verbose -Message ( @(
                                    "$($MyInvocation.MyCommand): "
                                        $($LocalizedData.ReplicationGroupFullMeshConnectionUpdatedMessage) `
                                        -f  $GroupName,$source,$destination
                                    ) -join '' )
                            } # if
                        }
                        else
                        {
                            Add-DfsrConnection @replicationGroupParameters `
                                -ErrorAction Stop

                            Write-Verbose -Message ( @(
                                "$($MyInvocation.MyCommand): "
                                    $($LocalizedData.ReplicationGroupFullMeshConnectionAddedMessage) `
                                    -f  $GroupName,$source,$destination
                                ) -join '' )
                        } # if
                    } # foreach
                } # foreach
            } # 'Fullmesh'
        } # swtich
    }
    else
    {
        # The Rep Group should not exist
        Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.EnsureReplicationGroupDoesNotExistMessage) `
                -f $GroupName
            ) -join '' )

        if ($replicationGroup)
        {
            # Remove the replication group
            Remove-DfsReplicationGroup @replicationGroupParameters `
                -RemoveReplicatedFolders `
                -Force `
                -ErrorAction Stop

            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.ReplicationGroupExistsRemovedMessage) `
                    -f $GroupName
                ) -join '' )
        }
    } # if
} # Set-TargetResource

<#
    .SYNOPSIS
    Tests the current state of a DFS Replication Group.
 
    .PARAMETER GroupName
    The name of the DFS Replication Group.
 
    .PARAMETER Ensure
    Specifies whether the DSF Replication Group should exist.
 
    .PARAMETER Description
    A description for the DFS Replication Group.
 
    .PARAMETER Members
    A list of computers that are members of this Replication Group.
    These can be specified using either the ComputerName or FQDN name
    for each member. If an FQDN name is used and the DomainName
    parameter is set, the FQDN domain name must match.
 
    .PARAMETER Folders
    A list of folders that are replicated in this Replication Group.
 
    .PARAMETER Topology
    This allows a replication topology to assign to the Replication Group.
    It defaults to Manual, which will not automatically create a topology.
    If set to Fullmesh, a full mesh topology between all members will be
    created.
 
    .PARAMETER ContentPaths
    An array of DFS Replication Group Content Paths to use for each of the
    Folders. This can have one entry for each Folder in the Folders
    parameter and should be set in th same order. If any entry is not blank
    then the Content Paths will need to be set manually by
    using the xDFSReplicationGroupMembership resource.
 
    .PARAMETER DomainName
    The AD domain the Replication Group should created in.
#>

function Test-TargetResource
{
    [OutputType([System.Boolean])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $GroupName,

        [Parameter(Mandatory = $true)]
        [ValidateSet('Present','Absent')]
        [System.String]
        $Ensure,

        [Parameter()]
        [System.String]
        $Description,

        [Parameter()]
        [System.String[]]
        $Members,

        [Parameter()]
        [System.String[]]
        $Folders,

        [Parameter()]
        [ValidateSet('Fullmesh','Manual')]
        [System.String]
        $Topology = 'Manual',

        [Parameter()]
        [System.String[]]
        $ContentPaths,

        [Parameter()]
        [System.String]
        $DomainName
    )

    # Flag to signal whether settings are correct
    [System.Boolean] $desiredConfigurationMatch = $true

    Write-Verbose -Message ( @(
        "$($MyInvocation.MyCommand): "
        $($LocalizedData.TestingRegGroupMessage) `
            -f $GroupName
        ) -join '' )

    # Lookup the existing Replication Group
    $replicationGroupParameters = @{
        GroupName = $GroupName
    }

    if ($DomainName)
    {
        $replicationGroupParameters += @{
            DomainName = $DomainName
        }
    } # if

    $replicationGroup = Get-DFSReplicationGroup @replicationGroupParameters `
        -ErrorAction Stop

    if ($Ensure -eq 'Present')
    {
        # The RG should exist
        if ($replicationGroup)
        {
            # The RG exists already
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.ReplicationGroupExistsMessage) `
                    -f $GroupName
                ) -join '' )

            # Check the description
            if (($Description) -and ($replicationGroup.Description -ne $Description))
            {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupDescriptionNeedsUpdateMessage) `
                        -f $GroupName
                    ) -join '' )

                $desiredConfigurationMatch = $false
            } # if

            # Create an array of FQDN Members from the Members Array
            $replicationGroupParameters += @{
                ComputerName = ''
            }

            foreach ($member in $Members)
            {
                $replicationGroupParameters.ComputerName = $member
                $fqdnMembers += @( Get-FQDNMemberName @replicationGroupParameters )
            } # foreach

            $replicationGroupParameters.Remove('ComputerName')

            # Compare the Members
            $existingMembers = @((Get-DfsrMember @replicationGroupParameters -ErrorAction Stop).DnsName)
            if ((Compare-Object `
                -ReferenceObject $fqdnMembers `
                -DifferenceObject $existingMembers).Count -ne 0)
            {
                # There is a member different of some kind.
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupMembersNeedUpdateMessage) `
                        -f $GroupName
                    ) -join '' )

                $desiredConfigurationMatch = $false
            } # if

            # Compare the Folders
            $existingFolders = @((Get-DfsReplicatedFolder @replicationGroupParameters -ErrorAction Stop).FolderName)

            if ((Compare-Object `
                -ReferenceObject $Folders `
                -DifferenceObject $existingFolders).Count -ne 0)
            {
                # There is a folder different of some kind.
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.ReplicationGroupFoldersNeedUpdateMessage) `
                        -f $GroupName
                    ) -join '' )

                $desiredConfigurationMatch = $false
            }

            # Get the content paths (if any were passed in the array)
            if ($ContentPaths)
            {
                # Get the current memberships for this rep group
                $memberships = Get-DfsrMembership @replicationGroupParameters `
                    -ErrorAction Stop

                # Scan through the content paths array
                for ($i=0; $i -lt $Folders.Count; $i++)
                {
                    $contentPath = $ContentPaths[$i]

                    if ($contentPath)
                    {
                        foreach ($membership in $memberships)
                        {
                            [System.Boolean] $primaryMember = ($membership.ComputerName -eq $Members[0])

                            if (($membership.FolderName -ne $Folders[$i]) `
                                -or (($membership.ContentPath -eq $contentPath) `
                                -and ($membership.PrimaryMember -eq $primaryMember)))
                            {
                                # This membership is in the correct state.
                                continue
                            }

                            Write-Verbose -Message ( @(
                                "$($MyInvocation.MyCommand): "
                                $($LocalizedData.ReplicationGroupContentPathNeedUpdateMessage) `
                                    -f $GroupName,$membership.ComputerName
                                ) -join '' )

                            $desiredConfigurationMatch = $false
                        } # if
                    } # if
                } # foreach
            } # if

            # If the topology is not manual, check the connections are configured
            switch ($Topology)
            {
                'Fullmesh'
                {
                    $replicationGroupParameters += @{
                        SourceComputerName = ''
                        DestinationComputerName = ''
                    }

                    # Scan through the combination of connections
                    foreach ($source in $fqdnMembers)
                    {
                        foreach ($destination in $fqdnMembers)
                        {
                            if ($source -eq $destination)
                            {
                                continue
                            } # if

                            $replicationGroupParameters.SourceComputerName = $source
                            $replicationGroupParameters.DestinationComputerName = $destination

                            $replicationGroupConnection = Get-DfsrConnection @replicationGroupParameters `
                                -ErrorAction Stop

                            if ($replicationGroupConnection)
                            {
                                if (-not $replicationGroupConnection.Enabled)
                                {
                                    Write-Verbose -Message ( @(
                                        "$($MyInvocation.MyCommand): "
                                         $($LocalizedData.ReplicationGroupFullMeshDisabledConnectionMessage) `
                                            -f  $GroupName,$source,$destination
                                        ) -join '' )

                                    $desiredConfigurationMatch = $false
                                } # if
                            }
                            else
                            {
                                Write-Verbose -Message ( @(
                                    "$($MyInvocation.MyCommand): "
                                     $($LocalizedData.ReplicationGroupFullMeshMissingConnectionMessage) `
                                        -f  $GroupName,$source,$destination
                                    ) -join '' )

                                $desiredConfigurationMatch = $false
                            } # if
                        } # foreach
                    } # foreach
                } # 'fullmesh'
            } # switch
        }
        else
        {
            # Ths RG doesn't exist but should
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                 $($LocalizedData.ReplicationGroupDoesNotExistButShouldMessage) `
                    -f  $GroupName
                ) -join '' )

            $desiredConfigurationMatch = $false
        } # if
    }
    else
    {
        # The RG should not exist
        if ($replicationGroup)
        {
            # The RG exists but should not
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                 $($LocalizedData.ReplicationGroupExistsButShouldNotMessage) `
                    -f $GroupName
                ) -join '' )

            $desiredConfigurationMatch = $false
        }
        else
        {
            # The RG does not exist and should not
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.ReplicationGroupDoesNotExistAndShouldNotMessage) `
                    -f $GroupName
                ) -join '' )
        } # if
    } # if

    return $desiredConfigurationMatch
} # Test-TargetResource

<#
.SYNOPSIS
    Returns the FQDN Member name based on the ComputerName and DomainName that are provided.
 
    If the ComputerName is already an FQDN but the domain in the FQDN does not match the
    value passed in DomainName then throw an exception.
 
    If the ComputerName is already an FQDN and the domain in the FQDN does match the value
    passed in DomainName then the existing ComputerName is returned.
 
    If the ComputerName is not already an FQDN and the DomainName passed is not empty then
    the ComputerName and DomainName are combined and returned.
 
    If the ComputerName is not already an FQDN and the DomainName passed is empty then
    the ComputerName is returned.
 
    .PARAMETER GroupName
    The name of the DFS Replication Group.
 
    .PARAMETER ComputerName
    The computer name of the DFS Replication Group member.
 
    .PARAMETER DomainName
    The AD domain the Replication Group should created in.
#>

function Get-FQDNMemberName
{
    [OutputType([System.String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $GroupName,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ComputerName,

        [Parameter()]
        [System.String]
        $DomainName
    )

    if ($ComputerName.Contains('.'))
    {
        if (($null -ne $DomainName) -and ($DomainName -ne ''))
        {
            if ($ComputerName -like "*.$DomainName")
            {
                return $ComputerName.ToLower()
            }
            else
            {
                New-InvalidOperationException `
                    -Message ($($LocalizedData.ReplicationGroupDomainMismatchError `
                        -f $GroupName,$ComputerName,$DomainName))
            } # if
        }
        else
        {
            return $ComputerName.ToLower()
        }
    }
    else
    {
        if (($null -ne $DomainName) -and ($DomainName -ne ''))
        {
            return "$ComputerName.$DomainName".ToLower()
        }
        else
        {
            return $ComputerName.ToLower()
        } # if
    } # if
} # Get-FQDNMemberName

Export-ModuleMember -Function *-TargetResource