-
Notifications
You must be signed in to change notification settings - Fork 141
/
MSFT_xADDomainController.psm1
224 lines (185 loc) · 7 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
#
# xADDomainController: DSC resource to install a domain controller in Active
# Directory.
#
function Get-TargetResource
{
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory)]
[String]$DomainName,
[Parameter(Mandatory)]
[PSCredential]$DomainAdministratorCredential,
[Parameter(Mandatory)]
[PSCredential]$SafemodeAdministratorPassword,
[String]$DatabasePath,
[String]$LogPath,
[String]$SysvolPath,
[String]$SiteName
)
$returnValue = @{
DomainName = $DomainName
Ensure = $false
}
try
{
Write-Verbose -Message "Resolving '$($DomainName)' ..."
$domain = Get-ADDomain -Identity $DomainName -Credential $DomainAdministratorCredential
if ($domain -ne $null)
{
Write-Verbose -Message "Domain '$($DomainName)' is present. Looking for DCs ..."
try
{
$dc = Get-ADDomainController -Identity $env:COMPUTERNAME -Credential $DomainAdministratorCredential
Write-Verbose -Message "Found domain controller '$($dc.Name)' in domain '$($dc.Domain)'."
if ($dc.Domain -eq $DomainName)
{
Write-Verbose -Message "Current node '$($dc.Name)' is already a domain controller for domain '$($dc.Domain)'."
$serviceNTDS = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\NTDS\Parameters'
$serviceNETLOGON = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters'
$returnValue.Ensure = $true
$returnValue.DatabasePath = $serviceNTDS.'DSA Working Directory'
$returnValue.LogPath = $serviceNTDS.'Database log files path'
$returnValue.SysvolPath = $serviceNETLOGON.SysVol -replace '\\sysvol$', ''
$returnValue.SiteName = $dc.Site
}
}
catch
{
if ($error[0]) {Write-Verbose $error[0].Exception}
Write-Verbose -Message "Current node does not host a domain controller."
}
}
}
catch [System.Management.Automation.CommandNotFoundException]
{
if ($error[0]) {Write-Verbose $error[0].Exception}
Write-Verbose -Message "Current node is not running AD WS, and hence is not a domain controller."
}
$returnValue
}
function Set-TargetResource
{
param
(
[Parameter(Mandatory)]
[String]$DomainName,
[Parameter(Mandatory)]
[PSCredential]$DomainAdministratorCredential,
[Parameter(Mandatory)]
[PSCredential]$SafemodeAdministratorPassword,
[String]$DatabasePath,
[String]$LogPath,
[String]$SysvolPath,
[String]$SiteName
)
# Debug can pause Install-ADDSDomainController, so we remove it.
$parameters = $PSBoundParameters.Remove("Debug");
$targetResource = Get-TargetResource @PSBoundParameters
if ($targetResource.Ensure -eq $false)
{
## Node is not a domain controllr so we promote it
Write-Verbose -Message "Checking if domain '$($DomainName)' is present ..."
$domain = $null;
try
{
$domain = Get-ADDomain -Identity $DomainName -Credential $DomainAdministratorCredential
}
catch
{
if ($error[0]) {Write-Verbose $error[0].Exception}
throw (New-Object -TypeName System.InvalidOperationException -ArgumentList "Domain '$($DomainName)' could not be found.")
}
Write-Verbose -Message "Verified that domain '$($DomainName)' is present, continuing ..."
$params = @{
DomainName = $DomainName
SafeModeAdministratorPassword = $SafemodeAdministratorPassword.Password
Credential = $DomainAdministratorCredential
NoRebootOnCompletion = $true
Force = $true
}
if ($DatabasePath -ne $null)
{
$params.Add("DatabasePath", $DatabasePath)
}
if ($LogPath -ne $null)
{
$params.Add("LogPath", $LogPath)
}
if ($SysvolPath -ne $null)
{
$params.Add("SysvolPath", $SysvolPath)
}
if ($SiteName -ne $null -and $SiteName -ne "")
{
$params.Add("SiteName", $SiteName)
}
Install-ADDSDomainController @params
Write-Verbose -Message "Node is now a domain controller for '$($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
if ($PSBoundParameters["SiteName"] -and $targetResource.SiteName -ne $SiteName)
{
## DC is not in correct site. Move it.
Write-Verbose "Moving Domain Controller from '$($targetResource.SiteName)' to '$SiteName'"
Move-ADDirectoryServer -Identity $env:COMPUTERNAME -Site $SiteName -Credential $DomainAdministratorCredential
}
}
}
function Test-TargetResource
{
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory)]
[String]$DomainName,
[Parameter(Mandatory)]
[PSCredential]$DomainAdministratorCredential,
[Parameter(Mandatory)]
[PSCredential]$SafemodeAdministratorPassword,
[String]$DatabasePath,
[String]$LogPath,
[String]$SysvolPath,
[String]$SiteName
)
if ($PSBoundParameters.SiteName)
{
if (-not (Test-ADReplicationSite -SiteName $SiteName -DomainName $DomainName -Credential $DomainAdministratorCredential))
{
throw (New-Object -TypeName System.InvalidOperationException -ArgumentList "Site '$($SiteName)' could not be found.")
}
}
$isCompliant = $true
try
{
$parameters = $PSBoundParameters.Remove("Debug");
$existingResource = Get-TargetResource @PSBoundParameters
$isCompliant = $existingResource.Ensure
if ([System.String]::IsNullOrEmpty($SiteName))
{
#If SiteName is not specified confgiuration is compliant
}
elseif ($existingResource.SiteName -ne $SiteName)
{
Write-Verbose "Domain Controller Site is not in a desired state. Expected '$SiteName', actual '$($existingResource.SiteName)'"
$isCompliant = $false
}
}
catch
{
if ($error[0]) {Write-Verbose $error[0].Exception}
Write-Verbose -Message "Domain '$($DomainName)' is NOT present on the current node."
$isCompliant = $false
}
$isCompliant
}
## Import the common AD functions
$adCommonFunctions = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath '\MSFT_xADCommon\MSFT_xADCommon.ps1'
. $adCommonFunctions
Export-ModuleMember -Function *-TargetResource