RDCManager.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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
using namespace System.Xml.Linq
using assembly System.Xml.Linq

function InitializeModule {
    Set-RdcConfiguration -Reset
}


function GetAdsiComputer {
    <#
    .SYNOPSIS
        Get an computer object using ADSI.
    .DESCRIPTION
        These basic ADSI commands allow the RdcMan document generator to be used without the MS AD module.
 
        Use of the internal commands is optional. If used, all filters must be written as LDAP filter.
    #>


    [CmdletBinding()]
    param (
        # A filter describing the computers units to find.
        [String]$Filter,

        # The search base for this search.
        [String]$SearchBase,

        # The search scope for the search operation.
        [System.DirectoryServices.SearchScope]$SearchScope,

        # Limit the number of results returned by a search. By default result set size is unlimited.
        [Int32]$ResultSetSize,

        # The server to use to execute the search.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential
    )

    if ($Filter -eq '*' -or -not $Filter) {
        $psboundparameters['Filter'] = '(&(objectCategory=computer)(objectClass=computer))'
    } else {
        $psboundparameters['Filter'] = '(&(objectCategory=computer)(objectClass=computer){0})' -f $Filter
    }

    GetAdsiObject -Properties 'name', 'description', 'dnsHostName' @psboundparameters
}


function GetAdsiObject {
    <#
    .SYNOPSIS
        Get an arbitrary object using ADSI.
    .DESCRIPTION
        These basic ADSI commands allow the RdcMan document generator to be used without the MS AD module.
 
        Use of the internal commands is optional. If used, all filters must be written as LDAP filter.
    #>


    [CmdletBinding()]
    param (
        # A filter describing the computers units to find.
        [Parameter(Mandatory)]
        [String]$Filter,

        # A list of properties to retrieve
        [String[]]$Properties = 'distinguishedName',

        # The search base for this search.
        [String]$SearchBase,

        # The search scope for the search operation.
        [System.DirectoryServices.SearchScope]$SearchScope,

        # Limit the number of results returned by a search. By default result set size is unlimited.
        [Int32]$ResultSetSize,

        # The server to use to execute the search.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential
    )

    $params = @{}
    if ($Server) { $params.Add('Server', $Server) }
    if ($Credential) { $params.Add('Credential', $Credential) }

    $params = @{}
    if ($Server) { $params.Add('Server', $Server) }
    if ($Credential) { $params.Add('Credential', $Credential) }

    if (-not $SearchBase) {
        $SearchBase = (GetAdsiRootDse @params).defaultNamingContext
    }
    $adsiSearchBase = NewDirectoryEntry -DistinguishedName $SearchBase @params

    $searcher = [ADSISearcher]@{
        Filter      = $Filter
        SearchRoot  = $adsiSearchBase
        SearchScope = $SearchScope
        PageSize    = 1000
    }
    $searcher.PropertiesToLoad.AddRange($Properties)

    if ($ResultSetSize) {
        $searcher.SizeLimit = $ResultSetSize
    }

    Write-Debug 'SEARCHER:'
    Write-Debug (' Filter : {0}' -f $Filter)
    Write-Debug (' SearchBase : {0}' -f $SearchBase)
    Write-Debug (' SearchScope: {0}' -f $SearchScope)

    foreach ($searchResult in $searcher.FindAll()) {
        $objectProperties = @{}
        foreach ($property in $Properties) {
            $objectProperties.Add($property, $searchResult.Properties[$property][0])
        }
        [PSCustomObject]$objectProperties
    }
}


function GetAdsiOrganizationalUnit {
    <#
    .SYNOPSIS
        Get an organization unit object using ADSI.
    .DESCRIPTION
        These basic ADSI commands allow the RdcMan document generator to be used without the MS AD module.
 
        Use of the internal commands is optional. If used, all filters must be written as LDAP filter.
    #>


    [CmdletBinding(DefaultParameterSetName = 'UsingFilter')]
    param (
        # A filter describing the organizational units to find.
        [Parameter(ParameterSetName = 'UsingFilter')]
        [String]$Filter,

        # Use identity instead of a filter to locate the OU.
        [Parameter(ParameterSetName = 'ByIdentity')]
        [String]$Identity,

        # The search base for this search.
        [String]$SearchBase,

        # The search scope for the search operation.
        [System.DirectoryServices.SearchScope]$SearchScope,

        # The server to use to execute the search.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential
    )

    if ($Identity) {
        $psboundparameters['Filter'] = '(&(objectClass=organizationalUnit)(distinguishedName={0}))' -f $Identity
        $psboundparameters['SearchScope'] = 'Subtree'
        $null = $psboundparameters.Remove('Identity')
    } elseif ($Filter -eq '*' -or -not $Filter) {
        $psboundparameters['Filter'] = '(objectClass=organizationalUnit)'
    } else {
        $psboundparameters['Filter'] = '(&(objectClass=organizationalUnit){0})' -f $Filter
    }

    GetAdsiObject -Properties 'name', 'description', 'distinguishedName' @psboundparameters
}


function GetAdsiRootDse {
    <#
    .SYNOPSIS
        Get a RootDSE node using ADSI.
    .DESCRIPTION
        These basic ADSI commands allow the RdcMan document generator to be used without the MS AD module.
 
        Use of the internal commands is optional. If used, all filters must be written as LDAP filter.
    #>


    [CmdletBinding()]
    param (
        # The server to use for the ADSI connection.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential
    )

    $rootDSE = NewDirectoryEntry -DistinguishedName 'RootDSE' @psboundparameters
    $properties = @{}
    foreach ($property in $rootDSE.Properties.Keys) {
        $properties.Add($property, $rootDSE.Properties[$property])
    }
    [PSCustomObject]$properties
}


function NewDirectoryEntry {
    <#
    .SYNOPSIS
        Creates a System.DirectoryServices.DirectoryEntry object.
    .DESCRIPTION
        Creates a System.DirectoryServices.DirectoryEntry object.
    #>


    [CmdletBinding()]
    param (
        # The distinguished name to connect to.
        [String]$DistinguishedName,

        # The server used for the connection.
        [String]$Server,

        # Any credentials which should be used.
        [PSCredential]$Credential
    )

    if ($Server) {
        $Path = 'LDAP://{0}/{1}' -f $Server, $DistinguishedName
    } else {
        $Path = 'LDAP://{0}' -f $DistinguishedName
    }
    if ($Credential) {
        [ADSI]::new($Path, $Credential.Username, $Credential.GetNetworkCredential().Password)
    } else {
        [ADSI]::new($Path)
    }
}


function GetADComputer {
    <#
    .SYNOPSIS
        Use either the ActiveDirectory module or ADSI to find computer objects.
    .DESCRIPTION
        Use either the ActiveDirectory module or ADSI to find computer objects.
    #>


    [CmdletBinding(DefaultParameterSetName = 'UsingFilter')]
    param (
        # A filter to use for the search. If using the ActiveDirectory module this can either be an LDAP filter, or the specialised form used by the ActiveDirectory module.
        [Parameter(ParameterSetName = 'UsingFilter')]
        [String]$Filter,

        # When searching by name the names are assembled into a filter for each name using the OR operator.
        [Parameter(ParameterSetName = 'ByName')]
        [String[]]$Name,

        # A searchbase to use. If a search base is not set, the root of the current domain is used.
        [String]$SearchBase,

        # The search scope for the search operation.
        [System.DirectoryServices.SearchScope]$SearchScope,

        # Limit the number of results returned by a search. By default result set size is unlimited.
        [Int32]$ResultSetSize,

        # The server to use for the search.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential,

        # The filter format to use.
        [String]$FilterFormat = (Get-RdcConfiguration -Name FilterFormat)
    )

    $null = $psboundparameters.Remove('FilterFormat')
    if ($pscmdlet.ParameterSetName -eq 'ByName') {
        $null = $psboundparameters.Remove('Name')

        $FilterFormat = 'LDAP'
        $nameFilters = foreach ($value in $Name) {
            '(name={0})' -f $value
        }
        $Filter = '(|{0})' -f (-join $nameFilters)
        $psboundparameters.Add('Filter', $Filter)
    }

    if (Get-RdcConfiguration -Name SearchMode -Eq ADModule) {
        if ($FilterFormat -eq 'LDAP') {
            $null = $psboundparameters.Remove('Filter')
            $psboundparameters.Add('LdapFilter', $Filter)
        }
        Get-ADComputer -Properties dnsHostName, displayName @psboundparameters
    } else {
        GetAdsiComputer @psboundparameters
    }
}


function GetADObject {
    <#
    .SYNOPSIS
        Use either the ActiveDirectory module or ADSI to find arbitrary objects.
    .DESCRIPTION
        Use either the ActiveDirectory module or ADSI to find arbitrary objects.
    #>


    [CmdletBinding()]
    param (
        # A filter to use for the search. If using the ActiveDirectory module this can either be an LDAP filter, or the specialised form used by the ActiveDirectory module.
        [String]$Filter,

        # A searchbase to use. If a search base is not set, the root of the current domain is used.
        [String]$SearchBase,

        # The search scope for the search operation.
        [System.DirectoryServices.SearchScope]$SearchScope,

        # The server to use for the search.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential
    )

    $null = $psboundparameters.Remove('FilterFormat')
    if ($pscmdlet.ParameterSetName -eq 'ByName') {
        $null = $psboundparameters.Remove('Name')

        $FilterFormat = 'LDAP'
        $nameFilters = foreach ($value in $Name) {
            '(name={0})' -f $value
        }
        $Filter = '(|{0})' -f (-join $nameFilters)
        $psboundparameters.Add('Filter', $Filter)
    }

    if (Get-RdcConfiguration -Name SearchMode -Eq ADModule) {
        if ($FilterFormat -eq 'LDAP') {
            $null = $psboundparameters.Remove('Filter')
            $psboundparameters.Add('LdapFilter', $Filter)
        }
        Get-ADComputer @psboundparameters
    } else {
        GetAdsiObject @psboundparameters
    }
}


function GetADOrganizationalUnit {
    <#
    .SYNOPSIS
        Use either the ActiveDirectory module or ADSI to find organizational unit objects.
    .DESCRIPTION
        Use either the ActiveDirectory module or ADSI to find organizational unit objects.
    #>


    [CmdletBinding()]
    param (
        [Parameter(ParameterSetName = 'ByName')]
        [String]$Name,

        # A filter to use for the search. If using the ActiveDirectory module this can either be an LDAP filter, or the specialised form used by the ActiveDirectory module.
        [Parameter(ParameterSetName = 'UsingFilter')]
        [String]$Filter,

        # Use identity instead of a filter to locate the OU.
        [Parameter(ParameterSetName = 'ByIdentity')]
        [String]$Identity,

        # A searchbase to use. If a search base is not set, the root of the current domain is used.
        [String]$SearchBase,

        # The search scope for the search operation.
        [System.DirectoryServices.SearchScope]$SearchScope,

        # The server to use for the search.
        [String]$Server,

        # Credentials to use when connecting to the server.
        [PSCredential]$Credential,

        # The filter format to use.
        [String]$FilterFormat = (Get-RdcConfiguration -Name FilterFormat)
    )

    if ($pscmdlet.ParameterSetName -eq 'ByName') {
        $null = $psboundparameters.Remove('Name')

        $FilterFormat = 'LDAP'
        $Filter = '(name={0})' -f $Name
        $psboundparameters.Add('Filter', $Filter)
    }

    if (-not $SearchBase) {
        $null = $psboundparameters.Remove('SearchBase')
    }

    if (Get-RdcConfiguration -Name SearchMode -Eq ADModule) {
        if ($FilterFormat -eq 'LDAP') {
            $null = $psboundparameters.Remove('Filter')
            $psboundparameters.Add('LdapFilter', $Filter)
        }
        Get-ADOrganizationalUnit @psboundparameters
    } else {
        GetAdsiOrganizationalUnit @psboundparameters
    }
}


function Get-RdcConfiguration {
    <#
    .SYNOPSIS
        Get the configuration for the document generator.
    .DESCRIPTION
        Get the configuration for the document generator.
    #>


    [CmdletBinding()]
    param (
        # Get a specific configuration value by name.
        [String]$Name,

        # Get a configuration value and test whether or not it is equal to the specified value.
        [Object]$Eq
    )

    if ($Name -and $psboundparameters.ContainsKey('Eq')) {
        $script:Configuration.$Name -eq $Eq
    } elseif ($Name) {
        $Script:configuration.$Name
    } else {
        $Script:configuration
    }
}


function Set-RdcConfiguration {
    <#
    .SYNOPSIS
        Set the configuration for the document generator.
    .DESCRIPTION
        Sets the configuration used by the document generator.
    #>


    [CmdletBinding()]
    param (
        # Set the search mode used when building content from AD.
        #
        # The following values may be set:
        #
        # - ADModule: Uses the MS ActiveDirectory module.
        # - ADSI: Uses the ADSI search commands in this module.
        #
        # The default search mode is ADModule if the ActiveDirectory module is available on the computer. Otherwise the search mode defaults to ADSI.
        #
        # If the ActiveDirectory module is made available using implicit remoting this option must be set.
        [Parameter(ParameterSetName = 'Update')]
        [ValidateSet('ADModule', 'ADSI')]
        [String]$SearchMode,

        # The format used for filters. By default LDAP format is used when the search mode is ADSI. The ActiveDirectory format is used if the module is used.
        [Parameter(ParameterSetName = 'Update')]
        [ValidateSet('ADModule', 'LDAP')]
        [String]$FilterFormat,

        # Reset the configuration to the default.
        [Parameter(ParameterSetName = 'Reset')]
        [Switch]$Reset
    )

    if ($pscmdlet.ParameterSetName -eq 'Reset') {
        [Boolean]$isADModulePresent = Get-Module ActiveDirectory -ListAvailable

        $Script:configuration = [PSCustomObject]@{
            SearchMode   = ('ADSI', 'ADModule')[$isADModulePresent]
            FilterFormat = ('LDAP', 'ADModule')[$isADModulePresent]
        }
    } else {
        if ($SearchMode -and -not $FilterFormat) {
            if ($SearchMode -eq 'ADSI') {
                $psboundparameters['FilterFormat'] = 'LDAP'
            } else {
                $psboundparameters['FilterFormat'] = 'ADModule'
            }
        }

        foreach ($parameterName in $psboundparameters.Keys) {
            if ($Script:configuration.PSObject.Properties.Item($parameterName)) {
                $Script:configuration.$parameterName = $psboundparameters[$parameterName]
            }
        }
    }
}


function ADConfiguration {
    <#
    .SYNOPSIS
        Set the AD any AD configuration which should be used when searching Active Directory.
    .DESCRIPTION
        The ADConfiguration element provides default values for AD search operations in child scopes.
 
        The ADConfiguration element is expected to be used in RdcDocument or RdcGroup elements.
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateScript(
            {
                if ($_.ContainsKey('Credential') -and $_['Credential'] -isnot [PSCredential]) {
                    throw 'The credential key was present, but the value is not a credential object.'
                }
                foreach ($key in $_.Keys) {
                    if ($key -notin 'Server', 'Credential') {
                        throw ('Invalid key in the ADConfigurastion hashtable. Valid keys are Server and Credential')
                    }
                }
                $true
            }
        )]
        [Hashtable]$ADConfiguration
    )

    try {
        # Get the value of the parentNode variable from the parent scope(s)
        $parentNode = Get-Variable currentNode -ValueOnly -ErrorAction Stop
    } catch {
        throw ('{0} must be nested in RdcDocument or RdcGroup: {1}' -f $myinvocation.InvocationName, $_.Exception.Message)
    }

    foreach ($key in $ADConfiguration.Keys) {
        New-Variable -Name ('RdcAD{0}' -f $key) -Value $ADConfiguration[$key] -Scope 1 -Force
    }
}


function RdcADComputer {
    <#
    .SYNOPSIS
        Creates a set of computers under a document or group.
    .DESCRIPTION
        RdcADComputer is used to create computer objects based on a search of Active Directory.
    #>


    [CmdletBinding(DefaultParameterSetName = 'UsingFilter')]
    param (
        # The filter which will be used to find computers.
        [Parameter(Position = 1, ParameterSetName = 'UsingFilter')]
        [String]$Filter = '*',

        # When searching by name the names are assembled into a filter for each name using the OR operator.
        [Parameter(ParameterSetName = 'ByName')]
        [String[]]$Name,

        # The search base. By default the search is performed from the root of the current domain.
        [String]$SearchBase,

        # The server to use for this operation.
        [String]$Server = (Get-Variable RdcADServer -ValueOnly -ErrorAction SilentlyContinue),

        # Credentials to use when connecting to active directory.
        [PSCredential]$Credential = (Get-Variable RdcADCredential -ValueOnly -ErrorAction SilentlyContinue),

        # If recurse is set computer objects from child OUs will be added to the parent group.
        [Switch]$Recurse
    )

    if (-not $psboundparameters.ContainsKey('SearchBase')) {
        if ($candidateDN = Get-Variable parentDN -ValueOnly -ErrorAction SilentlyContinue) {
            $SearchBase = $candidateDN
        }
    }

    $params = @{
        SearchBase  = $SearchBase
        SearchScope = ('OneLevel', 'Subtree')[$Recurse.ToBool()]
    }
    if ($Name) {
        $params.Add('Name', $Name)
    } else {
        $params.Add('Filter', $Filter)
    }
    if ($Server) {
        $params.Add('Server', $Server)
    }
    if ($Credential) {
        $params.Add('Credential', $Credential)
    }

    GetADComputer @params |
        Sort-Object Name |
        RdcComputer
}


function RdcADGroup {
    <#
    .SYNOPSIS
        Create a group node derived from the content of an organisational unit.
    .DESCRIPTION
        Create a group node derived from the content of an organisational unit.
    #>


    [CmdletBinding(DefaultParameterSetName = 'UsingFilter')]
    param (
        # The identity of a single OU.
        [Parameter(Mandatory, Position = 1, ParameterSetName = 'ByName')]
        [String]$Name,

        # A filter for OU objects.
        [Parameter(ParameterSetName = 'UsingFilter')]
        [String]$Filter = '*',

        # The identity of a single OU.
        [Parameter(Mandatory, ParameterSetName = 'ByIdentity')]
        [String]$Identity,

        # A filter to apply when evaluating descendent computer objects.
        [String]$ComputerFilter = '*',

        # The search base to use when using a filter.
        [Parameter(ParameterSetName = 'UsingFilter')]
        [String]$SearchBase,

        # The server to use for this operation.
        [String]$Server = (Get-Variable RdcADServer -ValueOnly -ErrorAction SilentlyContinue),

        # Credentials to use when connecting to active directory.
        [PSCredential]$Credential = (Get-Variable RdcADCredential -ValueOnly -ErrorAction SilentlyContinue),

        # If Recurse is set, groups will be created in the RDC document reprsenting each child organisational unit.
        #
        # Organizational units are only included as groups if the oganizational unit contains computer accounts or other organizational units.
        [Switch]$Recurse
    )

    if (-not $psboundparameters.ContainsKey('SearchBase')) {
        if ($candidateDN = Get-Variable parentDN -ValueOnly -ErrorAction SilentlyContinue) {
            $SearchBase = $candidateDN
        }
    }

    if ($pscmdlet.ParameterSetName -eq 'ByIdentity') {
        $params = @{
            Identity = $Identity
        }
    } elseif ($pscmdlet.ParameterSetName -eq 'ByName') {
        $params = @{
            Name        = $Name
            SearchBase  = $SearchBase
            SearchScope = 'Subtree'
        }
    } else {
        $params = @{
            Filter      = $Filter
            SearchBase  = $SearchBase
            SearchScope = 'OneLevel'
        }
    }

    $serverAndCredential = @{}
    if ($Server) {
        $serverAndCredential.Add('Server', $Server)
    }
    if ($Credential) {
        $serverAndCredential.Add('Credential', $Credential)
    }

    GetADOrganizationalUnit @params @serverAndCredential | ForEach-Object {
        # Determine if the OU has child objects. If so, allow it to be included.
        Write-Debug 'Searching for child computer objects'
        Write-Debug (' SearchBase: {0}' -f $_.DistinguishedName)

        $params = @{
            Filter        = '*'
            SearchBase    = $_.DistinguishedName
            SearchScope   = 'Subtree'
            ResultSetSize = 1
        }
        if (GetADComputer @params @serverAndCredential) {
            Write-Verbose ('Creating group {0}' -f $_.Name)

            $parentDN = $_.DistinguishedName
            if ($Recurse) {
                RdcGroup $_.Name {
                    RdcADGroup -Recurse -ComputerFilter $ComputerFilter @serverAndCredential
                    RdcADComputer -Filter $ComputerFilter @serverAndCredential
                }
            } else {
                RdcGroup $_.Name {
                    RdcADComputer -Filter $ComputerFilter @serverAndCredential -Recurse
                }
            }
        }
    }
}


function RdcComputer {
    <#
    .SYNOPSIS
        Create a computer in the RDCMan document.
    .DESCRIPTION
        Create a computer in the RDCMan document.
    #>


    [CmdletBinding(DefaultParameterSetName = 'FromPipeline')]
    param (
        [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'FromPipeline')]
        [String]$Name,

        [Parameter(Position = 2, ValueFromPipelineByPropertyName, ParameterSetName = 'FromPipeline')]
        [String]$DnsHostName,

        [Parameter(Position = 3, ValueFromPipelineByPropertyName, ParameterSetName = 'FromPipeline')]
        [Alias('IPv4Address')]
        [String]$Comment,

        [Parameter(Mandatory, Position = 1, ParameterSetName = 'FromHashtable')]
        [ValidateScript(
            {
                if (-not $_.ContainsKey('Name')) {
                    throw 'The Name key must be present'
                }
                foreach ($key in $_.Keys) {
                    if ($key -notin 'Name', 'DnsHostName', 'Comment') {
                        throw ('Invalid key in Properties hashtable. Valid keys are Name, DnsHostName, and Comment')
                    }
                }
                $true
            }
        )]
        [Hashtable]$Properties
    )

    begin {
        try {
            # Get the value of the parentNode variable from the parent scope(s)
            $parentNode = Get-Variable currentNode -ValueOnly -ErrorAction Stop
        } catch {
            throw ('{0} must be nested in RdcDocument or RdcGroup: {1}' -f $myinvocation.InvocationName, $_.Exception.Message)
        }
    }

    process {
        if ($Properties) {
            $Name = $Properties.Name
            $DnsHostName = $Properties.DnsHostName
            $Comment = $Properties.Comment
        }
        if (-not $DnsHostName) {
            $DnsHostName = $Name
        }

        $xElement = [System.Xml.Linq.XElement]('
            <server>
                <properties>
                    <displayname>{0}</displayname>
                    <name>{1}</name>
                    <comment>{2}</comment>
                </properties>
            </server>'
 -f $Name, $DnsHostName, $Comment)

        if ($parentNode -is [System.Xml.Linq.XDocument]) {
            $parentNode.Element('Rdc').Element('connected').AddBeforeSelf($xElement)
        } else {
            $parentNode.Element('properties').AddAfterSelf($xElement)
        }
    }
}


function RdcConfiguration {
    <#
    .SYNOPSIS
 
    .DESCRIPTION
        RdcConfiguration allows the generator behaviours to be defined using a node in the document.
    .EXAMPLE
        RdcDocument name {
            RdcConfiguration @{
                SearchMode = 'ADSI'
            }
        }
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [Hashtable]$Configuration
    )

    Set-RdcConfiguration @Configuration
}


function RdcDocument {
    <#
    .SYNOPSIS
        Declare an RDCMan document.
    .DESCRIPTION
        An RDC Document defines the basic document content and is the starting point for creating groups and computer elements.
    #>


    [CmdletBinding()]
    param (
        # The path to a file to save content.
        [Parameter(Mandatory, Position = 1)]
        [Alias('FileName', 'FullName')]
        [String]$Path,

        # A script block defining the content of the document.
        [Parameter(Mandatory, Position = 2)]
        [ScriptBlock]$Children
    )

    $xDocument = $currentNode = [System.Xml.Linq.XDocument]::Parse('
        <?xml version="1.0" encoding="utf-8"?>
        <Rdc programVersion="2.7" schemaVersion="3">
            <file>
                <credentialsProfiles />
                <properties>
                    <name>{0}</name>
                </properties>
            </file>
            <connected />
            <favorites />
            <recentlyUsed />
        </Rdc>'
.Trim() -f ([System.IO.FileInfo]$Path).BaseName)

    if ($Children) {
        & $Children
    }

    if ($Path -notmatch '\.rdg$') {
        $Path = '{0}.rdg' -f $Path
    }
    $Path = $pscmdlet.GetUnresolvedProviderPathFromPSPath($Path)
    $xDocument.Save($Path)
}


function RdcGroup {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, ValueFromPipeline, Position = 1)]
        [String]$Name,

        [Parameter(Mandatory, Position = 2)]
        [ScriptBlock]$Children
    )

    try {
        # Get the value of the parentNode variable from the parent scope(s)
        $parentNode = Get-Variable currentNode -ValueOnly -ErrorAction Stop
    } catch {
        throw ('{0} must be nested in RdcDocument or RdcGroup: {1}' -f $myinvocation.InvocationName, $_.Exception.Message)
    }

    $xElement = $currentNode = [System.Xml.Linq.XElement]::new('group',
        [System.Xml.Linq.XElement]::new('properties',
            [System.Xml.Linq.XElement]::new('name', $Name)
        )
    )

    if ($parentNode -is [System.Xml.Linq.XDocument]) {
        $parentNode.Element('Rdc').Element('file').Add($xElement)
    } else {
        $parentNode.Add($xElement)
    }

    if ($Children) {
        & $Children
    }
}


function RdcLogonCredential {
    <#
    .SYNOPSIS
        Creates a node to save credentials in the parent group or document.
    .DESCRIPTION
        Creates a node to save credentials in the parent group or document.
    #>


    [CmdletBinding(DefaultParameterSetName = 'FromHashtable')]
    param (
        [Parameter(Position = 1, ParameterSetName = 'FromHashtable')]
        [ValidateScript(
            {
                if ($_.Contains('Password') -and $_['Password'] -isnot [SecureString]) {
                    throw 'Passwords must be stored as a secure string'
                }
                foreach ($key in $_.Keys) {
                    if ($key -notin 'Username', 'Password', 'Domain') {
                        throw ('Invalid key in the RdcLogonCredentials hashtable. Valid keys are UserName, Password, and Domain')
                    }
                }
                $true
            }
        )]
        [Hashtable]$CredentialHash,

        [Parameter(ParameterSetName = 'FromCredential')]
        [PSCredential]$Credential,

        [Switch]$SavePassword
    )

    try {
        # Get the value of the parentNode variable from the parent scope(s)
        $parentNode = Get-Variable currentNode -ValueOnly -ErrorAction Stop
    } catch {
        throw ('{0} must be nested in RdcDocument or RdcGroup: {1}' -f $myinvocation.InvocationName, $_.Exception.Message)
    }

    if ($Credential) {
        if ($Credential.Username.Contains('\')) {
            $domainName, $username = $Credential.UserName -split '\\', 2
        } else {
            $domainName = ''
            $userName = $Credential.UserName
        }
        $secureString = $Credential.Password
    } else {
        $domainName = $CredentialHash['Domain']
        $userName = $CredentialHash['UserName']
        $secureString = $CredentialHash['Password']
    }

    if ($secureString.Length -gt 0) {
        $encryptedHexString = $secureString | ConvertFrom-SecureString
        $bytes = for ($i = 0; $i -lt $encryptedHexString.Length; $i += 2) {
            [Convert]::ToByte(
                ('{0}{1}' -f $encryptedHexString[$i], $encryptedHexString[$i + 1]),
                16
            )
        }
        $encryptedPassword = [Convert]::ToBase64String($bytes)
    } else {
        $encryptedPassword = ''
    }

    # V2: BigInteger variation

    # Add-Type -AssemblyName System.Numerics
    # $bytes = [System.Numerics.BigInteger]::Parse(
    # ($secureString | ConvertFrom-SecureString),
    # 'HexNumber'
    # ).ToByteArray()
    # [Array]::Reverse($bytes)

    # $encryptedString = [Convert]::ToBase64String($bytes)

    # [RdcMan.Encryption]::DecryptString($encryptedString, [RdcMan.EncryptionSettings]::new())

    # V3: Decrypt and reencrypt

    # $encryptedString = [Convert]::ToBase64String(
    # [System.Security.Cryptography.ProtectedData]::Protect(
    # [System.Text.Encoding]::Unicode.GetBytes(
    # $Credential.GetNetworkCredential().Password
    # ),
    # $null,
    # 'CurrentUser'
    # )
    # )

    $xElement = [System.Xml.Linq.XElement]('
        <logonCredentials inherit="None">
            <profileName scope="Local">Custom</profileName>
            <userName>{0}</userName>
            <password>{1}</password>
            <domain>{2}</domain>
        </logonCredentials>'
 -f $username, $encryptedPassword, $domainName)

    if ($parentNode.NodeType -eq 'Document') {
        $propertiesElement = $parentNode.FirstNode.Element('file').Element('properties')
    }
    else {
        $propertiesElement = $parentNode.Element('properties')
    }
    $propertiesElement.AddAfterSelf($xElement)
}


function RdcRemoteDesktopSetting {
    <#
    .SYNOPSIS
        Creates a node to configure remote desktop settings in the parent group or document.
    .DESCRIPTION
        Creates a node to configure remote desktop settings in the parent group or document.
    #>


    [CmdletBinding(DefaultParameterSetName = 'FromHashtable')]
    param (
        # Remote Destkop Settings configuration.
        #
        # Remote destkop settings allows the following to be defined:
        #
        # - Size - A value in the form Horizontal x Vertical.
        # - SameSizeAsClientArea - True or False. Make the remote desktop area fill the client window pane.
        # - FullScreen - True or False. Make the remote desktop full screen.
        # - ColorDepth - By default 24. ColorDepth can be set to 8, 15, 16, 24, or 32.
        [Parameter(Position = 1, ParameterSetName = 'FromHashtable')]
        [ValidateScript(
            {
                foreach ($key in $_.Keys) {
                    if ($key -notin 'Size', 'SameSizeAsClientArea', 'FullScreen', 'ColorDepth') {
                        throw ('Invalid key in the RdcLogonCredentials hashtable. Valid keys are Size, SameSizeAsClientArea, FullScreen, and ColorDepth')
                    }
                }
                $true
            }
        )]
        [Hashtable]$SettingsHash
    )

    try {
        # Get the value of the parentNode variable from the parent scope(s)
        $parentNode = Get-Variable currentNode -ValueOnly -ErrorAction Stop
    } catch {
        throw ('{0} must be nested in RdcDocument or RdcGroup: {1}' -f $myinvocation.InvocationName, $_.Exception.Message)
    }

    $settings = @{
        Size                 = $null
        SameSizeAsClientArea = $false
        FullScreen           = $true
        ColorDepth           = 24
    }
    foreach ($setting in $SettingsHash.Keys) {
        $settings[$setting] = $settingsHash[$setting]
    }
    if ($SettingsHash.Contains('SameSizeAsClientArea') -and $SettingsHash['SameSizeAsClientArea']) {
        $settings['FullScreen'] = $false
    }

    if ($settings['ColorDepth'] -notin 8, 15, 16, 24, 32) {
        throw 'Invalid color depth. Valid values are 8, 15, 16, 24, and 32.'
    }
    if ($settings['Size'] -and $settings['Size'] -notmatch '^\d+ *x *\d+$') {
        throw 'Invalid desktop size. Sizes must be specified in the format "Horizontal x Vertical"'
    } elseif ($settings['Size'] -match '^(\d+) *x *(\d+)$') {
        # Ensure Size is formatted exactly as RdcMan expects it to be.
        $settings['Size'] = '{0} x {1}' -f $matches[1], $matches[2]
    }

    $xElement = [System.Xml.Linq.XElement]('
        <remoteDesktop inherit="None">
            <sameSizeAsClientArea>{0}</sameSizeAsClientArea>
            <fullScreen>{1}</fullScreen>
            <colorDepth>{2}</colorDepth>
        </remoteDesktop>'
 -f $settings['SameSizeAsClientArea'], $settings['FullScreen'], $settings['ColorDepth'])

    if (-not $settings['FullScreen'] -and $settings['Size']) {
        $null = $xElement.Element('remoteDestkop').AddFirst(
            [System.Xml.Linq.XElement]('<size>{0}</size>' -f $settings['Size'])
        )
    }

    if ($parentNode -is [System.Xml.Linq.XDocument]) {
        $parentNode.Element('Rdc').Element('file').Add($xElement)
    } else {
        $parentNode.Add($xElement)
    }
}


InitializeModule