forked from Azure-Samples/DSRegTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSRegTool.ps1
3581 lines (3366 loc) · 206 KB
/
DSRegTool.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
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
DSRegTool V3.7 PowerShell script.
.DESCRIPTION
Device Registration Troubleshooter Tool is a PowerShell script that troubleshoots device registration common issues.
.AUTHOR:
Mohammad Zmaili
.EXAMPLE
.\DSRegTool.ps1
Enter (1) to troubleshoot Azure AD Register
Enter (2) to troubleshoot Azure AD Join device
Enter (3) to troubleshoot Hybrid Azure AD Join
Enter (4) to verify Service Connection Point (SCP)
Enter (5) to verify the health status of the device
Enter (6) to Verify Primary Refresh Token (PRT)
Enter (7) to collect the logs
Enter (Q) to Quit
#>
Function DSRegToolStart{
$ErrorActionPreference= 'silentlycontinue'
Write-Host "DSRegTool 3.7 has started" -ForegroundColor Yellow
$msg="Device Name : " + (Get-Childitem env:computername).value
Write-Host $msg -ForegroundColor Yellow
$msg="User Account: " + (whoami) +", UPN: "+$global:UserUPN
Write-Host $msg -ForegroundColor Yellow
}
Function Test-DevRegConnectivity($Write){
$ProxyTestFailed=$false
$winInetProxy=$false
$TestConnResult=@()
If($Write){Write-Host}
If($Write){Write-Host "Testing Internet Connectivity..." -ForegroundColor Yellow; Write-Log -Message "Testing Internet Connectivity..."}
$ErrorActionPreference= 'silentlycontinue'
$global:TestFailed=$false
$global:ProxyServer = checkProxy $Write
If($Write){Write-Host}
If($Write){Write-Host "Testing Device Registration Endpoints..." -ForegroundColor Yellow; Write-Log -Message "Testing Device Registration Endpoints..."}
if ($global:ProxyServer -ne "NoProxy"){
If($Write){Write-Host "Testing connection via winHTTP proxy..." -ForegroundColor Yellow; Write-Log -Message "Testing connection via winHTTP proxy..."}
if ($global:login){
$PSScript = "(Invoke-WebRequest -uri 'https://login.microsoftonline.com/common/oauth2' -UseBasicParsing).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
}else{
$PSScript = "(Invoke-WebRequest -uri 'https://login.microsoftonline.com/common/oauth2' -UseBasicParsing -Proxy $global:ProxyServer).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
}
if ($TestResult -eq 200){
If($Write){Write-Host "Connection to login.microsoftonline.com .............. Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to login.microsoftonline.com .............. Succeeded."}
$TestConnResult = $TestConnResult + "Connection to login.microsoftonline.com .............. Succeeded."
}else{
$ProxyTestFailed=$true
}
if ($global:device){
$PSScript = "(Invoke-WebRequest -uri 'https://device.login.microsoftonline.com/common/oauth2' -UseBasicParsing).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
}else{
$PSScript = "(Invoke-WebRequest -uri 'https://device.login.microsoftonline.com/common/oauth2' -UseBasicParsing -Proxy $global:ProxyServer).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
}
if ($TestResult -eq 200){
If($Write){Write-Host "Connection to device.login.microsoftonline.com ...... Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to device.login.microsoftonline.com ...... Succeeded."}
$TestConnResult = $TestConnResult + "Connection to device.login.microsoftonline.com ...... Succeeded."
}else{
$ProxyTestFailed=$true
}
if ($global:enterprise){
$PSScript = "(Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/$global:TenantName/discover?api-version=1.7' -UseBasicParsing -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
}else{
$PSScript = "(Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/$global:TenantName/discover?api-version=1.7' -UseBasicParsing -Proxy $global:ProxyServer -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
}
if ($TestResult -eq 200){
If($Write){Write-Host "Connection to enterpriseregistration.windows.net ..... Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to enterpriseregistration.windows.net ..... Succeeded."}
$TestConnResult = $TestConnResult + "Connection to enterpriseregistration.windows.net ..... Succeeded."
}else{
$ProxyTestFailed=$true
}
}
if (($global:ProxyServer -eq "NoProxy") -or ($ProxyTestFailed -eq $true)){
if($ProxyTestFailed -eq $true){
If($Write){Write-host "Connection failed via winHTTP, trying winInet..."; Write-Log -Message "Connection failed via winHTTP, trying winInet..." -Level WARN}
If($Write){Write-Host ''}
}else{
If($Write){Write-host "Testing connection via winInet..." -ForegroundColor Yellow; Write-Log -Message "Testing connection via winInet..."}
If($Write){Write-Host ''}
}
$PSScript = "(Invoke-WebRequest -uri 'https://login.microsoftonline.com/common/oauth2' -UseBasicParsing).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
if ($TestResult -eq 200){
$winInetProxy=$true
If($Write){Write-Host "Connection to login.microsoftonline.com .............. Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to login.microsoftonline.com .............. Succeeded."}
$TestConnResult = $TestConnResult + "Connection to login.microsoftonline.com .............. Succeeded."
}else{
$global:TestFailed=$true
If($Write){Write-Host "Connection to login.microsoftonline.com ................. failed." -ForegroundColor Red; Write-Log -Message "Connection to login.microsoftonline.com ................. failed." -Level ERROR}
$TestConnResult = $TestConnResult + "Connection to login.microsoftonline.com ................. failed."
}
$PSScript = "(Invoke-WebRequest -uri 'https://device.login.microsoftonline.com/common/oauth2' -UseBasicParsing).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
if ($TestResult -eq 200){
$winInetProxy=$true
If($Write){Write-Host "Connection to device.login.microsoftonline.com ...... Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to device.login.microsoftonline.com ...... Succeeded."}
$TestConnResult = $TestConnResult + "Connection to device.login.microsoftonline.com ...... Succeeded."
}else{
$global:TestFailed=$true
If($Write){Write-Host "Connection to device.login.microsoftonline.com .......... failed." -ForegroundColor Red; Write-Log -Message "Connection to device.login.microsoftonline.com .......... failed." -Level ERROR}
$TestConnResult = $TestConnResult + "Connection to device.login.microsoftonline.com .......... failed."
}
$PSScript = "(Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/$global:TenantName/discover?api-version=1.7' -UseBasicParsing -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode"
$TestResult = RunPScript -PSScript $PSScript
if ($TestResult -eq 200){
$winInetProxy=$true
If($Write){Write-Host "Connection to enterpriseregistration.windows.net ..... Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to enterpriseregistration.windows.net ..... Succeeded."}
$TestConnResult = $TestConnResult + "Connection to enterpriseregistration.windows.net ..... Succeeded."
}else{
$global:TestFailed=$true
If($Write){Write-Host "Connection to enterpriseregistration.windows.net ........ failed." -ForegroundColor Red; Write-Log -Message "Connection to enterpriseregistration.windows.net ........ failed." -Level ERROR}
$TestConnResult = $TestConnResult + "Connection to enterpriseregistration.windows.net ........ failed."
}
}
# If test failed
if ($Write){
if ($global:TestFailed){
Write-Host ''
Write-Host ''
Write-Host "Test failed: device is not able to communicate with MS endpoints under system account" -ForegroundColor red
Write-Log -Message "Test failed: device is not able to communicate with MS endpoints under system account" -Level ERROR
Write-Host ''
Write-Host "Recommended actions: " -ForegroundColor Yellow
Write-Host "- Make sure that the device is able to communicate with the above MS endpoints successfully under the system account." -ForegroundColor Yellow
Write-Host "- If the organization requires access to the internet via an outbound proxy, it is recommended to implement Web Proxy Auto-Discovery (WPAD)." -ForegroundColor Yellow
Write-Host "- If you don't use WPAD, you can configure proxy settings with GPO by deploying WinHTTP Proxy Settings on your computers beginning with Windows 10 1709." -ForegroundColor Yellow
Write-Host "- If the organization requires access to the internet via an authenticated outbound proxy, make sure that Windows 10 computers can successfully authenticate to the outbound proxy using the machine context." -ForegroundColor Yellow
Write-Log -Message "Recommended actions:
- Make sure that the device is able to communicate with the above MS endpoints successfully under the system account.
- If the organization requires access to the internet via an outbound proxy, it is recommended to implement Web Proxy Auto-Discovery (WPAD).
- If you don't use WPAD, you can configure proxy settings with GPO by deploying WinHTTP Proxy Settings on your computers beginning with Windows 10 1709.
- If the organization requires access to the internet via an authenticated outbound proxy, make sure that Windows 10 computers can successfully authenticate to the outbound proxy using the machine context."
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}else{
Write-Host ''
Write-Host "Test passed: Device is able to communicate with MS endpoints successfully under system context" -ForegroundColor Green
Write-Log -Message "Test passed: Device is able to communicate with MS endpoints successfully under system context"
}
}
If ($winInetProxy){$global:ProxyServer="winInet"}
return $TestConnResult
}
Function Test-DevRegConnectivity-User($Write){
$ErrorActionPreference= 'silentlycontinue'
$global:TestFailed=$false
$TestConnResult=@()
If($Write){Write-Host}
If($Write){Write-Host "Testing Internet Connectivity..." -ForegroundColor Yellow; Write-Log -Message "Testing Internet Connectivity..."}
$InternetConn1=$true
$InternetConn2=$true
$InternetConn3=$true
#$TestResult = (Test-NetConnection -ComputerName login.microsoftonline.com -Port 443).TcpTestSucceeded
$TestResult = (Invoke-WebRequest -uri 'https://login.microsoftonline.com/common/oauth2' -UseBasicParsing).StatusCode
if ($TestResult -eq 200){
If($Write){Write-Host "Connection to login.microsoftonline.com .............. Succeeded." -ForegroundColor Green; Write-Log -Message "Connection to login.microsoftonline.com .............. Succeeded."}
$TestConnResult = $TestConnResult + "Connection to login.microsoftonline.com .............. Succeeded."
}else{
If($Write){Write-Host "Connection to login.microsoftonline.com ................. failed." -ForegroundColor Red; Write-Log -Message "Connection to login.microsoftonline.com ................. failed." -Level ERROR}
$TestConnResult = $TestConnResult + "Connection to login.microsoftonline.com ................. failed."
$InternetConn1=$false
$global:TestFailed=$true
}
#$TestResult = (Test-NetConnection -ComputerName device.login.microsoftonline.com -Port 443).TcpTestSucceeded
$TestResult = (Invoke-WebRequest -uri 'https://device.login.microsoftonline.com/common/oauth2' -UseBasicParsing).StatusCode
if ($TestResult -eq 200){
If($Write){Write-Host "Connection to device.login.microsoftonline.com ...... Succeeded." -ForegroundColor Green ;Write-Log -Message "Connection to device.login.microsoftonline.com ...... Succeeded."}
$TestConnResult = $TestConnResult + "Connection to device.login.microsoftonline.com ...... Succeeded."
}else{
If($Write){Write-Host "Connection to device.login.microsoftonline.com .......... failed." -ForegroundColor Red ;Write-Log -Message "Connection to device.login.microsoftonline.com .......... failed." -Level ERROR}
$TestConnResult = $TestConnResult + "Connection to device.login.microsoftonline.com .......... failed."
$InternetConn2=$false
$global:TestFailed=$true
}
#$TestResult = (Test-NetConnection -ComputerName enterpriseregistration.windows.net -Port 443).TcpTestSucceeded
$TestResult = (Invoke-WebRequest -uri 'https://enterpriseregistration.windows.net/microsoft.com/discover?api-version=1.7' -UseBasicParsing -Headers @{'Accept' = 'application/json'; 'ocp-adrs-client-name' = 'dsreg'; 'ocp-adrs-client-version' = '10'}).StatusCode
if ($TestResult -eq 200){
If($Write){Write-Host "Connection to enterpriseregistration.windows.net ..... Succeeded." -ForegroundColor Green ;Write-Log -Message "Connection to enterpriseregistration.windows.net ..... Succeeded."}
$TestConnResult = $TestConnResult + "Connection to enterpriseregistration.windows.net ..... Succeeded."
}else{
If($Write){Write-Host "Connection to enterpriseregistration.windows.net ........ failed." -ForegroundColor Red ;Write-Log -Message "Connection to enterpriseregistration.windows.net ........ failed." -Level ERROR}
$TestConnResult = $TestConnResult + "Connection to enterpriseregistration.windows.net ........ failed."
$InternetConn3=$false
$global:TestFailed=$true
}
if ($Write){
if ($global:TestFailed){
Write-Host "Test failed: user is not able to communicate with MS endpoints" -ForegroundColor red ;Write-Log -Message "Test failed: user is not able to communicate with MS endpoints" -Level ERROR
Write-Host ''
Write-Host "Recommended action: make sure that the user is able to communicate with the above MS endpoints successfully" -ForegroundColor Yellow; Write-Log -Message "Recommended action: make sure that the user is able to communicate with the above MS endpoints successfully"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green; Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}else{
Write-Host ''
Write-Host "Test passed: User is able to communicate with MS endpoints successfully" -ForegroundColor Green ;Write-Log -Message "Test passed: User is able to communicate with MS endpoints successfully"
}
}
return $TestConnResult
}
Function CheckePRT{
Write-Host ''
Write-Host "Checking Enterprise PRT..." -ForegroundColor Yellow
Write-Log -Message "Checking Enterprise PRT..."
$ePRT = $DSReg | Select-String EnterprisePrt | select-object -First 1
$ePRT = ($ePRT.tostring() -split ":")[1].trim()
if ($ePRT -eq 'YES'){
$hostname = hostname
Write-Host $hostname "device does have Enterprise PRT" -ForegroundColor Green
Write-Log -Message "$hostname device does have Enterprise PRT"
}else{
$hostname = hostname
Write-Host $hostname "device does NOT have Enterprise PRT" -ForegroundColor Yellow
Write-Log -Message "$hostname device does NOT have Enterprise PRT" -Level WARN
}
}
Function Write-Log{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
[String] $Level = "INFO",
[Parameter(Mandatory=$True)]
[string] $Message,
[Parameter(Mandatory=$False)]
[string] $logfile = "DSRegTool.log"
)
if ($Message -eq " "){
Add-Content $logfile -Value " " -ErrorAction SilentlyContinue
}else{
$Date = (Get-Date).ToUniversalTime().ToString('yyyy-MM-dd HH:mm:ss.fff')
Add-Content $logfile -Value "[$date] [$Level] $Message" -ErrorAction SilentlyContinue
}
}
Function PSasAdmin{
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
Function Test-DevRegApp-old{
Write-Host ''
Write-Host "Testing Device Registration Service..." -ForegroundColor Yellow
Write-Log -Message "Testing Device Registration Service..."
if ((Get-MsolServicePrincipal -AppPrincipalId 01cb2876-7ebd-4aa4-9cc9-d28bd4d359a9).accountenabled){
Write-Host "Test passed: Device Registration Service is enabled on the tenant" -ForegroundColor Green
Write-Log -Message "Test passed: Device Registration Service is enabled on the tenant"
}else{
Write-Host "Test failed: Device Registration Service is disabled on the tenant" -ForegroundColor red
Write-Log -Message "Test failed: Device Registration Service is disabled on the tenant" -Level ERROR
Write-Host ''
Write-Host "Recommended action: enable Device Registration Service application on your tenant" -ForegroundColor Yellow
Write-Log -Message "Recommended action: enable Device Registration Service application on your tenant"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}
Function Test-DevRegApp{
Write-Host ''
Write-Host "Testing Device Registration Service..." -ForegroundColor Yellow
Write-Log -Message "Testing Device Registration Service..."
$headers = @{
'Content-Type' = "application\json"
'Authorization' = "Bearer $global:accesstoken"
}
$GraphLink = "https://graph.microsoft.com/v1.0/servicePrincipals?`$filter=appId eq '01cb2876-7ebd-4aa4-9cc9-d28bd4d359a9'"
$GraphResult=""
$GraphResult = (Invoke-WebRequest -Headers $Headers -Uri $GraphLink -UseBasicParsing -Method "GET" -ContentType "application/json").Content | ConvertFrom-Json
if ($GraphResult.value.accountenabled){
Write-Host "Test passed: Device Registration Service is enabled on the tenant" -ForegroundColor Green
Write-Log -Message "Test passed: Device Registration Service is enabled on the tenant"
}else{
Write-Host "Test failed: Device Registration Service is disabled on the tenant" -ForegroundColor red
Write-Log -Message "Test failed: Device Registration Service is disabled on the tenant" -Level ERROR
Write-Host ''
Write-Host "Recommended action: enable Device Registration Service application on your tenant" -ForegroundColor Yellow
Write-Log -Message "Recommended action: enable Device Registration Service application on your tenant"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}
Function SyncJoinCheck($Fallback){
if($Fallback){
Write-Host ''
Write-Host ''
Write-Host "Federated join flow failed, checking Sync join flow..."
Write-Log -Message "Federated join flow failed, checking Sync join flow..."
#Check OS version:
Write-Host ''
Write-Host "Testing OS version..." -ForegroundColor Yellow
Write-Log -Message "Testing OS version..."
$OSVersoin = ([environment]::OSVersion.Version).major
$OSBuild = ([environment]::OSVersion.Version).Build
if (($OSVersoin -ge 10) -and ($OSBuild -ge 17134)){#17134 build is 1803
$OSVer = (([environment]::OSVersion).Version).ToString()
Write-Host "Test passed: OS version supports fallback to sync join" -ForegroundColor Green
Write-Log -Message "Test passed: OS version supports fallback to sync join"
}else{
# dsregcmd will not work.
Write-Host "OS version does not support fallback to sync join, hence device registration will not complete" -ForegroundColor Red
Write-Log -Message "OS version does not support fallback to sync join, hence device registration will not complete" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Fallback to sync join enabled by default on 1803 version and above" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Fallback to sync join enabled by default on 1803 version and above"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
#Checking FallbackToSyncJoin enablement
Write-Host ''
Write-Host "Checking fallback to sync join configuration..." -ForegroundColor Yellow
Write-Log -Message "Checking fallback to sync join configuration..."
$reg=Get-ItemProperty -Path 'Registry::HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\CDJ' -ErrorAction SilentlyContinue
if ($reg.FallbackToSyncJoin -eq 0){
Write-Host "Test failed: Fallback to sync join is disabled" -ForegroundColor Red
Write-Log -Message "Test failed: Fallback to sync join is disabled" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Make sure that FallbackToSyncJoin is not disabled so that device fall back to sync join flow in case federated join flow failed" -ForegroundColor Yellow
Write-Host " This can be done by removing 'FallbackToSyncJoin' registry value under 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WorkplaceJoin'" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Make sure that FallbackToSyncJoin is not disabled so that device fall back to sync join flow in case federated join flow failed`n This can be done by removing 'FallbackToSyncJoin' registry value under 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WorkplaceJoin'"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}else{
Write-Host "Fallback to sync join is enabled" -ForegroundColor Green
Write-Log -Message "Fallback to sync join is enabled"
}
}
#Get device object GUID
$DN=([adsisearcher]"(&(objectCategory=computer)(objectClass=computer)(cn=$env:COMPUTERNAME))").findall().path
$OGuid = ([ADSI]$DN).ObjectGuid
$ComputerGUID=(new-object guid(,$OGuid[0])).Guid
#Checking userCert
Write-Host ''
Write-Host "Testing userCertificate attribute under AD computer object..." -ForegroundColor Yellow
Write-Log -Message "Testing userCertificate attribute under AD computer object..."
if($global:UserUPN.Length -ne 0){
$ValidUserCertExist=$false
$userCerts=([adsisearcher]"(&(name=$env:computername)(objectClass=computer))").findall().Properties.usercertificate
$userCertCount=$userCerts.count
if ($userCertCount -ge 1){
Write-Host "AD computer object has $userCertCount certificate(s) under userCertificate attribute" -ForegroundColor Green
Write-Log -Message "AD computer object has $userCertCount certificate(s) under userCertificate attribute"
Write-Host ''
Write-Host "Testing self-signed certificate validity..." -ForegroundColor Yellow
Write-Log -Message "Testing self-signed certificate validity..."
foreach ($userCert in $userCerts){
$userCert=(new-object X509Certificate(,$userCert))
$certSubject=($userCert.Subject.tostring() -split "CN=")[1].trim()
If ($certSubject -eq $ComputerGUID){
$ValidUserCertExist=$true
}
}
}else{
#No userCert exist
Write-Host "Test failed: There is no userCertificate under AD computer object" -ForegroundColor Red
Write-Log -Message "Test failed: There is no userCertificate under AD computer object" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Make sure to start device registration process, and the device has permission to write self-signed certificate under AD computer object" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Make sure to start device registration process, and the device has permission to write self-signed certificate under AD computer object"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
if ($ValidUserCertExist){
Write-Host "Test passed: AD computer object has a valid self-signed certificate" -ForegroundColor Green
Write-Log -Message "Test passed: AD computer object has a valid self-signed certificate"
}else{
Write-Host "Test failed: There is no valid self-signed certificate under AD computer object userCertificate attribute" -ForegroundColor Red
Write-Log -Message "Test failed: There is no valid self-signed certificate under AD computer object userCertificate attribute" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Make sure to start device registration process, and the device has permission to write self-signed certificate under AD computer object" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Make sure to start device registration process, and the device has permission to write self-signed certificate under AD computer object"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}else{
Write-Host "Test failed: signed in user is not a domain user, you should sign in with domain user to perform this test" -ForegroundColor Yellow
Write-Log -Message "Test failed: signed in user is not a domain user, you should sign in with domain user to perform this test" -Level WARN
}
#Checking if device synced
ConnecttoAzureAD
Write-Host ''
Write-Host "Testing if the device synced to Azure AD..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device synced to Azure AD..."
$headers = @{
'Content-Type' = "application\json"
'Authorization' = "Bearer $global:accesstoken"
}
$GraphLink = "https://graph.microsoft.com/v1.0/devices?`$filter=deviceId eq '$ComputerGUID'"
$GraphResult = Invoke-WebRequest -Headers $Headers -Uri $GraphLink -UseBasicParsing -Method "GET" -ContentType "application/json"
$AADDevice=$GraphResult.Content | ConvertFrom-Json
if($AADDevice.value.Count -ge 1){
#The device existing in AAD:
Write-Host "Test passed: the device object exists on Azure AD" -ForegroundColor Green
Write-Log -Message "Test passed: the device object exists on Azure AD"
}else{
#Device does not exist:
###Reregister device to AAD
Write-Host "Test failed: the device does not exist in your Azure AD tenant" -ForegroundColor Red
Write-Log -Message "Test failed: the device does not exist in your Azure AD tenant" -Level ERROR
$DeviceDN = ((([adsisearcher]"(&(name=$env:computername)(objectClass=computer))").findall().path).tostring() -split "LDAP://")[1].trim()
Write-Host ''
Write-Host "Recommended action: Make sure the device is in the sync scope, and it is successfully exported to Azure AD by Azure AD Connect." -ForegroundColor Yellow
Write-Host "Device DN: $DeviceDN" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Make sure the device is in the sync scope, and it is successfully exported to Azure AD by Azure AD Connect.`n Device DN: $DeviceDN"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}
Function CheckPRT{
#Check OS version:
Write-Host ''
Write-Host "Testing OS version..." -ForegroundColor Yellow
Write-Log -Message "Testing OS version..."
$OSVersoin = ([environment]::OSVersion.Version).major
$OSBuild = ([environment]::OSVersion.Version).Build
if (($OSVersoin -ge 10) -and ($OSBuild -ge 1511)){
$OSVer = (([environment]::OSVersion).Version).ToString()
Write-Host "Test passed: device has current OS version ($OSVer)" -ForegroundColor Green
Write-Log -Message "Test passed: device has current OS version ($OSVer)"
}else{
# dsregcmd will not work.
Write-Host "The device has a Windows down-level OS version" -ForegroundColor Red
Write-Log -Message "The device has a Windows down-level OS version" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Run this test on current OS versions e.g. Windows 10, Server 2016 and above" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Run this test on current OS versions e.g. Windows 10, Server 2016 and above"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
#Check dsregcmd status.
$DSReg = dsregcmd /status
Write-Host ''
Write-Host "Testing if the device is joined to the local domain..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is joined to the local domain..."
$DJ = $DSReg | Select-String DomainJoin
$DJ = ($DJ.tostring() -split ":")[1].trim()
if ($DJ -ne "YES"){
$hostname = hostname
Write-Host $hostname "device is NOT joined to the local domain" -ForegroundColor Yellow
Write-Log -Message "$hostname device is NOT joined to the local domain"
}else{
#The device is joined to the local domain.
$IS_DJ = $true
$DomainName = $DSReg | Select-String DomainName
$DomainName =($DomainName.tostring() -split ":")[1].trim()
$hostname = hostname
Write-Host $hostname "device is joined to the local domain:" $DomainName -ForegroundColor Yellow
Write-Log -Message "$hostname device is joined to the local domain: $DomainName"
}
#Checking if the device connected to AzureAD
if ($DJ -eq 'YES'){
#Check if the device is hybrid
Write-Host ''
Write-Host "Testing if the device is Hybrid Azure AD joined..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is Hybrid Azure AD joined..."
$AADJ = $DSReg | Select-String AzureAdJoined
$AADJ = ($AADJ.tostring() -split ":")[1].trim()
if ($AADJ -eq 'YES'){
#The device is hybrid
$hostname = hostname
Write-Host $hostname "device is Hybrid Azure AD joined" -ForegroundColor Green
Write-Log -Message "$hostname device is Hybrid Azure AD joined"
#CheckPRT value
Write-Host ''
Write-Host "Testing Azure AD PRT..." -ForegroundColor Yellow
Write-Log -Message "Testing Azure AD PRT..."
$ADPRT = $DSReg | Select-String AzureAdPrt | select-object -First 1
$ADPRT = ($ADPRT.tostring() -split ":")[1].Trim()
if ($ADPRT -eq 'YES'){
#PRT is available
Write-Host "Test passed: Azure AD PRT is available on this device for the looged on user" -ForegroundColor Green
Write-Log -Message "Test passed: Azure AD PRT is available on this device for the looged on user"
CheckePRT
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
}else{
#PRT not available
Write-Host "Test failed: Azure AD PRT is not available. Hence SSO will not work, and the device may be blocked if you have a device-based Conditional Access Policy" -ForegroundColor Red
Write-Log -Message "Test failed: Azure AD PRT is not available. Hence SSO will not work, and the device may be blocked if you have a device-based Conditional Access Policy" -Level ERROR
Write-Host ''
Write-Host "Recommended action: lock the device and unlock it and run the script again. If the issue remains, collect the logs and send them to MS support" -ForegroundColor Yellow
Write-Log -Message "Recommended action: lock the device and unlock it and run the script again. If the issue remains, collect the logs and send them to MS support"
CheckePRT
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}else{
$hostname = hostname
Write-Host $hostname "device is NOT Hybrid Azure AD joined" -ForegroundColor Yellow
Write-Log -Message "$hostname device is NOT Hybrid Azure AD joined"
#Check WPJ
Write-Host ''
Write-Host "Testing if the device is Azure AD Registered..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is Azure AD Registered..."
$WPJ = $DSReg | Select-String WorkplaceJoined | Select-Object -First 1
$WPJ = ($WPJ.tostring() -split ":")[1].trim()
if ($WPJ -eq 'YES'){
$hostname = hostname
Write-Host $hostname "device is Azure AD Registered" -ForegroundColor Green
Write-Log -Message "$hostname device is Azure AD Registered"
#Device is WPJ, check the registry
Write-Host ''
Write-Host "Testing Azure AD PRT registry value..." -ForegroundColor Yellow
if ((Get-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\AAD\Storage\https://login.microsoftonline.com -ErrorAction SilentlyContinue).PSPath){
Write-Host "Test passed: Azure AD PRT registry value exists for the looged on user" -ForegroundColor Green
Write-Log -Message "Test passed: Azure AD PRT registry value exists for the looged on user"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
}else{
Write-Host "Test failed: Azure AD PRT registry value does not exist for the looged on user" -ForegroundColor Red
Write-Log -Message "Test failed: Azure AD PRT registry value does not exist for the looged on user" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Disconnect the device from Azure AD form 'settings > Accounts > Access work or school' and then connect it again to Azure AD" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Disconnect the device from Azure AD form 'settings > Accounts > Access work or school' and then connect it again to Azure AD"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}else{
$hostname = hostname
Write-Host $hostname "device is NOT Azure AD Registered" -ForegroundColor Yellow
Write-Log -Message "$hostname device is NOT Azure AD Registered"
Write-Host ''
Write-Host "Test failed:" $hostname "device is NOT connected to Azure AD, hence PRT does not exist" -ForegroundColor Red
Write-Log -Message "Test failed: $hostname device is NOT connected to Azure AD, hence PRT does not exist" -Level ERROR
Write-Host ''
Write-Host "Recommended action: make sure the device is connected to Azure AD to get Azure AD PRT" -ForegroundColor Yellow
Write-Log -Message "Recommended action: make sure the device is connected to Azure AD to get Azure AD PRT"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}
}else{
#Check if the device AADJ
Write-Host ''
Write-Host "Testing if the device is Azure AD Joined..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is Azure AD Joined..."
$AADJ = $DSReg | Select-String AzureAdJoined
$AADJ = ($AADJ.tostring() -split ":")[1].trim()
if ($AADJ -eq 'YES'){
#The device AADJ
$hostname = hostname
Write-Host $hostname "device is Azure AD joined" -ForegroundColor Green
Write-Log -Message "$hostname device is Azure AD joined"
#CheckPRT value
Write-Host ''
Write-Host "Testing Azure AD PRT..." -ForegroundColor Yellow
Write-Log -Message "Testing Azure AD PRT..."
$ADPRT = $DSReg | Select-String AzureAdPrt | select-object -First 1
$ADPRT = ($ADPRT.tostring() -split ":")[1].Trim()
if ($ADPRT -eq 'YES'){
#PRT is available
Write-Host "Test passed: Azure AD PRT is available on this device for the looged on user" -ForegroundColor Green
Write-Log -Message "Test passed: Azure AD PRT is available on this device for the looged on user"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
}else{
#PRT not available
Write-Host "Test failed: Azure AD PRT is not available. Hence SSO will not work, and the device may be blocked if you have a device-based Conditional Access Policy" -ForegroundColor Red
Write-Log -Message "Test failed: Azure AD PRT is not available. Hence SSO will not work, and the device may be blocked if you have a device-based Conditional Access Policy"
Write-Host ''
Write-Host "Recommended action: lock the device and unlock it and run the script again. If the issue remains, collect the logs and send them to MS support" -ForegroundColor Yellow
Write-Log -Message "Recommended action: lock the device and unlock it and run the script again. If the issue remains, collect the logs and send them to MS support"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}else{
$hostname = hostname
Write-Host $hostname "device is NOT Azure AD joined" -ForegroundColor Yellow
Write-Log -Message "$hostname device is NOT Azure AD joined"
#Check WPJ
Write-Host ''
Write-Host "Testing if the device is Azure AD Registered..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is Azure AD Registered..."
$WPJ = $DSReg | Select-String WorkplaceJoined
$WPJ = ($WPJ.tostring() -split ":")[1].trim()
if ($WPJ -eq 'YES'){
#Device is WPJ, check the registry
$hostname = hostname
Write-Host $hostname "device is Azure AD Registered" -ForegroundColor Green
Write-Log -Message "$hostname device is Azure AD Registered"
#check registry
Write-Host ''
Write-Host "Testing Azure AD PRT registry value..." -ForegroundColor Yellow
Write-Log -Message "Testing Azure AD PRT registry value..."
if ((Get-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\AAD\Storage\https://login.microsoftonline.com -ErrorAction SilentlyContinue).PSPath){
Write-Host "Test passed: Azure AD PRT registry value exists for the looged on user" -ForegroundColor Green
Write-Log -Message "Test passed: Azure AD PRT registry value exists for the looged on user"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
}else{
Write-Host "Test failed: Azure AD PRT registry value does not exist for the looged on user" -ForegroundColor Red
Write-Log -Message "Test failed: Azure AD PRT registry value does not exist for the looged on user" -Level ERROR
Write-Host ''
Write-Host "Recommended action: Disconnect the device from Azure AD form 'settings > Accounts > Access work or school' and then connect it again to Azure AD" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Disconnect the device from Azure AD form 'settings > Accounts > Access work or school' and then connect it again to Azure AD"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}else{
$hostname = hostname
Write-Host $hostname "device is NOT Azure AD Registered" -ForegroundColor Yellow
Write-Log -Message "$hostname device is NOT Azure AD Registered"
Write-Host "Test failed:" $hostname "device is NOT connected to Azure AD, hence PRT does not exist" -ForegroundColor Red
Write-Log -Message "Test failed: $hostname device is NOT connected to Azure AD, hence PRT does not exist"
Write-Host ''
Write-Host "Recommended action: make sure the device is connected to Azure AD to get Azure PRT" -ForegroundColor Yellow
Write-Log -Message "Recommended action: make sure the device is connected to Azure AD to get Azure PRT"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
}
}
}
Function checkProxy($Write){
# Check Proxy settings
If($Write){Write-Host "Checking winHTTP proxy settings..." -ForegroundColor Yellow; Write-Log -Message "Checking winHTTP proxy settings..."}
$global:ProxyServer="NoProxy"
$winHTTP = netsh winhttp show proxy
$Proxy = $winHTTP | Select-String server
$global:ProxyServer=$Proxy.ToString().TrimStart("Proxy Server(s) : ")
$global:Bypass = $winHTTP | Select-String Bypass
$global:Bypass=$global:Bypass.ToString().TrimStart("Bypass List : ")
if ($global:ProxyServer -eq "Direct access (no proxy server)."){
$global:ProxyServer="NoProxy"
If($Write){Write-Host "Access Type : DIRECT"; Write-Log -Message "Access Type : DIRECT"}
}
if ( ($global:ProxyServer -ne "NoProxy") -and (-not($global:ProxyServer.StartsWith("http://")))){
If($Write){Write-Host " Access Type : PROXY"; Write-Log -Message " Access Type : PROXY"}
If($Write){Write-Host "Proxy Server List :" $global:ProxyServer; Write-Log -Message "Proxy Server List : $global:ProxyServer"}
If($Write){Write-Host "Proxy Bypass List :" $global:Bypass; Write-Log -Message "Proxy Bypass List : $global:Bypass"}
$global:ProxyServer = "http://" + $global:ProxyServer
}
$global:login= $global:Bypass.Contains("*.microsoftonline.com") -or $global:Bypass.Contains("login.microsoftonline.com")
$global:device= $global:Bypass.Contains("*.microsoftonline.com") -or $global:Bypass.Contains("*.login.microsoftonline.com") -or $global:Bypass.Contains("device.login.microsoftonline.com")
$global:enterprise= $global:Bypass.Contains("*.windows.net") -or $global:Bypass.Contains("enterpriseregistration.windows.net")
#CheckwinInet proxy
If($Write){Write-Host ''}
If($Write){Write-Host "Checking winInet proxy settings..." -ForegroundColor Yellow; Write-Log -Message "Checking winInet proxy settings..."}
$winInet=RunPScript -PSScript "Get-ItemProperty -Path 'Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings'"
if($winInet.ProxyEnable){If($Write){Write-Host " Proxy Enabled : Yes"; Write-Log -Message " Proxy Enabled : Yes"}}else{If($Write){Write-Host " Proxy Enabled : No";Write-Log -Message " Proxy Enabled : No"}}
$winInetProxy="Proxy Server List : "+$winInet.ProxyServer
If($Write){Write-Host $winInetProxy;Write-Log -Message $winInetProxy}
$winInetBypass="Proxy Bypass List : "+$winInet.ProxyOverride
If($Write){Write-Host $winInetBypass; Write-Log -Message $winInetBypass}
$winInetAutoConfigURL=" AutoConfigURL : "+$winInet.AutoConfigURL
If($Write){Write-Host $winInetAutoConfigURL;Write-Log -Message $winInetAutoConfigURL}
return $global:ProxyServer
}
Function WPJTS{
#Check OS version:
Write-Host ''
Write-Host "Testing OS version..." -ForegroundColor Yellow
Write-Log -Message "Testing OS version..."
$OSVersoin = ([environment]::OSVersion.Version).major
$OSBuild = ([environment]::OSVersion.Version).Build
if (($OSVersoin -ge 10) -and ($OSBuild -ge 1511)){
$OSVer = (([environment]::OSVersion).Version).ToString()
Write-Host "Test passed: device has current OS version ($OSVer)" -ForegroundColor Green
Write-Log -Message "Test passed: device has current OS version ($OSVer)"
}else{
# dsregcmd will not work.
Write-Host "The device has a Windows down-level OS version" -ForegroundColor Red
Write-Log -Message "The device has a Windows down-level OS version"
Write-Host ''
Write-Host "Recommended action: Run this test on current OS versions e.g. Windows 10, Server 2016 and above" -ForegroundColor Yellow
Write-Log -Message "Recommended action: Run this test on current OS versions e.g. Windows 10, Server 2016 and above"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
#Check dsregcmd status.
$DSReg = dsregcmd /status
$hostname=hostname
#Checking if the device connected to AzureAD
Write-Host ''
Write-Host "Testing if the device is Azure AD Registered..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is Azure AD Registered..."
$WPJ = $DSReg | Select-String WorkplaceJoined | Select-Object -First 1
$WPJ = ($WPJ.tostring() -split ":")[1].trim()
if ($WPJ -ne "YES"){
#The device is not connected to AAD:
### perform WPJ (all other tests should be here)
Write-Host "Test failed:" $hostname "device is NOT connected to Azure AD as Azure AD Registered device" -ForegroundColor Red
Write-Log -Message "Test failed: $hostname device is NOT connected to Azure AD as Azure AD Registered device"
#Checking Internet connectivity
Test-DevRegConnectivity-User $true | out-null
ConnecttoAzureAD
Test-DevRegApp
Write-Host ''
Write-Host ''
Write-Host "All tests completed successfully. You can start registering your device to Azure AD." -ForegroundColor Green
Write-Log -Message "All tests completed successfully. You can start registering your device to Azure AD."
Write-Host ''
Write-Host ''
exit
}else{
#The device is WPJ join
$TenantName = $DSReg | Select-String WorkplaceTenantName
$TenantName =($TenantName.tostring() -split ":")[1].trim()
$hostname = hostname
Write-Host "Test passed:" $hostname "device is connected to Azure AD tenant:" $TenantName "as Azure AD Register device" -ForegroundColor Green
Write-Log -Message "Test passed: $hostname device is connected to Azure AD tenant: $TenantName as Azure AD Register device"
}
#Check the device status on AAD:
$DID = $DSReg | Select-String WorkplaceDeviceId
$DID = ($DID.ToString() -split ":")[1].Trim()
CheckDeviceHealth $DID $true
Write-Host ''
Write-Host ''
Write-Host "The device is connected to Azure AD as Azure AD Registered device, and it is in healthy state." -ForegroundColor Green
Write-Log -Message "The device is connected to Azure AD as Azure AD Registered device, and it is in healthy state."
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
}#end WPJTS
Function AADJ{
<##Check PSAdmin
Write-Host ''
Write-Host "Testing if PowerShell running with elevated privileges..." -ForegroundColor Yellow
Write-Log -Message "Testing if PowerShell running with elevated privileges..."
if (PSasAdmin){
# PS running as admin.
Write-Host "PowerShell is running with elevated privileges" -ForegroundColor Green
Write-Log -Message "PowerShell is running with elevated privileges"
}else{
Write-Host "PowerShell is NOT running with elevated privileges" -ForegroundColor Red
Write-Log -Message "PowerShell is NOT running with elevated privileges" -Level ERROR
Write-Host ''
Write-Host "Recommended action: This test needs to be running with elevated privileges" -ForegroundColor Yellow
Write-Log -Message "Recommended action: This test needs to be running with elevated privileges"
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}#>
#Check OS version:
Write-Host ''
Write-Host "Testing OS version..." -ForegroundColor Yellow
Write-Log -Message "Testing OS version..."
$OSVersoin = ([environment]::OSVersion.Version).major
$OSBuild = ([environment]::OSVersion.Version).Build
if (($OSVersoin -ge 10) -and ($OSBuild -ge 1511)){
$OSVer = (([environment]::OSVersion).Version).ToString()
Write-Host "Test passed: device has current OS version ($OSVer)" -ForegroundColor Green
Write-Log -Message "Test passed: device has current OS version ($OSVer)"
}else{
# dsregcmd will not work.
Write-Host "The device has a Windows down-level OS version." -ForegroundColor Red
Write-Log -Message "The device has a Windows down-level OS version." -Level ERROR
Write-Host ''
Write-Host "Recommended action: Run this test on current OS versions e.g. Windows 10, Server 2016 and above." -ForegroundColor Yellow
Write-Log -Message "Recommended action: Run this test on current OS versions e.g. Windows 10, Server 2016 and above."
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
#Check dsregcmd status.
$DSReg = dsregcmd /status
Write-Host ''
Write-Host "Testing if the device joined to the local domain..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device joined to the local domain..."
$DJ = $DSReg | Select-String DomainJoin
$DJ = ($DJ.tostring() -split ":")[1].trim()
if ($DJ -ne "YES"){
$hostname = hostname
Write-Host $hostname "device is NOT joined to the local domain" -ForegroundColor Yellow
Write-Log -Message "device is NOT joined to the local domain"
}else{
#The device is joined to the local domain.
$DomainName = $DSReg | Select-String DomainName
$DomainName =($DomainName.tostring() -split ":")[1].trim()
$hostname = hostname
Write-Host $hostname "device is joined to the local domain:" $DomainName -ForegroundColor Yellow
Write-Log -Message "$hostname device is joined to the local domain: $DomainName"
Write-Host ''
Write-Host "Recommended action: The selected option runs for AADJ devices. To troubleshoot hybrid devices, re-run the script and select option '3'." -ForegroundColor Yellow
Write-Log -Message "Recommended action: The selected option runs for AADJ devices. To troubleshoot hybrid devices, re-run the script and select option '3'."
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}
#Checking if the device connected to AzureAD
''
Write-Host "Testing if the device is joined to Azure AD..." -ForegroundColor Yellow
Write-Log -Message "Testing if the device is joined to Azure AD..."
$AADJ = $DSReg | Select-String AzureAdJoined
$AADJ = ($AADJ.tostring() -split ":")[1].trim()
if ($AADJ -ne "YES"){
#The device is not connected to AAD:
### perform AADJ (all other tests should be here)
Write-Host "Test failed:" $hostname "device is NOT connected to Azure AD" -ForegroundColor Red
Write-Log -Message "Test failed: $hostname device is NOT connected to Azure AD"
<##Checking if the user is bulitin admin
Write-Host ''
Write-Host "Testing if you signed in user is a Built-in Administrator account..." -ForegroundColor Yellow
Write-Log -Message "Testing if you signed in user is a Built-in Administrator account..."
$BAdmin=(Get-LocalUser | where{$_.SID -like "*-500"}).name
$LUser=$env:username
if ($BAdmin -eq $LUser){
Write-Host "Test failed: you signed in using the built-in Administrator account" -ForegroundColor Red
Write-Log -Message "Test failed: you signed in using the built-in Administrator account" -Level ERROR
Write-Host ''
Write-Host "Recommended action: create a different local account before you use Azure Active Directory join to finish the setup." -ForegroundColor Yellow
Write-Log -Message "Recommended action: create a different local account before you use Azure Active Directory join to finish the setup."
Write-Host ''
Write-Host ''
Write-Host "Script completed successfully." -ForegroundColor Green
Write-Log -Message "Script completed successfully."
Write-Host ''
Write-Host ''
exit
}else{
Write-Host "Test passed: you are not signed in using the built-in Administrator account" -ForegroundColor Green
Write-Log -Message "Test passed: you are not signed in using the built-in Administrator account"
}#>
#Checking if the signed in user is a local admin
Write-Host ''
Write-Host "Testing if the signed in user has local admin permissions..." -ForegroundColor Yellow
Write-Log -Message "Testing if the signed in user has local admin permissions..."
$LocalAdminGroup=(whoami /groups | Select-String 'S-1-5-32-544')
#if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){
if ($LocalAdminGroup){
Write-Host "Test passed: the signed in user has local admin permissions" -ForegroundColor Green
Write-Log -Message "Test passed: the signed in user has local admin permissions"
}else{
Write-Host "Test failed: the signed in user does NOT have local admin permissions" -ForegroundColor Red
Write-Log -Message "Test failed: the signed in user does NOT have local admin permissions" -Level ERROR