-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathMSFT_ADDomainController.psm1
More file actions
953 lines (784 loc) · 36 KB
/
MSFT_ADDomainController.psm1
File metadata and controls
953 lines (784 loc) · 36 KB
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
$resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$modulesFolderPath = Join-Path -Path $resourceModulePath -ChildPath 'Modules'
$aDCommonModulePath = Join-Path -Path $modulesFolderPath -ChildPath 'ActiveDirectoryDsc.Common'
Import-Module -Name $aDCommonModulePath
$dscResourceCommonModulePath = Join-Path -Path $modulesFolderPath -ChildPath 'DscResource.Common'
Import-Module -Name $dscResourceCommonModulePath
$script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
<#
.SYNOPSIS
Returns the current state of the domain controller.
.PARAMETER DomainName
Provide the FQDN of the domain the Domain Controller is being added to.
.PARAMETER Credential
Specifies the credential for the account used to install the domain controller.
This account must have permission to access the other domain controllers
in the domain to be able replicate domain information.
.PARAMETER SafemodeAdministratorPassword
Provide a password that will be used to set the DSRM password. This is a PSCredential.
.NOTES
Used Functions:
Name | Module
------------------------------------------------|--------------------------
Get-ADDomain | ActiveDirectory
Get-ADDomainControllerPasswordReplicationPolicy | ActiveDirectory
Get-DomainControllerObject | ActiveDirectoryDsc.Common
Assert-Module | DscResource.Common
New-ObjectNotFoundException | DscResource.Common
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DomainName,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SafemodeAdministratorPassword
)
Assert-Module -ModuleName 'ActiveDirectory'
Write-Verbose -Message ($script:localizedData.ResolveDomainName -f $DomainName)
try
{
$domain = Get-ADDomain -Identity $DomainName -Credential $Credential
}
catch
{
$errorMessage = $script:localizedData.MissingDomain -f $DomainName
New-ObjectNotFoundException -Message $errorMessage -ErrorRecord $_
}
Write-Verbose -Message ($script:localizedData.DomainPresent -f $DomainName)
$domainControllerObject = Get-DomainControllerObject `
-DomainName $DomainName -ComputerName $env:COMPUTERNAME -Credential $Credential
if ($domainControllerObject)
{
Write-Verbose -Message ($script:localizedData.IsDomainController -f
$domainControllerObject.Name, $domainControllerObject.Domain)
$allowedPasswordReplicationAccountName = (
Get-ADDomainControllerPasswordReplicationPolicy -Allowed -Identity $domainControllerObject |
ForEach-Object -MemberName sAMAccountName)
$deniedPasswordReplicationAccountName = (
Get-ADDomainControllerPasswordReplicationPolicy -Denied -Identity $domainControllerObject |
ForEach-Object -MemberName sAMAccountName)
$serviceNTDS = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\NTDS\Parameters'
$serviceNETLOGON = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters'
$installDns = [System.Boolean](Get-Service -Name dns -ErrorAction SilentlyContinue)
$targetResource = @{
AllowPasswordReplicationAccountName = @($allowedPasswordReplicationAccountName)
Credential = $Credential
DatabasePath = $serviceNTDS.'DSA Working Directory'
DenyPasswordReplicationAccountName = @($deniedPasswordReplicationAccountName)
DomainName = $domainControllerObject.Domain
Ensure = $true
FlexibleSingleMasterOperationRole = @($domainControllerObject.OperationMasterRoles)
InstallationMediaPath = $null
InstallDns = $installDns
IsGlobalCatalog = $domainControllerObject.IsGlobalCatalog
LogPath = $serviceNTDS.'Database log files path'
ReadOnlyReplica = $domainControllerObject.IsReadOnly
SafemodeAdministratorPassword = $SafemodeAdministratorPassword
SiteName = $domainControllerObject.Site
SysvolPath = $serviceNETLOGON.SysVol -replace '\\sysvol$', ''
}
}
else
{
Write-Verbose -Message ($script:localizedData.NotDomainController -f $env:COMPUTERNAME)
$targetResource = @{
AllowPasswordReplicationAccountName = $null
Credential = $Credential
DatabasePath = $null
DenyPasswordReplicationAccountName = $null
DomainName = $DomainName
Ensure = $false
FlexibleSingleMasterOperationRole = $null
InstallationMediaPath = $null
InstallDns = $false
IsGlobalCatalog = $false
LogPath = $null
ReadOnlyReplica = $false
SafemodeAdministratorPassword = $SafemodeAdministratorPassword
SiteName = $null
SysvolPath = $null
}
}
return $targetResource
}
<#
.SYNOPSIS
Installs, or change properties on, a domain controller.
.PARAMETER DomainName
Provide the FQDN of the domain the Domain Controller is being added to.
.PARAMETER Credential
Specifies the credential for the account used to install the domain controller.
This account must have permission to access the other domain controllers
in the domain to be able replicate domain information.
.PARAMETER SafemodeAdministratorPassword
Provide a password that will be used to set the DSRM password. This is a PSCredential.
.PARAMETER Ensure
Specifies if the node will be configured as a domain controller
.PARAMETER DatabasePath
Provide the path where the NTDS.dit will be created and stored.
.PARAMETER LogPath
Provide the path where the logs for the NTDS will be created and stored.
.PARAMETER SysvolPath
Provide the path where the Sysvol will be created and stored.
.PARAMETER SiteName
Provide the name of the site you want the Domain Controller to be added to.
.PARAMETER InstallationMediaPath
Provide the path for the IFM folder that was created with ntdsutil.
This should not be on a share but locally to the Domain Controller being promoted.
.PARAMETER IsGlobalCatalog
Specifies if the domain controller will be a Global Catalog (GC).
.PARAMETER ReadOnlyReplica
Specifies if the domain controller should be provisioned as read-only domain controller
.PARAMETER AllowPasswordReplicationAccountName
Provides a list of the users, computers, and groups to add to the password replication allowed list.
.PARAMETER DenyPasswordReplicationAccountName
Provides a list of the users, computers, and groups to add to the password replication denied list.
.PARAMETER FlexibleSingleMasterOperationRole
Specifies one or more Flexible Single Master Operation (FSMO) roles to
move to this domain controller. The current owner must be online and
responding for the move to be allowed.
.PARAMETER InstallDns
Specifies if the DNS Server service should be installed and configured on
the domain controller. If this is not set the default value of the parameter
InstallDns of the cmdlet Install-ADDSDomainController is used.
The parameter `InstallDns` is only used during the provisioning of a domain
controller. The parameter cannot be used to install or uninstall the DNS
server on an already provisioned domain controller.
.NOTES
Used Functions:
Name | Module
---------------------------------------------------|--------------------------
Install-ADDSDomainController | ActiveDirectory
Test-ADDSDomainControllerUninstallation | ActiveDirectory
Uninstall-ADDSDomainController | ActiveDirectory
Get-ADDomain | ActiveDirectory
Get-ADForest | ActiveDirectory
Set-ADObject | ActiveDirectory
Move-ADDirectoryServer | ActiveDirectory
Move-ADDirectoryServerOperationMasterRole | ActiveDirectory
Remove-ADDomainControllerPasswordReplicationPolicy | ActiveDirectory
Add-ADDomainControllerPasswordReplicationPolicy | ActiveDirectory
Get-DomainControllerObject | ActiveDirectoryDsc.Common
New-InvalidOperationException | DscResource.Common
#>
function Set-TargetResource
{
<#
Suppressing this rule because $global:DSCMachineStatus is used to
trigger a reboot for the one that was suppressed when calling
Install-ADDSDomainController.
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPlainTextForPassword', '',
Justification = 'Read-Only Domain Controller (RODC) Creation support(AllowPasswordReplicationAccountName and DenyPasswordReplicationAccountName)')]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DomainName,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SafemodeAdministratorPassword,
[Parameter()]
[ValidateSet('Absent', 'Present')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[System.String]
$DatabasePath,
[Parameter()]
[System.String]
$LogPath,
[Parameter()]
[System.String]
$SysvolPath,
[Parameter()]
[System.String]
$SiteName,
[Parameter()]
[System.String]
$InstallationMediaPath,
[Parameter()]
[System.Boolean]
$IsGlobalCatalog,
[Parameter()]
[System.Boolean]
$ReadOnlyReplica,
[Parameter()]
[System.String[]]
$AllowPasswordReplicationAccountName,
[Parameter()]
[System.String[]]
$DenyPasswordReplicationAccountName,
[Parameter()]
[ValidateSet('DomainNamingMaster', 'SchemaMaster', 'InfrastructureMaster', 'PDCEmulator', 'RIDMaster')]
[System.String[]]
$FlexibleSingleMasterOperationRole,
[Parameter()]
[System.Boolean]
$InstallDns
)
$getTargetResourceParameters = @{
DomainName = $DomainName
Credential = $Credential
SafeModeAdministratorPassword = $SafemodeAdministratorPassword
}
$targetResource = Get-TargetResource @getTargetResourceParameters
if ($targetResource.Ensure)
{
$ensureValue = 'Present'
}
else
{
$ensureValue = 'Absent'
}
if ($Ensure -eq 'Absent')
{
if ($targetResource.Ensure -eq $false)
{
break
}
# Test to make sure the domain controller can be removed from the domain
$ADDSDomainUninstallationParameters = @{
LocalAdministratorPassword = $SafemodeAdministratorPassword.Password
Credential = $Credential
NoRebootOnCompletion = $true
Force = $true
}
$testStatus = Test-ADDSDomainControllerUninstallation @ADDSDomainUninstallationParameters
if ($testStatus.Status -eq 'Error')
{
New-InvalidOperationException -Message ($script:localizedData.TestDemoteStatus -f $testStatus.Status, $testStatus.Message)
}
elseif ($testStatus.Status -eq 'Success')
{
# No issues found that will cause issues demoting the domain controller
try
{
Uninstall-ADDSDomainController @ADDSDomainUninstallationParameters -ErrorAction Stop
Write-Verbose -Message ($script:localizedData.Demoted -f $env:COMPUTERNAME)
}
catch
{
$errorMessage = $script:localizedData.FailedToDemote
New-InvalidResultException -Message $errorMessage -ErrorRecord $_
}
<#
Signal to the LCM to reboot the node to compensate for the one we
suppressed from Uninstall-ADDSDomainController
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '',
Justification = 'Set LCM DSCMachineStatus to indicate reboot required')]
$global:DSCMachineStatus = 1
}
}
elseif ($targetResource.Ensure -eq $false)
{
Write-Verbose -Message ($script:localizedData.Promoting -f $env:COMPUTERNAME, $DomainName)
# Node is not a domain controller so we promote it.
$installADDSDomainControllerParameters = @{
DomainName = $DomainName
SafeModeAdministratorPassword = $SafemodeAdministratorPassword.Password
Credential = $Credential
NoRebootOnCompletion = $true
Force = $true
}
if ($PSBoundParameters.ContainsKey('ReadOnlyReplica') -and $ReadOnlyReplica -eq $true)
{
if (-not $PSBoundParameters.ContainsKey('SiteName'))
{
New-InvalidOperationException -Message $script:localizedData.RODCMissingSite
}
$installADDSDomainControllerParameters.Add('ReadOnlyReplica', $true)
}
if ($PSBoundParameters.ContainsKey('AllowPasswordReplicationAccountName'))
{
$installADDSDomainControllerParameters.Add('AllowPasswordReplicationAccountName',
$AllowPasswordReplicationAccountName)
}
if ($PSBoundParameters.ContainsKey('DenyPasswordReplicationAccountName'))
{
$installADDSDomainControllerParameters.Add('DenyPasswordReplicationAccountName',
$DenyPasswordReplicationAccountName)
}
if ($PSBoundParameters.ContainsKey('DatabasePath'))
{
$installADDSDomainControllerParameters.Add('DatabasePath', $DatabasePath)
}
if ($PSBoundParameters.ContainsKey('LogPath'))
{
$installADDSDomainControllerParameters.Add('LogPath', $LogPath)
}
if ($PSBoundParameters.ContainsKey('SysvolPath'))
{
$installADDSDomainControllerParameters.Add('SysvolPath', $SysvolPath)
}
if ($PSBoundParameters.ContainsKey('SiteName') -and $SiteName)
{
$installADDSDomainControllerParameters.Add('SiteName', $SiteName)
}
if ($PSBoundParameters.ContainsKey('IsGlobalCatalog') -and $IsGlobalCatalog -eq $false)
{
$installADDSDomainControllerParameters.Add('NoGlobalCatalog', $true)
}
if ($PSBoundParameters.ContainsKey('InstallDns'))
{
$installADDSDomainControllerParameters.Add('InstallDns', $InstallDns)
}
if (-not [System.String]::IsNullOrWhiteSpace($InstallationMediaPath))
{
$installADDSDomainControllerParameters.Add('InstallationMediaPath', $InstallationMediaPath)
}
Install-ADDSDomainController @installADDSDomainControllerParameters
Write-Verbose -Message ($script:localizedData.Promoted -f $env:COMPUTERNAME, $DomainName)
<#
Signal to the LCM to reboot the node to compensate for the one we
suppressed from Install-ADDSDomainController
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '',
Justification = 'Set LCM DSCMachineStatus to indicate reboot required')]
$global:DSCMachineStatus = 1
}
elseif ($targetResource.Ensure)
{
# Node is a domain controller. We check if other properties are in desired state
Write-Verbose -Message ($script:localizedData.IsDomainController -f $env:COMPUTERNAME, $DomainName)
$domainControllerObject = Get-DomainControllerObject `
-DomainName $DomainName -ComputerName $env:COMPUTERNAME -Credential $Credential
# Check if Node Global Catalog state is correct
if ($PSBoundParameters.ContainsKey('IsGlobalCatalog') -and
$targetResource.IsGlobalCatalog -ne $IsGlobalCatalog)
{
# DC is not in the expected Global Catalog state
if ($IsGlobalCatalog)
{
$globalCatalogOptionValue = 1
Write-Verbose -Message $script:localizedData.AddGlobalCatalog
}
else
{
$globalCatalogOptionValue = 0
Write-Verbose -Message $script:localizedData.RemoveGlobalCatalog
}
Set-ADObject -Identity $domainControllerObject.NTDSSettingsObjectDN -Replace @{
options = $globalCatalogOptionValue
}
}
if ($PSBoundParameters.ContainsKey('SiteName') -and $targetResource.SiteName -ne $SiteName)
{
# DC is not in correct site. Move it.
Write-Verbose -Message ($script:localizedData.MovingDomainController -f
$targetResource.SiteName, $SiteName)
Move-ADDirectoryServer -Identity $env:COMPUTERNAME -Site $SiteName -Credential $Credential
}
if ($PSBoundParameters.ContainsKey('AllowPasswordReplicationAccountName'))
{
$testMembersParameters = @{
ExistingMembers = $targetResource.AllowPasswordReplicationAccountName
Members = $AllowPasswordReplicationAccountName
}
if (-not (Test-Members @testMembersParameters))
{
Write-Verbose -Message (
$script:localizedData.AllowedSyncAccountsMismatch -f
($targetResource.AllowPasswordReplicationAccountName -join ';'),
($AllowPasswordReplicationAccountName -join ';')
)
$getMembersToAddAndRemoveParameters = @{
DesiredMembers = $AllowPasswordReplicationAccountName
CurrentMembers = $targetResource.AllowPasswordReplicationAccountName
}
$getMembersToAddAndRemoveResult = Get-MembersToAddAndRemove @getMembersToAddAndRemoveParameters
$adPrincipalsToRemove = $getMembersToAddAndRemoveResult.MembersToRemove
$adPrincipalsToAdd = $getMembersToAddAndRemoveResult.MembersToAdd
if ($null -ne $adPrincipalsToRemove)
{
$removeADPasswordReplicationPolicy = @{
Identity = $domainControllerObject
AllowedList = $adPrincipalsToRemove
}
Remove-ADDomainControllerPasswordReplicationPolicy @removeADPasswordReplicationPolicy `
-Confirm:$false
}
if ($null -ne $adPrincipalsToAdd)
{
$addADPasswordReplicationPolicy = @{
Identity = $domainControllerObject
AllowedList = $adPrincipalsToAdd
}
Add-ADDomainControllerPasswordReplicationPolicy @addADPasswordReplicationPolicy
}
}
}
if ($PSBoundParameters.ContainsKey('DenyPasswordReplicationAccountName'))
{
$testMembersParameters = @{
ExistingMembers = $targetResource.DenyPasswordReplicationAccountName
Members = $DenyPasswordReplicationAccountName;
}
if (-not (Test-Members @testMembersParameters))
{
Write-Verbose -Message (
$script:localizedData.DenySyncAccountsMismatch -f
($targetResource.DenyPasswordReplicationAccountName -join ';'),
($DenyPasswordReplicationAccountName -join ';')
)
$getMembersToAddAndRemoveParameters = @{
DesiredMembers = $DenyPasswordReplicationAccountName
CurrentMembers = $targetResource.DenyPasswordReplicationAccountName
}
$getMembersToAddAndRemoveResult = Get-MembersToAddAndRemove @getMembersToAddAndRemoveParameters
$adPrincipalsToRemove = $getMembersToAddAndRemoveResult.MembersToRemove
$adPrincipalsToAdd = $getMembersToAddAndRemoveResult.MembersToAdd
if ($null -ne $adPrincipalsToRemove)
{
$removeADPasswordReplicationPolicy = @{
Identity = $domainControllerObject
DeniedList = $adPrincipalsToRemove
}
Remove-ADDomainControllerPasswordReplicationPolicy @removeADPasswordReplicationPolicy `
-Confirm:$false
}
if ($null -ne $adPrincipalsToAdd)
{
$addADPasswordReplicationPolicy = @{
Identity = $domainControllerObject
DeniedList = $adPrincipalsToAdd
}
Add-ADDomainControllerPasswordReplicationPolicy @addADPasswordReplicationPolicy
}
}
}
if ($PSBoundParameters.ContainsKey('FlexibleSingleMasterOperationRole'))
{
foreach ($desiredFlexibleSingleMasterOperationRole in $FlexibleSingleMasterOperationRole)
{
if ($desiredFlexibleSingleMasterOperationRole -notin $targetResource.FlexibleSingleMasterOperationRole)
{
switch ($desiredFlexibleSingleMasterOperationRole)
{
<#
Connect to any available domain controller to get the
current owner for the specific role.
#>
{ $_ -in @('DomainNamingMaster', 'SchemaMaster') }
{
$currentOwnerFullyQualifiedDomainName = (Get-ADForest).$_
}
{ $_ -in @('InfrastructureMaster', 'PDCEmulator', 'RIDMaster') }
{
$currentOwnerFullyQualifiedDomainName = (Get-ADDomain).$_
}
}
Write-Verbose -Message ($script:localizedData.MovingFlexibleSingleMasterOperationRole -f
$desiredFlexibleSingleMasterOperationRole, $currentOwnerFullyQualifiedDomainName)
<#
Using the object returned from Get-ADDomainController to handle
an issue with calling Move-ADDirectoryServerOperationMasterRole
with Fully Qualified Domain Name (FQDN) in the Identity parameter.
#>
$MoveADDirectoryServerOperationMasterRoleParameters = @{
Identity = $domainControllerObject
OperationMasterRole = $desiredFlexibleSingleMasterOperationRole
Server = $currentOwnerFullyQualifiedDomainName
ErrorAction = 'Stop'
}
Move-ADDirectoryServerOperationMasterRole @MoveADDirectoryServerOperationMasterRoleParameters
}
}
}
}
}
<#
.SYNOPSIS
Determines if the domain controller is in desired state.
.PARAMETER DomainName
Provide the FQDN of the domain the Domain Controller is being added to.
.PARAMETER Credential
Specifies the credential for the account used to install the domain controller.
This account must have permission to access the other domain controllers
in the domain to be able replicate domain information.
.PARAMETER SafemodeAdministratorPassword
Provide a password that will be used to set the DSRM password. This is a PSCredential.
.PARAMETER Ensure
Specifies if the node will be configured as a domain controller
.PARAMETER DatabasePath
Provide the path where the NTDS.dit will be created and stored.
.PARAMETER LogPath
Provide the path where the logs for the NTDS will be created and stored.
.PARAMETER SysvolPath
Provide the path where the Sysvol will be created and stored.
.PARAMETER SiteName
Provide the name of the site you want the Domain Controller to be added to.
.PARAMETER InstallationMediaPath
Provide the path for the IFM folder that was created with ntdsutil.
This should not be on a share but locally to the Domain Controller being promoted.
.PARAMETER IsGlobalCatalog
Specifies if the domain controller will be a Global Catalog (GC).
.PARAMETER ReadOnlyReplica
Specifies if the domain controller should be provisioned as read-only domain controller
.PARAMETER AllowPasswordReplicationAccountName
Provides a list of the users, computers, and groups to add to the password replication allowed list.
.PARAMETER DenyPasswordReplicationAccountName
Provides a list of the users, computers, and groups to add to the password replication denied list.
.PARAMETER FlexibleSingleMasterOperationRole
Specifies one or more Flexible Single Master Operation (FSMO) roles to
move to this domain controller. The current owner must be online and
responding for the move to be allowed.
.PARAMETER InstallDns
Specifies if the DNS Server service should be installed and configured on
the domain controller. If this is not set the default value of the parameter
InstallDns of the cmdlet Install-ADDSDomainController is used.
The parameter `InstallDns` is only used during the provisioning of a domain
controller. The parameter cannot be used to install or uninstall the DNS
server on an already provisioned domain controller.
Not used in Test-TargetResource.
.NOTES
Used Functions:
Name | Module
------------------------------|--------------------------
Test-ADReplicationSite | ActiveDirectoryDsc.Common
Test-Members | ActiveDirectoryDsc.Common
New-InvalidOperationException | DscResource.Common
New-ObjectNotFoundException | DscResource.Common
#>
function Test-TargetResource
{
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "",
Justification = 'Read-Only Domain Controller (RODC) Creation support($AllowPasswordReplicationAccountName and DenyPasswordReplicationAccountName)')]
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DomainName,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SafemodeAdministratorPassword,
[Parameter()]
[ValidateSet('Absent', 'Present')]
[System.String]
$Ensure = 'Present',
[Parameter()]
[System.String]
$DatabasePath,
[Parameter()]
[System.String]
$LogPath,
[Parameter()]
[System.String]
$SysvolPath,
[Parameter()]
[System.String]
$SiteName,
[Parameter()]
[System.String]
$InstallationMediaPath,
[Parameter()]
[System.Boolean]
$IsGlobalCatalog,
[Parameter()]
[System.Boolean]
$ReadOnlyReplica,
[Parameter()]
[System.String[]]
$AllowPasswordReplicationAccountName,
[Parameter()]
[System.String[]]
$DenyPasswordReplicationAccountName,
[Parameter()]
[ValidateSet('DomainNamingMaster', 'SchemaMaster', 'InfrastructureMaster', 'PDCEmulator', 'RIDMaster')]
[System.String[]]
$FlexibleSingleMasterOperationRole,
[Parameter()]
[System.Boolean]
$InstallDns
)
Write-Verbose -Message ($script:localizedData.TestingConfiguration -f $env:COMPUTERNAME, $DomainName)
if ($PSBoundParameters.ContainsKey('ReadOnlyReplica') -and $ReadOnlyReplica -eq $true)
{
if (-not $PSBoundParameters.ContainsKey('SiteName'))
{
New-InvalidOperationException -Message $script:localizedData.RODCMissingSite
}
}
if ($PSBoundParameters.ContainsKey('SiteName'))
{
if (-not (Test-ADReplicationSite -SiteName $SiteName -DomainName $DomainName -Credential $Credential))
{
$errorMessage = $script:localizedData.FailedToFindSite -f $SiteName, $DomainName
New-ObjectNotFoundException -Message $errorMessage
}
}
$getTargetResourceParameters = @{
DomainName = $DomainName
Credential = $Credential
SafeModeAdministratorPassword = $SafemodeAdministratorPassword
}
$existingResource = Get-TargetResource @getTargetResourceParameters
$testTargetResourceReturnValue = $existingResource.Ensure
if ($testTargetResourceReturnValue)
{
$ensureValue = 'Present'
}
else
{
$ensureValue = 'Absent'
}
if ($ensureValue -ne $Ensure)
{
Write-Verbose -Message ($script:localizedData.EnsureMismatch -f $ensureValue, $Ensure)
$testTargetResourceReturnValue = $false
}
if ($PSBoundParameters.ContainsKey('ReadOnlyReplica') -and $ReadOnlyReplica)
{
if ($testTargetResourceReturnValue -and -not $existingResource.ReadOnlyReplica)
{
New-InvalidOperationException -Message $script:localizedData.CannotConvertToRODC
}
}
if ($PSBoundParameters.ContainsKey('SiteName') -and $existingResource.SiteName -ne $SiteName)
{
Write-Verbose -Message ($script:localizedData.WrongSite -f $existingResource.SiteName, $SiteName)
$testTargetResourceReturnValue = $false
}
# Check Global Catalog Config
if ($PSBoundParameters.ContainsKey('IsGlobalCatalog') -and $existingResource.IsGlobalCatalog -ne $IsGlobalCatalog)
{
if ($IsGlobalCatalog)
{
Write-Verbose -Message ($script:localizedData.ExpectedGlobalCatalogEnabled -f
$existingResource.SiteName, $SiteName)
}
else
{
Write-Verbose -Message ($script:localizedData.ExpectedGlobalCatalogDisabled -f
$existingResource.SiteName, $SiteName)
}
$testTargetResourceReturnValue = $false
}
if ($PSBoundParameters.ContainsKey('AllowPasswordReplicationAccountName') -and
$null -ne $existingResource.AllowPasswordReplicationAccountName)
{
$testMembersParameters = @{
ExistingMembers = $existingResource.AllowPasswordReplicationAccountName
Members = $AllowPasswordReplicationAccountName
}
if (-not (Test-Members @testMembersParameters))
{
Write-Verbose -Message (
$script:localizedData.AllowedSyncAccountsMismatch -f
($existingResource.AllowPasswordReplicationAccountName -join ';'),
($AllowPasswordReplicationAccountName -join ';')
)
$testTargetResourceReturnValue = $false
}
}
if ($PSBoundParameters.ContainsKey('DenyPasswordReplicationAccountName') -and
$null -ne $existingResource.DenyPasswordReplicationAccountName)
{
$testMembersParameters = @{
ExistingMembers = $existingResource.DenyPasswordReplicationAccountName
Members = $DenyPasswordReplicationAccountName;
}
if (-not (Test-Members @testMembersParameters))
{
Write-Verbose -Message (
$script:localizedData.DenySyncAccountsMismatch -f
($existingResource.DenyPasswordReplicationAccountName -join ';'),
($DenyPasswordReplicationAccountName -join ';')
)
$testTargetResourceReturnValue = $false
}
}
<#
Only evaluate Flexible Single Master Operation (FSMO) roles if the
node is already a domain controller.
#>
if ($PSBoundParameters.ContainsKey('FlexibleSingleMasterOperationRole') -and $existingResource.Ensure -eq $true)
{
$FlexibleSingleMasterOperationRole | ForEach-Object -Process {
if ($_ -notin $existingResource.FlexibleSingleMasterOperationRole)
{
Write-Verbose -Message ($script:localizedData.NotOwnerOfFlexibleSingleMasterOperationRole -f $_ )
$testTargetResourceReturnValue = $false
}
}
}
<#
If the node is not a domain controller and ensure is set to Absent we need to return
True as it will fail if other options are set on the resource.
#>
if ($ensureValue -eq 'Absent' -and $Ensure -eq 'Absent')
{
$testTargetResourceReturnValue = $true
}
return $testTargetResourceReturnValue
}
<#
.SYNOPSIS
Return a hashtable with members that are not present in CurrentMembers,
and members that are present add should not be present.
.PARAMETER DesiredMembers
Specifies the list of desired members in the hashtable.
.PARAMETER CurrentMembers
Specifies the list of current members in the hashtable.
.OUTPUTS
Returns a hashtable with two properties. The property MembersToAdd contains the
members as ADPrincipal objects that are not members in the collection
provided in $CurrentMembers. The property MembersToRemove contains the
unwanted members as ADPrincipal objects in the collection provided
in $CurrentMembers.
#>
function Get-MembersToAddAndRemove
{
param
(
[Parameter(Mandatory = $true)]
[AllowNull()]
[AllowEmptyCollection()]
[System.String[]]
$DesiredMembers,
[Parameter(Mandatory = $true)]
[AllowNull()]
[AllowEmptyCollection()]
[System.String[]]
$CurrentMembers
)
$principalsToRemove = foreach ($memberName in $CurrentMembers)
{
if ($memberName -notin $DesiredMembers)
{
New-Object -TypeName Microsoft.ActiveDirectory.Management.ADPrincipal -ArgumentList $memberName
}
}
$principalsToAdd = foreach ($memberName in $DesiredMembers)
{
if ($memberName -notin $CurrentMembers)
{
New-Object -TypeName Microsoft.ActiveDirectory.Management.ADPrincipal -ArgumentList $memberName
}
}
return @{
MembersToAdd = [Microsoft.ActiveDirectory.Management.ADPrincipal[]] $principalsToAdd
MembersToRemove = [Microsoft.ActiveDirectory.Management.ADPrincipal[]] $principalsToRemove
}
}
Export-ModuleMember -Function *-TargetResource