-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fix-IntuneEnrollment.ps1
521 lines (453 loc) · 32.5 KB
/
Fix-IntuneEnrollment.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
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
<#PSScriptInfo
.VERSION 0.11
.GUID 715a6707-796c-445f-9e8a-8a0fffd778a4
.AUTHOR Rudy Ooms
.COMPANYNAME
.COPYRIGHT
.TAGS Windows, AutoPilot, Powershell
.LICENSEURI
.PROJECTURI https://www.github.com
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.RELEASENOTES
Version 0.1: Initial Release.
version 0.2: added way more checks and fixes
version 0.3: added the mdmurls check
version 0.8: added the IME service check and reinstall functionality and improved some error handling
Adam Gell - edits the comments and expanded the encoded code.
.PRIVATEDATA
#>
<#
.DESCRIPTION
.SYNOPSIS
GUI to fix intune sync issues.
MIT LICENSE
Copyright (c) 2022 Rudy Ooms
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
.DESCRIPTION
The goal of this script is to help with the troubleshooting of Attestation issues when enrolling your device with Autopilot for Pre-Provisioned deployments
.EXAMPLE
Blog post with examples and explanations @call4cloud.nl
.LINK
Online version: https://call4cloud.nl/
#>
function test-intunesyncerrors {
########
### Version 0.8 | This tool will check for missing Intune certificates, if the Certificate is in the wrong certificate store, and if the certificate has been expired
#######
#################################
#defining some functions first###
###################################
function fix-wrongstore {
$title = 'Fixing missing Certificate in the System Store'
$question = 'Are you sure you want to proceed?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
$progressPreference = 'silentlyContinue'
write-host "Exporting and Importing the Intune certificate to the proper Certificate Store" -foregroundcolor yellow
Invoke-WebRequest -Uri 'https://download.sysinternals.com/files/PSTools.zip' -OutFile 'pstools.zip'
Expand-Archive -Path 'pstools.zip' -DestinationPath "$env:TEMP\pstools" -force
Move-Item -Path "$env:TEMP\pstools\psexec.exe" -force
reg.exe ADD HKCU\Software\Sysinternals /v EulaAccepted /t REG_DWORD /d 1 /f | out-null
Start-Process -windowstyle hidden -FilePath "$env:TEMP\pstools\psexec.exe" -ArgumentList '-s cmd /c "powershell.exe -ExecutionPolicy Bypass -encodedcommand JABjAGUAcgB0AGkAZgBpAGMAYQB0AGUAIAA9ACAARwBlAHQALQBDAGgAaQBsAGQASQB0AGUAbQAgAC0AUABhAHQAaAAgAEMAZQByAHQAOgBcAEMAdQByAHIAZQBuAHQAdQBzAGUAcgBcAE0AeQBcAAoAJABwAGEAcwBzAHcAbwByAGQAPQAgACIAcwBlAGMAcgBlAHQAIgAgAHwAIABDAG8AbgB2AGUAcgB0AFQAbwAtAFMAZQBjAHUAcgBlAFMAdAByAGkAbgBnACAALQBBAHMAUABsAGEAaQBuAFQAZQB4AHQAIAAtAEYAbwByAGMAZQAKAEUAeABwAG8AcgB0AC0AUABmAHgAQwBlAHIAdABpAGYAaQBjAGEAdABlACAALQBDAGUAcgB0ACAAJABjAGUAcgB0AGkAZgBpAGMAYQB0AGUAIAAtAEYAaQBsAGUAUABhAHQAaAAgAGMAOgBcAGkAbgB0AHUAbgBlAC4AcABmAHgAIAAtAFAAYQBzAHMAdwBvAHIAZAAgACQAcABhAHMAcwB3AG8AcgBkAAoASQBtAHAAbwByAHQALQBQAGYAeABDAGUAcgB0AGkAZgBpAGMAYQB0AGUAIAAtAEUAeABwAG8AcgB0AGEAYgBsAGUAIAAtAFAAYQBzAHMAdwBvAHIAZAAgACQAcABhAHMAcwB3AG8AcgBkACAALQBDAGUAcgB0AFMAdABvAHIAZQBMAG8AYwBhAHQAaQBvAG4AIABDAGUAcgB0ADoAXABMAG8AYwBhAGwATQBhAGMAaABpAG4AZQBcAE0AeQAgAC0ARgBpAGwAZQBQAGEAdABoACAAYwA6AFwAaQBuAHQAdQBuAGUALgBwAGYAeAA="'
}
else {
Write-Host 'Exiting...' -foregroundcolor red
read-Host -prompt "Press any key to continue..."
exit
}
}
function check-certdate {
Write-Host "Checking If the Certificate hasn't expired" -foregroundcolor yellow
if ((Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq $thumbprint -and $_.NotAfter -lt (Get-Date) }) -eq $null) {
Write-Host "The Intune Device Certificate is not expired." -foregroundcolor green
}
else {
Write-Host "The Intune Device Certificate is EXPIRED!" -foregroundcolor red
fix-certificate
}
}
function check-intunecert {
if (Get-ChildItem Cert:\LocalMachine\My\ | where { $_.issuer -like "*Microsoft Intune MDM Device CA*" }) {
write-Host "Intune Device Certificate is in installed in the Local Machine Certificate store" -foregroundcolor green
}
else {
Write-Host "Intune device Certificate still seems to be missing... sorry!" -foregroundcolor red
}
}
function fix-certificate {
$title = 'Fixing the Intune Enrollment'
$question = 'Are you 100% sure you want to proceed?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
$progressPreference = 'silentlyContinue'
write-host "Trying to enroll your device into Intune or something else..." -foregroundcolor yellow
fix-mdmurls
Invoke-WebRequest -Uri 'https://download.sysinternals.com/files/PSTools.zip' -OutFile 'pstools.zip'
Expand-Archive -Path 'pstools.zip' -DestinationPath "$env:TEMP\pstools" -force
#Move-Item -Path "$env:TEMP\pstools\psexec.exe" -force
reg.exe ADD HKCU\Software\Sysinternals /v EulaAccepted /t REG_DWORD /d 1 /f | out-null
<# the encoded command below does the following...
$RegistryKeys = "HKLM:\SOFTWARE\Microsoft\Enrollments", "HKLM:\SOFTWARE\Microsoft\Enrollments\Status","HKLM:\SOFTWARE\Microsoft\EnterpriseResourceManager\Tracked", "HKLM:\SOFTWARE\Microsoft\PolicyManager\AdmxInstalled", "HKLM:\SOFTWARE\Microsoft\PolicyManager\Providers","HKLM:\SOFTWARE\Microsoft\Provisioning\OMADM\Accounts", "HKLM:\SOFTWARE\Microsoft\Provisioning\OMADM\Logger", "HKLM:\SOFTWARE\Microsoft\Provisioning\OMADM\Sessions"
$IntuneCert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {
$_.Issuer -match "Intune MDM"
} | Remove-Item
$EnrollmentID = Get-ScheduledTask | Where-Object {$_.TaskPath -like "*Microsoft*Windows*EnterpriseMgmt*"} | Select-Object -ExpandProperty TaskPath -Unique | Where-Object {$_ -like "*-*-*"} | Split-Path -Leaf
Get-ScheduledTask | Where-Object {$_.Taskpath -match $EnrollmentID} | Unregister-ScheduledTask -Confirm:$false
foreach ($Key in $RegistryKeys) {
if (Test-Path -Path $Key) {
get-ChildItem -Path $Key | Where-Object {$_.Name -match $EnrollmentID} | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue
}
foreach ($enrollment in $enrollmentid){
Get-ScheduledTask | Where-Object {$_.Taskpath -match $Enrollment} | Unregister-ScheduledTask -Confirm:$false
}
}
Start-Sleep -Seconds 5
$EnrollmentProcess = Start-Process -FilePath "C:\Windows\System32\DeviceEnroller.exe" -ArgumentList "/C /AutoenrollMDM" -NoNewWindow -Wait -PassThru
#>
$enroll = Start-Process -windowstyle hidden -FilePath "$env:TEMP\pstools\psexec.exe" -ArgumentList '-s cmd /c "powershell.exe -ExecutionPolicy Bypass -encodedcommand JABSAGUAZwBpAHMAdA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QA6ACQAZgBhAGwAcwBlAAoAfQAgAAkACgAJAAkACQB9AAoACQAJAAkAUwB0AGEAcgB0AC0AUwBsAGUAZQBwACAALQBTAGUAYwBvAG4AZABzACAANQAKACQARQBuAHIAbwBsAGwAbQBlAG4AdABQAHIAbwBjAGUAcwBzACAAPQAgAFMAdABhAHIAdAAtAFAAcgBvAGMAZQBzAHMAIAAtAEYAaQBsAGUAUABhAHQAaAAgACIAQwA6AFwAVwBpAG4AZABvAHcAcwBcAFMAeQBzAHQAZQBtADMAMgBcAEQAZQB2AGkAYwBlAEUAbgByAG8AbABsAGUAcgAuAGUAeABlACIAIAAtAEEAcgBnAHUAbQBlAG4AdABMAGkAcwB0ACAAIgAvAEMAIAAvAEEAdQB0AG8AZQBuAHIAbwBsAGwATQBEAE0AIgAgAC0ATgBvAE4AZQB3AFcAaQBuAGQAbwB3ACAALQBXAGEAaQB0ACAALQBQAGEAcwBzAFQAaAByAHUA"'
$enroll
write-host "`n"
write-host "Please give the OMA DM client some time (about 20 seconds)to sync and get your device enrolled into Intune" -foregroundcolor yellow
write-host "`n"
start-sleep -seconds 20
write-host "Checking the Intune Certificate Again!." -foregroundcolor yellow
check-intunecert
check-dmwapservice
Get-ScheduledTask | ? { $_.TaskName -eq 'Schedule #1 created by enrollment client' } | Start-ScheduledTask
start-sleep -seconds 10
$Shell = New-Object -ComObject Shell.Application
$Shell.open("intunemanagementextension://syncapp")
check-dmpcert
start-sleep -seconds 5
get-schedule1
read-Host -prompt "Press any key to continue..."
exit
}
else {
write-host "`n"
Write-Host 'You dont like me fixing it...? Fine...exiting now' -foregroundcolor red
exit 1
}
}
function fix-privatekey {
$title = 'Intune Private Key'
$question = 'Are you sure you want to fix the private key missing??'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host "List certificates without private key: " -NoNewline
$certsWithoutKey = Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.HasPrivateKey -eq $false }
if ($certsWithoutKey) {
Write-Host "V" -ForegroundColor Green
$Choice = $certsWithoutKey | Select-Object Subject, Issuer, NotAfter, ThumbPrint | Out-Gridview -Passthru
if ($Choice) {
Write-Host "Search private key for $($Choice.Thumbprint): " -NoNewline
$Output = certutil -repairstore my "$($Choice.Thumbprint)"
$Result = [regex]::match($output, "CertUtil: (.*)").Groups[1].Value
if ($Result -eq '-repairstore command completed successfully.') {
Write-Host "V" -ForegroundColor Green
}
else {
Write-Host $Result -ForegroundColor Red
}
}
else {
Write-Host "No choice was made." -ForegroundColor DarkYellow
}
}
else {
Write-Host "There were no certificates found without private key." -ForegroundColor DarkYellow
}
}
else {
Write-Host 'You cancelled the fix... why?' -foregroundcolor red
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
}
function get-privatekey {
if ((Get-ChildItem Cert:\LocalMachine\My | where { $_.Thumbprint -match $thumbprint }).HasPrivateKey) {
Write-Host "Nice.. your Intune Device Certificate still has its private key" -foregroundcolor green
}
else {
Write-Host "I guess we need to fix something because the certificate is missing its private key" -foregroundcolor red
fix-privatekey
}
}
function check-mdmlog {
Write-Host "Hold on a moment... Initializing a sync and checking the MDM logs for sync errors!" -foregroundcolor yellow
$Shell = New-Object -ComObject Shell.Application
$Shell.open("intunemanagementextension://syncapp")
start-sleep -seconds 5
Remove-Item -Path $env:TEMP\diag\* -Force -ErrorAction SilentlyContinue
Start-Process MdmDiagnosticsTool.exe -Wait -ArgumentList "-out $env:TEMP\diag\" -NoNewWindow
$checkmdmlog = Select-String -Path $env:TEMP\diag\MDMDiagReport.html -Pattern "The last sync failed"
if ($checkmdmlog -eq $null) {
Write-Host "Not detecting any sync errors in the MDM log" -foregroundcolor green
}
else {
Write-Host "It's a good thing you are running this script because you do have some Intune sync issues going on" -foregroundcolor red
}
}
function check-imelog {
$path = "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log"
If (Test-Path $path) {
$checklog = Select-String -Path 'C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log' -Pattern "Set MdmDeviceCertificate : $thumbprint"
if ($checklog -ne $null) {
Write-Host "The proper Intune certificate with $thumbprint is also mentioned in the IME" -foregroundcolor green
}
else {
$checklogzero = Select-String -Path 'C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log' -Pattern "Find 0 MDM certificates"
$firstline = $checklogzero | select-object -first 1
Write-Host "this is could not be a good thing... $firstline" -foregroundcolor red
}
}
Else { Write-Host "the log is missing... it seems the IME is not installed" -foregroundcolor red }
check-imeservice
}
function check-dmpcert {
write-host "`n"
write-host "Determing if the certificate mentioned in the SSLClientCertreference is also configured in the Enrollments part of the registry " -foregroundcolor yellow
try {
$ProviderRegistryPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Enrollments"
$ProviderPropertyName = "ProviderID"
$ProviderPropertyValue = "MS DM Server"
$GUID = (Get-ChildItem -Path Registry::$ProviderRegistryPath -Recurse -ErrorAction SilentlyContinue | ForEach-Object { if ((Get-ItemProperty -Name $ProviderPropertyName -Path $_.PSPath -ErrorAction SilentlyContinue | Get-ItemPropertyValue -Name $ProviderPropertyName -ErrorAction SilentlyContinue) -match $ProviderPropertyValue) { $_ } }).PSChildName
$cert = (Get-ChildItem Cert:\LocalMachine\My\ | where { $_.issuer -like "*Microsoft Intune MDM Device CA*" })
$certthumbprint = $cert.thumbprint
$certsubject = $cert.subject
$subject = $certsubject -replace "CN=", ""
}
catch {
Write-host "Failed to get guid for enrollment from registry, device doesnt seem enrolled?" -foregroundcolor red
}
if ((Get-ItemProperty -Path "HKLM:SOFTWARE\Microsoft\Enrollments\$guid").DMPCertThumbPrint -eq $certthumbprint) {
Write-Host "Great!!! The Intune Device Certificate with the Thumbprint $certthumbprint is configured in the registry Enrollments" -foregroundcolor green
}
else {
Write-Host "Intune Device Certificate is not configured in the Registry Enrollments" -foregroundcolor red
}
}
function get-sslclientcertreference {
try {
$ProviderRegistryPath = "HKLM:SOFTWARE\Microsoft\Provisioning\OMADM\Accounts\"
$ProviderPropertyName = "ServerVer"
$ProviderPropertyValue = "4.0"
$GUID = (Get-ChildItem -Path $ProviderRegistryPath -Recurse -ErrorAction SilentlyContinue | ForEach-Object { if ((Get-ItemProperty -Name $ProviderPropertyName -Path $_.PSPath -ErrorAction SilentlyContinue | Get-ItemPropertyValue -Name $ProviderPropertyName -ErrorAction SilentlyContinue) -match $ProviderPropertyValue) { $_ } }).PSChildName
$ssl = (Get-ItemProperty -Path "HKLM:SOFTWARE\Microsoft\Provisioning\OMADM\Accounts\$guid" -ErrorAction SilentlyContinue).sslclientcertreference
}
catch [System.Exception] {
Write-Error "Failed to get Enrollment GUID or SSL Client Reference for enrollment from registry, device doesnt seem enrolled or it needs a reboot first"
$result = $false
}
if ($ssl -eq $null) {
Write-Host "Thats weird, your device doesnt seem to be enrolled into Intune." -foregroundcolor red
}
else {
Write-Host "Device seems to be Enrolled into Intune... proceeding" -foregroundcolor green
}
}
function check-imeservice {
write-host "`n"
write-host "Determing if the IME service is succesfully installed" -foregroundcolor yellow
$path = "C:\Program Files (x86)\Microsoft Intune Management Extension\Microsoft.Management.Services.IntuneWindowsAgent.exe"
If (Test-Path $path) {
write-host "IntuneWindowsAgent.exe is available on the device"-foregroundcolor green
write-host "Going to check if the IME service is installed" -foregroundcolor yellow
$service = Get-Service -Name IntuneManagementExtension -ErrorAction SilentlyContinue
if ($service.Length -gt 0) {
Write-Host "the IME service seems to be installed!" -foregroundcolor green
}
else {
Write-Host "Mmm okay.. The IME software isn't installed" -foregroundcolor red
}
}
else {
write-host "IntuneWindowsAgent.exe seems to be missing, checking if its even installed" -foregroundcolor red
if ((Get-WmiObject -Class Win32_Product).caption -eq "Microsoft Intune Management Extension") {
Write-Host "the IME software seems to be installed!" -foregroundcolor green
}
else {
Write-Host "The IME software isn't installed" -foregroundcolor red
$title = 'Fixing the IME'
$question = 'Are you 100% sure you want to proceed?!!!!!'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
$ProviderRegistryPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\EnterpriseDesktopAppManagement\S-0-0-00-0000000000-0000000000-000000000-000\MSI"
$ProviderPropertyName = "CurrentDownloadUrl"
$ProviderPropertyValue = "*IntuneWindowsAgent.msi*"
$GUID = (Get-ChildItem -Path Registry::$ProviderRegistryPath -Recurse -ErrorAction SilentlyContinue | ForEach-Object { if ((Get-ItemProperty -Name $ProviderPropertyName -Path $_.PSPath -ErrorAction SilentlyContinue | Get-ItemPropertyValue -Name $ProviderPropertyName -ErrorAction SilentlyContinue) -like $ProviderPropertyValue) { $_ } }).pschildname | select-object -first 1
$link = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\EnterpriseDesktopAppManagement\S-0-0-00-0000000000-0000000000-000000000-000\MSI\$GUID
$link = $link.currentdownloadurl
Invoke-WebRequest -Uri $link -OutFile 'IntuneWindowsAgent.msi'
.\IntuneWindowsAgent.msi /quiet
}
else {
write-host "`n"
Write-Host 'Exiting now' -foregroundcolor red
}
}
}
}
function check-entdmid {
write-host "`n"
write-host "Determing if the certificate subject is also configured in the EntDMID key " -foregroundcolor yellow
try {
$ProviderRegistryPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Enrollments"
$ProviderPropertyName = "ProviderID"
$ProviderPropertyValue = "MS DM Server"
$GUID = (Get-ChildItem -Path Registry::$ProviderRegistryPath -Recurse -ErrorAction SilentlyContinue | ForEach-Object { if ((Get-ItemProperty -Name $ProviderPropertyName -Path $_.PSPath -ErrorAction SilentlyContinue | Get-ItemPropertyValue -Name $ProviderPropertyName -ErrorAction SilentlyContinue) -match $ProviderPropertyValue) { $_ } }).PSChildName
$cert = (Get-ChildItem Cert:\LocalMachine\My\ | where { $_.issuer -like "*Microsoft Intune MDM Device CA*" })
$certthumbprint = $cert.thumbprint
$certsubject = $cert.subject
$subject = $certsubject -replace "CN=", ""
}
catch {
Write-host "Failed to get guid for enrollment from registry, device doesnt seem enrolled?" -foregroundcolor red
}
if ((Get-ItemProperty -Path "HKLM:SOFTWARE\Microsoft\Enrollments\$guid\DMClient\MS DM Server").entdmid -eq $subject) {
Write-Host "I have good news!! The subject of the Intune Certificate is also set in the EntDMID registry key. Let's party!!!!" -foregroundcolor green
}
else {
Write-Host "The EntDMID key is not configured, you probably need to reboot the device and run the test again" -foregroundcolor red
}
}
function check-dmwapservice {
write-host "`n"
write-host "Determing if the dmwappushservice is running because we don't want to end up with no endpoints left to the endpointmapper" -foregroundcolor yellow
$ServiceName = "dmwappushservice"
$ServiceStatus = (Get-Service -Name $ServiceName).status
if ($ServiceStatus -eq "Running") {
Write-Host "I am happy...! The DMWAPPUSHSERVICE is Running!" -foregroundcolor green
}
else {
Write-Host "The DMWAPPUSHSERVICE isn't running, let's kickstart that damn service to speed up the enrollment! " -foregroundcolor red
Start-Service $Servicename -ErrorAction SilentlyContinue
}
}
function get-schedule1 {
write-host "Almost finished, checking if the EnterpriseMGT tasks are running to start the sync!" -foregroundcolor yellow
If ((Get-ScheduledTask | Where TaskName -eq 'Schedule #1 created by enrollment client').State -eq 'running') {
write-host "`n"
write-host "Enrollment task is running! It looks like I fixed your sync issues.I guess you owe me a membeer now!" -foregroundcolor green
}
elseif ((Get-ScheduledTask | Where TaskName -eq 'Schedule #1 created by enrollment client').State -eq 'ready') {
write-host "Enrollment task is ready!!!" -foregroundcolor green
}
else {
write-host "Enrollment task doesn't exist" -foregroundcolor red
}
}
function fix-mdmurls {
write-host "`n"
write-host "Determing if the required MDM enrollment urls are configured in the registry" -foregroundcolor yellow
$key = 'SYSTEM\CurrentControlSet\Control\CloudDomainJoin\TenantInfo\*'
$keyinfo = Get-Item "HKLM:\$key" -ErrorAction Ignore
$url = $keyinfo.name
$url = $url.Split("\")[-1]
$path = "HKLM:\SYSTEM\CurrentControlSet\Control\CloudDomainJoin\TenantInfo\$url"
if (test-path $path) {
$mdmurl = get-itemproperty -LiteralPath $path -Name 'MdmEnrollmentUrl'
$mdmurl = $mdmurl.mdmenrollmenturl
}
else {
write-host "I guess I am missing the proper tenantinfo" -foregroundcolor red
}
if ($mdmurl -eq "https://enrollment.manage.microsoft.com/enrollmentserver/discovery.svc") {
write-host "MDM Enrollment URLS are configured the way I like it!Nice!!" -foregroundcolor green
}
else {
write-host "MDM enrollment url's are missing! Let me get my wrench and fix it for you!" -foregroundcolor red
New-ItemProperty -LiteralPath $path -Name 'MdmEnrollmentUrl' -Value 'https://enrollment.manage.microsoft.com/enrollmentserver/discovery.svc' -PropertyType String -Force -ea SilentlyContinue;
New-ItemProperty -LiteralPath $path -Name 'MdmTermsOfUseUrl' -Value 'https://portal.manage.microsoft.com/TermsofUse.aspx' -PropertyType String -Force -ea SilentlyContinue;
New-ItemProperty -LiteralPath $path -Name 'MdmComplianceUrl' -Value 'https://portal.manage.microsoft.com/?portalAction=Compliance' -PropertyType String -Force -ea SilentlyContinue;
}
}
##################################################################
###############starting the reallll script########################
##################################################################
check-mdmlog
write-host "`n"
write-host "Determining if the device is enrolled and fetching the SSLClientCertReference registry key" -foregroundcolor yellow
try {
$ProviderRegistryPath = "HKLM:SOFTWARE\Microsoft\Provisioning\OMADM\Accounts\"
$ProviderPropertyName = "SslClientCertReference"
$GUID = (Get-ChildItem -Path $ProviderRegistryPath -Recurse -ErrorAction SilentlyContinue | ForEach-Object { if ((Get-ItemProperty -Name $ProviderPropertyName -Path $_.PSPath -ErrorAction SilentlyContinue | Get-ItemPropertyValue -Name $ProviderPropertyName -ErrorAction SilentlyContinue)) { $_ } }).PSChildName
$ssl = (Get-ItemProperty -Path "HKLM:SOFTWARE\Microsoft\Provisioning\OMADM\Accounts\$guid" -ErrorAction SilentlyContinue).sslclientcertreference
}
catch [System.Exception] {
Write-Error "Failed to get Enrollment GUID or SSL Client Reference for enrollment from registry... that's odd almost as if the Intune Certificate is gone"
$result = $false
}
if ($ssl -eq $null) {
Write-Host "Your device doesnt seem to be enrolled into Intune, lets find out why!" -foregroundcolor red
}
else {
Write-Host "Device seems to be Enrolled into Intune... proceeding" -foregroundcolor green
}
write-host "`n"
write-host "Checking the Certificate Prefix.. to find out if it is configured as SYSTEM or USER" -foregroundcolor yellow
try {
$thumbprintPrefix = "MY;System;"
$thumbprint = $ssl.Replace($thumbprintPrefix, "")
if ($ssl.StartsWith($thumbprintPrefix) -eq $true) {
write-host "The Intune Certificate Prefix is configured as $thumbprintprefix" -foregroundcolor green
write-host "`n"
write-host "Determing if the certificate is installed in the local machine certificate store" -foregroundcolor yellow
if (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq $thumbprint }) {
Write-Host "Intune Device Certificate is installed in the Local Machine Certificate store" -foregroundcolor green
write-host "`n"
check-certdate
write-host "`n"
write-host "Checking if the Certificate is also mentioned in the IME log" -foregroundcolor yellow
check-imelog
}
else {
Write-Host "Intune device Certificate is missing in the Local Machine Certificate store" -foregroundcolor red
fix-certificate
write-host "Running some tests to determine if the device has the SSLClientCertReference registry key configured!" -foregroundcolor yellow
get-sslclientcertreference
}
write-host "`n"
write-host "Determing if the certificate has a Private Key Attached" -foregroundcolor yellow
get-privatekey
check-dmpcert
}
else {
write-host "Damn... the SSL prefix is not configured as SYSTEM but as $SSL" -foregroundcolor red
$thumbprintPrefix = "MY;User;"
$thumbprint = $ssl.Replace($thumbprintPrefix, "")
write-host "`n"
write-host "Determing if the certificate is also not in the System Certificate Store" -foregroundcolor yellow
if (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Thumbprint -eq $thumbprint }) {
Write-Host "Intune Device Certificate is installed in the Local Machine Certificate store" -foregroundcolor green
write-host "`n"
check-certdate
write-host "`n"
write-host "Determing if the certificate has a Private Key Attached" -foregroundcolor yellow
get-privatekey
check-dmpcert
}
else {
Write-Host "Intune device Certificate is installed in the wrong user store. I will fix it for you!" -foregroundcolor red
fix-wrongstore
write-host "Determing if the certificate is now been installed in the proper store" -foregroundcolor yellow
check-intunecert
}
}
}
catch {
Write-host "Failed to get Intune Enrollment Guid from registry, device doesnt seem enrolled? Who cares?Let's fix it" -foregroundcolor red
fix-certificate
}
check-entdmid
}
test-intunesyncerrors