DSCResources/HWG_cDFSNamespace/HWG_cDFSNamespace.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
data LocalizedData
{
# culture="en-US"
ConvertFrom-StringData -StringData @'
GettingNamespaceMessage=Getting DFS Namespace "{0}" from "{1}.{2}".
NamespaceExistsMessage=DFS Namespace "{0}" on "{1}.{2}" exists.
NamespaceDoesNotExistMessage=DFS Namespace "{0}" on "{1}.{2}" does not exist.
NamespaceTargetExistsMessage=DFS Namespace "{0}" target "{3}" on "{1}.{2}" exists.
NamespaceTargetDoesNotExistMessage=DFS Namespace "{0}" target "{3}" on "{1}.{2}" does not exist.
SettingNamespaceMessage=Setting DFS Namespace "{0}" on "{1}.{2}".
NamespaceUpdateParameterMessage=Setting DFS Namespace "{0}" on "{1}.{2}" parameter {3} to "{4}".
NamespaceCreatedMessage=DFS Namespace "{0}" on "{1}.{2}" created.
NamespaceTargetRemovedMessage=DFS Namespace "{0}" on "{1}.{2}" target "{3}" removed.
TestingNamespaceMessage=Testing DFS Namespace "{0}" on "{1}.{2}".
NamespaceTypeConversionError=Error- {3} DFS Namespace can not be added to non-{3} DFS Namespace "{0}" on "{1}.{2}".
NamespaceParameterNeedsUpdateMessage=DFS Namespace "{0}" on "{1}.{2}" {3} is different. Change required.
NamespaceDoesNotExistButShouldMessage=DFS Namespace "{0}" on "{1}.{2}" does not exist but should. Change required.
NamespaceTargetExistsButShouldNotMessage=DFS Namespace "{0}" target "{3}" on "{1}.{2}" exists but should not. Change required.
NamespaceTargetDoesNotExistButShouldMessage=DFS Namespace "{0}" target "{3}" on "{1}.{2}" does not exist but should. Change required.
NamespaceDoesNotExistAndShouldNotMessage=DFS Namespace "{0}" on "{1}.{2}" does not exists and should not. Change not required.
NamespaceTargetDoesNotExistAndShouldNotMessage=DFS Namespace "{0}" target "{3}" on "{1}.{2}" does not exist and should not. Change not required.
'@

}

function Get-TargetResource 
{
    [CmdletBinding()]
    [OutputType([Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $Namespace,

        [parameter(Mandatory = $true)]
        [ValidateSet('Present','Absent')]
        [String]
        $Ensure,
        
        [String]
        $ComputerName = ($ENV:COMPUTERNAME),

        [String]
        $DomainName       
    )       

    Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.GettingNamespaceMessage) `
                -f $Namespace,$ComputerName,$DomainName 
        ) -join '' )

    # Generate the return object assuming absent.
    $ReturnValue = @{
        Namespace = $Namespace
        Ensure = 'Absent'
        ComputerName = $ComputerName
        DomainName = $DomainName
    }
    
    # Remove the Ensue parmeter from the bound parameters
    $null = $PSBoundParameters.Remove('Ensure')
    
    # Get the namespace path and target path for this namespace target
    $NamespacePath = Get-NamespacePath @PSBoundParameters
    $TargetPath = Get-TargetPath @PSBoundParameters
    
    # Lookup the existing Namespace root
    $Root = Get-Root `
        -Path $NamespacePath
    
    if ($Root)
    {
        # The namespace exists
        Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.NamespaceExistsMessage) `
                    -f $Namespace,$ComputerName,$DomainName 
            ) -join '' )
    }
    else
    {
        # The namespace does not exist
        Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.NamespaceDoesNotExistMessage) `
                    -f $Namespace,$ComputerName,$DomainName 
            ) -join '' )
        return $ReturnValue
    }

    $ReturnValue += @{
        Path                         = $Root.Path
        Type                         = $Root.Type
        TimeToLiveSec                = $Root.TimeToLiveSec
        State                        = $Root.State
        Description                  = $Root.Description
        EnableSiteCosting            = ($Root.Flags -contains 'Site Costing')
        EnableInsiteReferrals        = ($Root.Flags -contains 'Insite Referrals')
        EnableAccessBasedEnumeration = ($Root.Flags -contains 'AccessBased Enumeration')
        EnableRootScalability        = ($Root.Flags -contains 'Root Scalability')
        EnableTargetFailback         = ($Root.Flags -contains 'Target Failback')
    }
    
    # DFS Root exists but does target exist?
    $Target = Get-RootTarget `
        -Path $NamespacePath `
        -TargetPath $TargetPath

    if ($Target)
    {
        # The target exists in this namespace
        $ReturnValue.Ensure = 'Present'
        $ReturnValue += @{
            ReferralPriorityClass        = $Target.ReferralPriorityClass
            ReferralPriorityRank         = $Target.ReferralPriorityRank
        }
        
        Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.NamespaceTargetExistsMessage) `
                    -f $Namespace,$ComputerName,$DomainName,$TargetPath 
            ) -join '' )
    }
    else
    {               
        # The target does not exist in this namespace
        Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.NamespaceTargetDoesNotExistMessage) `
                    -f $Namespace,$ComputerName,$DomainName,$TargetPath
            ) -join '' )
    }    
    
    return $ReturnValue
} # Get-TargetResource

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $Namespace,

        [parameter(Mandatory = $true)]
        [ValidateSet('Present','Absent')]
        [String]
        $Ensure,
        
        [String]
        $ComputerName = ($ENV:COMPUTERNAME),

        [String]
        $DomainName,

        [String]
        $Description,
        
        [Boolean]
        $EnableSiteCosting,
        
        [Boolean]
        $EnableInsiteReferrals,
    
        [Boolean]
        $EnableAccessBasedEnumeration,
        
        [Boolean]
        $EnableRootScalability,
        
        [Boolean]
        $EnableTargetFailback,

        [ValidateSet('Global-High','SiteCost-High','SiteCost-Normal','SiteCost-Low','Global-Low')]
        [String]
        $ReferralPriorityClass,
        
        [Uint32]
        $ReferralPriorityRank        
    )

    Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.SettingNamespaceMessage) `
                -f $Namespace,$ComputerName,$DomainName 
        ) -join '' )

    # Create splat for passing to get-* functions
    $Splat = @{
        Namespace = $Namespace
        ComputerName = $ComputerName
        DomainName = $DomainName
    }
    
    # Get the namespace path and target path for this namespace target
    $NamespacePath = Get-NamespacePath @Splat
    $TargetPath = Get-TargetPath @Splat

    # Lookup the existing Namespace root
    $Root = Get-Root `
        -Path $NamespacePath

    if ($Ensure -eq 'Present')
    {
        # Set desired Configuration
        if ($Root)
        {
            # Does the root need to be updated?
            [boolean] $RootChange = $false
            
            # The root properties that will be updated
            $RootProperties = @{
                State = 'online'
            }

            if (($Description) `
                -and ($Root.Description -ne $Description))
            {
                $RootProperties += @{
                    Description = $Description
                }
                $RootChange = $true
            }

            if (($EnableSiteCosting -ne $null) `
                -and (($Root.Flags -contains 'Site Costing') -ne $EnableSiteCosting))
            {
                $RootProperties += @{                    
                    EnableSiteCosting = $EnableSiteCosting
                }
                $RootChange = $true
            }

            if (($EnableInsiteReferrals -ne $null) `
                -and (($Root.Flags -contains 'Insite Referrals') -ne $EnableInsiteReferrals))
            {
                $RootProperties += @{                    
                    EnableInsiteReferrals = $EnableInsiteReferrals
                }
                $RootChange = $true
            }

            if (($EnableAccessBasedEnumeration -ne $null) `
                -and (($Root.Flags -contains 'AccessBased Enumeration') -ne $EnableAccessBasedEnumeration))
            {
                $RootProperties += @{                    
                    EnableAccessBasedEnumeration = $EnableAccessBasedEnumeration
                }
                $RootChange = $true
            }

            if (($EnableRootScalability -ne $null) `
                -and (($Root.Flags -contains 'Root Scalability') -ne $EnableRootScalability))
            {
                $RootProperties += @{                    
                    EnableRootScalability = $EnableRootScalability
                }
                $RootChange = $true
            }

            if (($EnableTargetFailback -ne $null) `
                -and (($Root.Flags -contains 'Target Failback') -ne $EnableTargetFailback))
            {
                $RootProperties += @{                    
                    EnableTargetFailback = $EnableTargetFailback
                }
                $RootChange = $true
            }
            
            if ($RootChange)
            {
                # Update root settings
                $null = Set-DfsnRoot `
                    -Path $NamespacePath `
                    @RootProperties `
                    -ErrorAction Stop

                $RootProperties.GetEnumerator() | ForEach-Object -Process {                
                    Write-Verbose -Message ( @(
                        "$($MyInvocation.MyCommand): "
                        $($LocalizedData.NamespaceUpdateParameterMessage) `
                            -f $Namespace,$ComputerName,$DomainName,$_.name, $_.value
                    ) -join '' )            
                }
            }
                                
            # Get target
            $Target = Get-RootTarget `
                -Path $NamespacePath `
                -TargetPath $TargetPath
            
            # Does the target need to be updated?
            [boolean] $TargetChange = $false

            # The Target properties that will be updated
            $TargetProperties = @{}

            # Check the target properties
            if (($ReferralPriorityClass) `
                -and ($Target.ReferralPriorityClass -ne $ReferralPriorityClass))
            {
                $TargetProperties += @{
                    ReferralPriorityClass = ($ReferralPriorityClass -replace '-','')
                }
                $TargetChange = $true
            }
            
            if (($ReferralPriorityRank) `
                -and ($Target.ReferralPriorityRank -ne $ReferralPriorityRank))
            {
                $TargetProperties += @{                    
                    ReferralPriorityRank = $ReferralPriorityRank
                }
                $TargetChange = $true
            }                

            # Is the target a member of the namespace?
            if ($Target)
            {
                # Does the target need to be changed?
                if ($TargetChange)
                {
                    # Update target settings
                    $null = Set-DfsnRootTarget `
                        -Path $NamespacePath `
                        -TargetPath $TargetPath `
                        @TargetProperties `
                        -ErrorAction Stop
                }
            }
            else
            {
                # Add target to Namespace
                $null = New-DfsnRootTarget `
                    -Path $NamespacePath `
                    -TargetPath $TargetPath `
                    @TargetProperties `
                    -ErrorAction Stop
            }

            # Output the target parameters that were changed/set
            $TargetProperties.GetEnumerator() | ForEach-Object -Process {                
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceUpdateParameterMessage) `
                        -f $Namespace,$ComputerName,$DomainName,$_.name, $_.value
                ) -join '' )            
            }            
        }
        else
        {
            # Prepare to use the PSBoundParameters as a splat to created
            # The new DFS Namespace root.
            $null = $PSBoundParameters.Remove('Namespace')
            $null = $PSBoundParameters.Remove('Ensure')
            $null = $PSBoundParameters.Remove('ComputerName')
            $null = $PSBoundParameters.Remove('DomainName')
            
            $PSBoundParameters += @{
                Type = 'Standalone'
                TargetPath  = $TargetPath
            }

            if ($DomainName)
            {
                $PSBoundParameters.Type = 'DomainV2'
            }

            if (! $Description)
            {
                $PSBoundParameters += @{ Description = "DFS of Namespace $Namespace" }
            } 
            
            if ($ReferralPriorityClass)
            { 
                $PSBoundParameters.ReferralPriorityClass = ($ReferralPriorityClass -replace '-','')
            }

            # Create New-DfsnRoot
            $null = New-DfsnRoot `
                @PSBoundParameters `
                -ErrorAction Stop
                                    
            $PSBoundParameters.GetEnumerator() | ForEach-Object -Process {                
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceUpdateParameterMessage) `
                        -f $Namespace,$ComputerName,$DomainName,$_.name, $_.value
                ) -join '' )            
            }
        }
    }
    else
    {
        # The Namespace Target should not exist

        # Get root target
        $Target = Get-RootTarget `
            -Path $NamespacePath `
            -TargetPath $TargetPath
            
        if ($Target)                
        {
            # Remove the target from the namespace
            $null = Remove-DfsnRootTarget `
                -Path $NamespacePath `
                -TargetPath $TargetPath `
                -Confirm:$false `
                -ErrorAction Stop

            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                $($LocalizedData.NamespaceTargetRemovedMessage) `
                    -f $Namespace,$ComputerName,$DomainName,$TargetPath
            ) -join '' )
        }            
    }
} # Set-TargetResource

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

        [parameter(Mandatory = $true)]
        [ValidateSet('Present','Absent')]
        [String]
        $Ensure,
        
        [String]
        $ComputerName = ($ENV:COMPUTERNAME),

        [String]
        $DomainName,
        
        [String]
        $Description,

        [Boolean]
        $EnableSiteCosting,
        
        [Boolean]
        $EnableInsiteReferrals,
    
        [Boolean]
        $EnableAccessBasedEnumeration,
        
        [Boolean]
        $EnableRootScalability,
        
        [Boolean]
        $EnableTargetFailback,

        [ValidateSet('Global-High','SiteCost-High','SiteCost-Normal','SiteCost-Low','Global-Low')]
        [String]
        $ReferralPriorityClass,
        
        [Uint32]
        $ReferralPriorityRank          
    )
   
    Write-Verbose -Message ( @(
            "$($MyInvocation.MyCommand): "
            $($LocalizedData.TestingNamespaceMessage) `
                -f $Namespace,$ComputerName,$DomainName 
        ) -join '' )

    # Flag to signal whether settings are correct
    [Boolean] $DesiredConfigurationMatch = $true    

    # Create splat for passing to get-* functions
    $Splat = @{
        Namespace = $Namespace
        ComputerName = $ComputerName
        DomainName = $DomainName
    }

    # Get the namespace path and target path for this namespace target
    $NamespacePath = Get-NamespacePath @Splat
    $TargetPath = Get-TargetPath @Splat

    # Lookup the existing Namespace root
    $Root = Get-Root `
        -Path $NamespacePath
            
    if ($Ensure -eq 'Present')
    {
        # The Namespace root should exist
        if ($Root)
        {
            # The Namespace root exists and should

            # Changing the namespace type is not possible - the namespace
            # can only be recreated if the type should change.
            if ((($DomainName) -and ($Root.Type -notmatch 'Domain')) -or `
                 ((! $DomainName) -and ($Root.Type -notmatch 'Standalone')))
            {                    
                if ($Root.Type -eq 'Standalone')
                {
                    $NewType = 'Domain'
                }
                else
                {
                    $NewType = 'Standalone'
                }
                $ErrorParam = @{
                    ErrorId = 'NamespaceTypeConversionError'
                    ErrorMessage = $($LocalizedData.NamespaceTypeConversionError) `
                        -f $Namespace,$ComputerName,$DomainName,$NewType
                    ErrorCategory = 'InvalidOperation'
                    ErrorAction = 'Stop'
                }
                New-TerminatingError @ErrorParam
            }        

            # Check the Namespace parameters
            if (($Description) `
                -and ($Root.Description -ne $Description)) {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                        -f $Namespace,$ComputerName,$DomainName,'Description'
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }
                        
            if (($EnableSiteCosting -ne $null) `
                -and (($Root.Flags -contains 'Site Costing') -ne $EnableSiteCosting)) {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                        -f $Namespace,$ComputerName,$DomainName,'EnableSiteCosting'
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }

            if (($EnableInsiteReferrals -ne $null) `
                -and (($Root.Flags -contains 'Insite Referrals') -ne $EnableInsiteReferrals)) {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                        -f $Namespace,$ComputerName,$DomainName,'EnableInsiteReferrals'
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }

            if (($EnableAccessBasedEnumeration -ne $null) `
                -and (($Root.Flags -contains 'AccessBased Enumeration') -ne $EnableAccessBasedEnumeration)) {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                        -f $Namespace,$ComputerName,$DomainName,'EnableAccessBasedEnumeration'
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }

            if (($EnableRootScalability -ne $null) `
                -and (($Root.Flags -contains 'Root Scalability') -ne $EnableRootScalability)) {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                        -f $Namespace,$ComputerName,$DomainName,'EnableRootScalability'
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }

            if (($EnableTargetFailback -ne $null) `
                -and (($Root.Flags -contains 'Target Failback') -ne $EnableTargetFailback)) {
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                        -f $Namespace,$ComputerName,$DomainName,'EnableTargetFailback'
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }

            $Target = Get-RootTarget `
                -Path $NamespacePath `
                -TargetPath $TargetPath

            if ($Target)
            {
                if (($ReferralPriorityClass) `
                    -and ($Target.ReferralPriorityClass -ne $ReferralPriorityClass)) {
                    Write-Verbose -Message ( @(
                        "$($MyInvocation.MyCommand): "
                        $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                            -f $Namespace,$ComputerName,$DomainName,'ReferralPriorityClass'
                        ) -join '' )
                    $desiredConfigurationMatch = $false
                }

                if (($ReferralPriorityRank) `
                    -and ($Target.ReferralPriorityRank -ne $ReferralPriorityRank)) {
                    Write-Verbose -Message ( @(
                        "$($MyInvocation.MyCommand): "
                        $($LocalizedData.NamespaceParameterNeedsUpdateMessage) `
                            -f $Namespace,$ComputerName,$DomainName,'ReferralPriorityRank'
                        ) -join '' )
                    $desiredConfigurationMatch = $false
                }
            }
            else
            {
                # The Root target does not exist but should - change required
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceTargetDoesNotExistButShouldMessage) `
                        -f $Namespace,$ComputerName,$DomainName,$TargetPath
                    ) -join '' )
                $desiredConfigurationMatch = $false                   
            }
        }
        else
        {
            # Ths Namespace root doesn't exist but should - change required
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                 $($LocalizedData.NamespaceDoesNotExistButShouldMessage) `
                    -f $Namespace,$ComputerName,$DomainName
                ) -join '' )
            $desiredConfigurationMatch = $false
        }
    }
    else
    {
        # The Namespace target should not exist
        if ($Root)
        {
            $Target = Get-RootTarget `
                -Path $NamespacePath `
                -TargetPath $TargetPath
                
            if ($Target)
            {
                # The Root target exists but should not - change required
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceTargetExistsButShouldNotMessage) `
                        -f $Namespace,$ComputerName,$DomainName,$TargetPath
                    ) -join '' )
                $desiredConfigurationMatch = $false
            }
            else
            {
                # The Namespace exists but the target doesn't - change not required
                Write-Verbose -Message ( @(
                    "$($MyInvocation.MyCommand): "
                    $($LocalizedData.NamespaceTargetDoesNotExistAndShouldNotMessage) `
                        -f $Namespace,$ComputerName,$DomainName,$TargetPath
                    ) -join '' )
            }
        }
        else
        {
            # The Namespace does not exist (so neither does the target) - change not required
            Write-Verbose -Message ( @(
                "$($MyInvocation.MyCommand): "
                 $($LocalizedData.NamespaceDoesNotExistAndShouldNotMessage) `
                    -f $Namespace,$ComputerName,$DomainName
                ) -join '' )
        }
    } # if
               
    return $DesiredConfigurationMatch 

} # Test-TargetResource

# Helper Functions
Function Get-NamespacePath {
    param
    (
        [String]
        $Namespace,

        [String]
        $ComputerName,

        [String]
        $DomainName
    )       
    # Determine the Namespace Path.
    if ($DomainName)
    {
        $DFSRootName = $DomainName
    }
    else 
    {
        $DFSRootName = $ComputerName.ToUpper()
    }
    return (Join-Path -Path "\\$DFSRootName" -ChildPath $Namespace)
}

Function Get-TargetPath {
    param
    (
        [String]
        $Namespace,

        [String]
        $ComputerName,

        [String]
        $DomainName
    )       
    # Determine the Target Path.
    return (Join-Path -Path "\\$($ComputerName.ToUpper())" -ChildPath $Namespace)
}

Function Get-Root {
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $Path
    )
    # Lookup the DFSN Root.
    # Return null if doesn't exist.
    try
    {
        $DfsnRoot = Get-DfsnRoot `
            -Path $Path `
            -ErrorAction Stop
    }
    catch [Microsoft.Management.Infrastructure.CimException]
    {
        $DfsnRoot = $null
    }
    catch
    {
        Throw $_
    }
    Return $DfsnRoot
}

Function Get-RootTarget {
    param
    (
        [parameter(Mandatory = $true)]
        [String]
        $Path,
        
        [parameter(Mandatory = $true)]
        [String]
        $TargetPath        
    )
    # Lookup the DFSN Root Target in a namespace.
    # Return null if doesn't exist.
    try
    {
        $DfsnTarget = Get-DfsnRootTarget `
            -Path $Path `
            -TargetPath $TargetPath `
            -ErrorAction Stop
    }
    catch [Microsoft.Management.Infrastructure.CimException]
    {
        $DfsnTarget = $null
    }
    catch
    {
        Throw $_
    }
    Return $DfsnTarget
}

function New-TerminatingError
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [String] $ErrorId,

        [Parameter(Mandatory)]
        [String] $ErrorMessage,

        [Parameter(Mandatory)]
        [System.Management.Automation.ErrorCategory] $ErrorCategory
    )

    $exception = New-Object `
        -TypeName System.InvalidOperationException `
        -ArgumentList $errorMessage
    $errorRecord = New-Object `
        -TypeName System.Management.Automation.ErrorRecord `
        -ArgumentList $exception, $errorId, $errorCategory, $null
    $PSCmdlet.ThrowTerminatingError($errorRecord)
}

Export-ModuleMember -Function *-TargetResource