-
Notifications
You must be signed in to change notification settings - Fork 141
/
MSFT_xADDomainController.psm1
765 lines (615 loc) · 27.6 KB
/
MSFT_xADDomainController.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
$script:resourceModulePath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
$script:modulesFolderPath = Join-Path -Path $script:resourceModulePath -ChildPath 'Modules'
$script:localizationModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'xActiveDirectory.Common'
Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath 'xActiveDirectory.Common.psm1')
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_xADDomainController'
<#
.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 DomainAdministrationCredential
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 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 InstallDns
Specifies if the Dns service will be installed or not. Default value is true.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$DomainName,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$DomainAdministratorCredential,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SafemodeAdministratorPassword,
[Parameter()]
[System.String]
$DatabasePath,
[Parameter()]
[System.String]
$LogPath,
[Parameter()]
[System.String]
$SysvolPath,
[Parameter()]
[System.String]
$SiteName,
[Parameter()]
[boolean]
$InstallDns
)
Assert-Module -ModuleName 'ActiveDirectory'
$getTargetResourceResult = @{
DomainName = $DomainName
DomainAdministratorCredential = $DomainAdministratorCredential
SafemodeAdministratorPassword = $SafemodeAdministratorPassword
Ensure = $false
IsGlobalCatalog = $false
ReadOnlyReplica = $false
AllowPasswordReplicationAccountName = $null
DenyPasswordReplicationAccountName = $null
}
Write-Verbose -Message (
$script:localizedData.ResolveDomainName -f $DomainName
)
try
{
$domain = Get-ADDomain -Identity $DomainName -Credential $DomainAdministratorCredential
}
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 $DomainAdministratorCredential
if ($domainControllerObject)
{
Write-Verbose -Message (
$script:localizedData.FoundDomainController -f $domainControllerObject.Name, $domainControllerObject.Domain
)
Write-Verbose -Message (
$script:localizedData.AlreadyDomainController -f $domainControllerObject.Name, $domainControllerObject.Domain
)
$allowedPasswordReplicationAccountName = [System.String[]] (Get-ADDomainControllerPasswordReplicationPolicy -Allowed -Identity $domainControllerObject | ForEach-Object -MemberName sAMAccountName)
$deniedPasswordReplicationAccountName = [System.String[]] (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'
$getTargetResourceResult.Ensure = $true
$getTargetResourceResult.DatabasePath = $serviceNTDS.'DSA Working Directory'
$getTargetResourceResult.LogPath = $serviceNTDS.'Database log files path'
$getTargetResourceResult.SysvolPath = $serviceNETLOGON.SysVol -replace '\\sysvol$', ''
$getTargetResourceResult.SiteName = $domainControllerObject.Site
$getTargetResourceResult.IsGlobalCatalog = $domainControllerObject.IsGlobalCatalog
$getTargetResourceResult.DomainName = $domainControllerObject.Domain
$getTargetResourceResult.ReadOnlyReplica = $domainControllerObject.IsReadOnly
$getTargetResourceResult.AllowPasswordReplicationAccountName = $allowedPasswordReplicationAccountName
$getTargetResourceResult.DenyPasswordReplicationAccountName = $deniedPasswordReplicationAccountName
}
else
{
Write-Verbose -Message (
$script:localizedData.NotDomainController -f $env:COMPUTERNAME
)
}
return $getTargetResourceResult
}
<#
.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 DomainAdministrationCredential
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 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 InstallDns
Specifies if the Dns service will be installed or not. Default value is true.
#>
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', '')]
<#
Suppressing this rule because $global:DSCMachineStatus is only set,
never used (by design of Desired State Configuration).
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Scope='Function', Target='DSCMachineStatus')]
[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]
$DomainAdministratorCredential,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SafemodeAdministratorPassword,
[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()]
[boolean]
$InstallDns
)
$getTargetResourceParameters = @{} + $PSBoundParameters
$getTargetResourceParameters.Remove('InstallationMediaPath')
$getTargetResourceParameters.Remove('IsGlobalCatalog')
$getTargetResourceParameters.Remove('ReadOnlyReplica')
$getTargetResourceParameters.Remove('AllowPasswordReplicationAccountName')
$getTargetResourceParameters.Remove('DenyPasswordReplicationAccountName')
$targetResource = Get-TargetResource @getTargetResourceParameters
if ($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 = $DomainAdministratorCredential
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') -and $InstallDns -eq $false)
{
$installADDSDomainControllerParameters.Add('InstallDns', $false)
}
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
#>
$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 $DomainAdministratorCredential
# 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)
{
Write-Verbose -Message (
$script:localizedData.IsDomainController -f $targetResource.SiteName, $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 $DomainAdministratorCredential
}
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
}
}
}
}
}
<#
.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 DomainAdministrationCredential
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 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 InstallDns
Specifies if the Dns service will be installed or not. Default value is true.
#>
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]
$DomainAdministratorCredential,
[Parameter(Mandatory = $true)]
[System.Management.Automation.PSCredential]
$SafemodeAdministratorPassword,
[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()]
[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 $DomainAdministratorCredential))
{
$errorMessage = $script:localizedData.FailedToFindSite -f $SiteName, $DomainName
New-ObjectNotFoundException -Message $errorMessage
}
}
$getTargetResourceParameters = @{} + $PSBoundParameters
$getTargetResourceParameters.Remove('InstallationMediaPath')
$getTargetResourceParameters.Remove('IsGlobalCatalog')
$getTargetResourceParameters.Remove('ReadOnlyReplica')
$getTargetResourceParameters.Remove('AllowPasswordReplicationAccountName')
$getTargetResourceParameters.Remove('DenyPasswordReplicationAccountName')
$existingResource = Get-TargetResource @getTargetResourceParameters
$testTargetResourceReturnValue = $existingResource.Ensure
if ($PSBoundParameters.ContainsKey('ReadOnlyReplica') -and $ReadOnlyReplica)
{
if ($testTargetResourceReturnValue -and -not $testTargetResourceReturnValue.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
}
}
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 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.
.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