-
Notifications
You must be signed in to change notification settings - Fork 0
/
CertEnroll.ps1
303 lines (257 loc) · 11.4 KB
/
CertEnroll.ps1
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
<#
.SYNOPSIS
Performs the 802.1x machine cert enrollment to the targeted domain.
.DESCRIPTION
Port from BATCH script version adding more robust code with resiliency and active monitoring for the workstation cert enrollment.
.INPUTS
[N/A] No parameter required as issuing CA is per forest.
.OUTPUTS
n/a
.EXAMPLE
.\certenroll.ps1
.ISSUES
1) AD Replication must have been completed to the certificate server (hence 1 min retry loop for 30 min)
2) The certificate template name is hard coded
3) Dependency from external tool NTRights.exe that grants logon rights to 'Network Service'. Needs to be replaced by PowerShell equivalent.
.NOTES
- Name: CertEnroll
- Author: Diogo Catossi
- Version: 1.0
#>
Import-Module ActiveDirectory
$ErrorActionPreference = "SilentlyContinue"
#Set-PSDebug -Debug $true -Trace 2
###### Global Variables ######
$sScriptName = "CertEnroll"
$sScriptVersion = "1.0"
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$iEnrollAttempts = 20
$waitTime = 1 #minutes
[ADSI]$EnrollSvcs = ""
#### Log #####
$Date = Get-Date -Format yyyyMMdd-hhmm
$sLogPath = (Get-Location).Path
$sLogFileName = "$sScriptName-$Date.log"
$sLogFile = Join-Path -path $slogpath -childpath $sLogFileName
$sHeader = ""
$iLogFileSize = 1024000 #1 MB
$sComputerName = $env:computername
###### Setting Error State Variables ######
$ErrorState = 0
$Error.Clear()
##################################################
# Begin Functions
##################################################
Function Write-Log()
{
<#
.SYNOPSIS
Writes A Given Message To The Specified Log File
.DESCRIPTION
Writes a message to the specified log file
Return: What was written to the log
.PARAMETER sMessage
Message to write to the log file
.PARAMETER iTabs
Number of tabs to indent text
.PARAMETER sFileName
Name of the log file
.INPUTS
[-sLogPath] <String> Path of the log file to write
[-sLogFileName] <String> Filename of log to write
[-sMessage] <String> Content to write to the log file
[-iTabs] <Int32> Number of tabs to append at the beginning of the line
.OUTPUTS
<String> What was written to the log
.EXAMPLE
Write-Log -sLogPath "C:\" -sLogFileName "test_task2.log" -sMessage "The message is ....." -iTabs 0
.NOTES
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, HelpMessage = "Log Path")]
[Alias("LogPath")]
[String]$sLogPath,
[Parameter(Mandatory = $true, HelpMessage = "Log File Name")]
[Alias("LogName")]
[String]$sLogFileName,
[Parameter(Mandatory = $true, HelpMessage = "Log Text")]
[Alias("LogText", "LogMessage")]
[String]$sMessage,
[Parameter(Mandatory = $false, HelpMessage = "Tabs at left")]
[Alias("Tabs")]
[Int]$iTabs = 0
)
#Function's main 'Try'
Try
{
#Loop through tabs provided to see If text should be indented within file
$sTabs = ""
For ($a = 1; $a -le $iTabs; $a++) { $sTabs = $sTabs + "`t" }
#Populated content with tabs and message
$sContent = $sTabs + $sMessage
#Define $sLogFile with the full file name
$sLogFile = Join-Path -Path $sLogPath -Childpath $sLogFileName
#Write contect to the file and If debug is on, to the console for troubleshooting
Try
{
$sContent | Out-File $sLogFile -Append
}
Catch
{
$sContent = "ERROR: Log File '$sLogFile' could NOT be appended."
}
#Write to host when $global:bDebug is $true
If ($global:bDebug) { Write-host $sContent }
}
Catch { throw "Major failure. Error`: $($Error[0].Exception)" }
} #End Of Write-Log
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-#
########################################################################################################################
# Begin Main
########################################################################################################################
try
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "##########################################################################" -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " Begin of $sScriptName`: $(Get-Date)" -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "##########################################################################" -iTabs 0
$Action = "Defining Template"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " " -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "#### Action: $Action" -iTabs 0
#ATENTION: CertTemplate should have no spaces"
[string]$sCertTemplate = ""
$domain = (Get-ADDomain).DNSRoot
#Validates Production domain(s)/Forest(s)
if ($domain.ToUpper().Contains("MYFOREST.COM"))
{
$sCertTemplate = "MYCERTTEMPLATE"
[ADSI]$EnrollSvcs = "LDAP://CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,DC=MYFOREST,DC=com"
}
#Validates LAB domain(s)/Forest(s)
elseif ($domain.ToUpper().Contains("MYLABFOREST.COM"))
{
$sCertTemplate = "MYLABCERTTEMPLATE"
[ADSI]$EnrollSvcs = "LDAP://CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,DC=MYLABFOREST,DC=com"
}
else
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Error in action: $Action. Computer is not member of a valid forest. Exiting" -iTabs 2
$ErrorState = 3
}
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Domain: $env:MachDomain" -iTabs 1
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Cert Template: $sCertTemplate" -iTabs 1
$Action = "Getting network information"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " " -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "#### Action: $Action" -iTabs 0
Get-NetIPConfiguration -Detailed | Out-File $sLogFile -Append
$Action = "'NT Authority\Network Service' logon permission"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " " -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "#### Action: $Action" -iTabs 0
try
{
#Adds Network service authority rights to logon locally and perform certificate enrollment during OSD build.
#TODO: troubleshoot group not found error using powershell method.
#$return = Add-LocalGroupMember -Group "Remote Support" -Member "NT Authority\Network Service" -ErrorAction Stop
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Full command: $scriptDir\ntrights.exe +r SeNetworkLogonRight -u `"NT AUTHORITY\NETWORK SERVICE`" 2>&1" -iTabs 0
$result = & $scriptDir\ntrights.exe +r SeNetworkLogonRight -u "NT AUTHORITY\NETWORK SERVICE" 2>&1 #OLD METHOD
If ($LASTEXITCODE -eq 0)
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "$result" -iTabs 1
}
Else
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Error code: $LASTEXITCODE, Message: $result" -iTabs
throw $result
}
}
<#catch [Microsoft.PowerShell.Commands.MemberExistsException]
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "NT Authority\Network Service is already member of Remote Support local group." -iTabs 1
}#>
catch
{
$message = "Could not add NT Authority\Network Service network logon rights. Aborting. Exception: $($Error[0].ToString())"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage $message -iTabs 2
throw $message
}
try
{
:EnrollLoop for ($i = 0; $i -lt $iEnrollAttempts; $i++)
{
$Action = "Test AD"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " " -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "#### Action: $Action connectivity" -iTabs 0
if ($EnrollSvcs.Children) #Checks if ADSI instance is reachable
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Enrollment services are reachable" -iTabs 1
#Parse enrollment services from AD Forest for available CAs
$Action = "Parse EnrollSvcs"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " " -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "#### Action: $Action for published CAs that contain required template." -iTabs 0
foreach ($child in $EnrollSvcs.Children)
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Validating: $($child.displayName)" -iTabs 1
#Verifies if the CA has equivalent cert template published
if ($child.certificateTemplates.Contains($sCertTemplate))
{
$Action = "Enrollment"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " " -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "#### Action: Start $Action" -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "CA: $($child.displayName)" -iTabs 1
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "DN: $($child.distinguishedName)" -iTabs 1
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Templates: $($child.certificateTemplates)" -iTabs 1
#Verifies if the CA is available.
if (Test-Connection $child.dNSHostName)
{
Try
{
#Performs the enrollment
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Requesting certificate..." -iTabs 1
$result = Get-Certificate -Template $sCertTemplate -Url "ldap:///$($child.distinguishedName)" -CertStoreLocation "Cert:\LocalMachine\My\"
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Enrollment Status: $($result.Status)" -iTabs 1
$ErrorState = 0
break EnrollLoop
}
catch
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Critical failure enrolling to Workstation certificate: $($Error[0].toString())" -iTabs 2
$ErrorState = 1
}
}
else
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "CA $($ca.Server) unavailable." -iTabs 3
$ErrorState = 1
}
}
}
}
else
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Could not reach current AD forest." -iTabs 3
$ErrorState = 1
}
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Retrying in $waitTime minute(s)." -iTabs 1
Start-Sleep (60 * $waitTime)
}
}
catch
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Certificate enrollment ERROR in action $Action`: $($Error[0].toString())" -iTabs 3
#[System.Windows.MessageBox]::Show("Certificate enrollment ERROR: $($Error[0].toString())")
$ErrorState = 1
}
return $ErrorState
}
catch
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "Certificate enrollment ERROR in action $Action`: $($Error[0].toString())" -iTabs 3
#[System.Windows.MessageBox]::Show("Certificate enrollment ERROR: $($Error[0].toString())")
return 1
}
finally
{
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "##########################################################################" -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage " End of $sScriptName`: $(Get-Date)" -iTabs 0
Write-Log -sLogPath $sLogPath -sLogFileName $sLogFileName -sMessage "##########################################################################" -iTabs 0
}