-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoPilotReadiness.ps1
1458 lines (1233 loc) · 72.1 KB
/
AutoPilotReadiness.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
Ensure device is Autopilot Ready
.DESCRIPTION
To ensure a new or existing device is ready to be Autopilot deployed
.NOTES
Author : Dick Tracy <[email protected]>
Source : https://github.com/PowerShellCrack/PSAutopilotReadinessCheck
Version : 2.2.8
README : Review README.md for more details and configurations
CHANGELOG : Review CHANGELOG.md for updates and fixes
IMPORTANT : By using this script or parts of it, you have read and accepted the DISCLAIMER.md and LICENSE agreement
.PARAMETER AzureEnvironment
Specify the Azure environment for graph
.PARAMETER Serial
Specify the serial number of a device.
.PARAMETER DeviceName
Specify the deviceName to check against.
.PARAMETER UserPrincipalName
Specity the UserPrinciplName
.PARAMETER CheckUserLicense
Check user licenses
.PARAMETER CheckAzureAdvSettings
Check additional settings in Azure
.EXAMPLE
.\AutoPilotReadiness.ps1 -Serial 'N4N0CX11Z173170'
.EXAMPLE
.\AutoPilotReadiness.ps1 -DeviceName 'DTOAAD-1Z156178'
.EXAMPLE
.\AutoPilotReadiness.ps1 -Serial 'N4N0CX11Z173170' -UserPrincipalName '[email protected]' -CheckUserLicense
.EXAMPLE
.\AutoPilotReadiness.ps1 -DeviceName 'DTOAAD-1Z156178' -UserPrincipalName '[email protected]' -CheckUserLicense -CheckAzureAdvSettings
#>
[CmdletBinding()]
Param(
[ValidateSet('Public','USGov','USDoD')]
[string]$AzureEnvironment = 'Public',
[Parameter(Mandatory = $true,ParameterSetName='device')]
[string]$DeviceName,
[Parameter(Mandatory = $true,ParameterSetName='serial')]
[string]$Serial,
[Parameter(Mandatory = $false,HelpMessage="Please enter a valid user principal name [userid@domain]")]
[ValidateScript({$_ -match "^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$"})]
[string]$UserPrincipalName,
[switch]$CheckUserLicense,
[switch]$CheckAzureAdvSettings
)
##======================
## VARIABLES
##======================
$ErrorActionPreference = "Stop"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Save current progress preference and hide the progress
$global:ProgressPreference = 'SilentlyContinue'
#Check if verbose is used; if it is don't use the nonewline in output
If($VerbosePreference){$NoNewLine=$False}Else{$NoNewLine=$True}
Write-Host ("`nPrerequisite check...") -ForegroundColor Cyan
# Determine what environment to use
switch($AzureEnvironment){
'Public' {$script:GraphEndpoint = 'https://graph.microsoft.com';$GraphEnvironment = "Global"}
'USgov' {$script:GraphEndpoint = 'https://graph.microsoft.us';$GraphEnvironment = "USgov"
Write-Host "Autopilot is not available in USgov environment as of 8/2/2023. Exiting script..." -ForegroundColor Red
Exit
}
'USDoD' {$script:GraphEndpoint = 'https://dod-graph.microsoft.us';$GraphEnvironment = "USGovDoD"
Write-Host "Autopilot is not available in USDod environment as of 8/2/2023. Exiting script..." -ForegroundColor Red
Exit
}
default {$script:GraphEndpoint = 'https://graph.microsoft.com';$GraphEnvironment = "Global"}
}
#CONSTANTS
$IntuneEnrolled = $false
$MDMPolicyAssigned = $false
$AssignedToIntuneLicense = $false
$PrimaryAssignedUser = $null
$AssignedToESPApp = $true
$UserAssignedApps = @()
$ZTDID = $null
$AutopilotDevice = $null
$ShowWarning = $false
## ================================
## FUNCTIONS
## ================================
Function Get-Symbol{
<#
.SYNOPSIS
Returns a UTF-8 character based on the symbol name
.DESCRIPTION
Returns a UTF-8 character based on the symbol name
.PARAMETER Symbol
The name of the symbol to return
.EXAMPLE
Get-Symbol -Symbol GreenCheckmark
.EXAMPLE
Get-Symbol -Symbol RedX
#>
Param(
[ValidateSet( 'AccessDenied',
'Alert',
'Cloud',
'GreenCheckmark',
'Hourglass',
'Information',
'Lightbulb',
'Lock',
'RedX',
'Script',
'WarningSign'
)]
[string]$Symbol
)
switch($Symbol){
'AccessDenied' { Return [char]::ConvertFromUtf32(0x1F6AB)}
'Alert' { Return [char]::ConvertFromUtf32(0x1F514)}
'Cloud' { Return [char]::ConvertFromUtf32(0x2601)}
'GreenCheckmark' { Return [char]::ConvertFromUtf32(0x2705)}
'Hourglass' { Return [char]::ConvertFromUtf32(0x231B)}
'Information' { Return [char]::ConvertFromUtf32(0x2139)}
'Lightbulb' { Return [char]::ConvertFromUtf32(0x1F4A1)}
'Lock' { Return [char]::ConvertFromUtf32(0x1F512)}
'RedX' { Return [char]::ConvertFromUtf32(0x274C)}
'Script' { Return [char]::ConvertFromUtf32(0x1F4DC)}
'WarningSign' { Return [char]::ConvertFromUtf32(0x26A0)}
}
}
Function Get-AssignmentList{
<#
.SYNOPSIS
Returns a list of all the assignments from a graph request
.PARAMETER GraphRequestPayload
The graph request payload
.PARAMETER IncludeNotAssigned
Include assignments that are not assigned
#>
Param(
[Parameter(Mandatory = $true)]
$GraphRequestPayload,
[switch]$IncludeNotAssigned
)
$AssignmentList = @()
#TEST $Item = $GraphRequestPayload[0]
#TEST $Item = $GraphRequestPayload[2]
Foreach($Item in $GraphRequestPayload){
If($IncludeNotAssigned -and -Not([bool]$Item.assignments) ){
$assignmentValue = New-Object pscustomobject
$assignmentValue | Add-Member -MemberType NoteProperty -Name Name -Value $Item.displayName
$assignmentValue | Add-Member -MemberType NoteProperty -Name Id -Value $Item.id
$assignmentValue | Add-Member -MemberType NoteProperty -Name Intent -Value 'none'
$assignmentValue | Add-Member -MemberType NoteProperty -Name AssignmentType -Value $null
$AssignmentList += $assignmentValue
}Else{
#TEST $assignmentEntry = $Item.assignments.target[0]
foreach ($assignmentEntry in $Item.assignments)
{
Foreach($AssignmentTarget in $assignmentEntry.target){
$assignmentValue = New-Object pscustomobject
$assignmentValue | Add-Member -MemberType NoteProperty -Name Name -Value $Item.displayName
$assignmentValue | Add-Member -MemberType NoteProperty -Name Id -Value $Item.id
$assignmentValue | Add-Member -MemberType NoteProperty -Name Intent -Value $assignmentEntry.intent
$assignmentValue | Add-Member -MemberType NoteProperty -Name AssignmentType -Value ($AssignmentTarget.'@odata.type'.replace('#microsoft.graph.',''))
if ($null -ne $assignmentEntry.deviceAndAppManagementAssignmentFilterType)
{
$assignmentValue | Add-Member -MemberType NoteProperty -Name TargetFilterType -Value $AssignmentTarget.deviceAndAppManagementAssignmentFilterType.ToString()
}
$assignmentValue | Add-Member -MemberType NoteProperty -Name FilterId -Value $AssignmentTarget.deviceAndAppManagementAssignmentFilterId
$assignmentValue | Add-Member -MemberType NoteProperty -Name groupId -Value $AssignmentTarget.groupId
#add to collection
$AssignmentList += $assignmentValue
}
}
}
}
return $AssignmentList
}
Function Set-AssignmentAssociations {
<#
.SYNOPSIS
Returns a list of all the assignments from a graph request
.PARAMETER AssignmentPayload
The Assignment payload
.PARAMETER DeviceGroupIds
The list of device groups to associate
.PARAMETER UserGroupIds
The list of user groups to associate
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Alias("Assignments")]
$AssignmentPayload,
[AllowEmptyString()]
$DeviceGroupIds,
[AllowEmptyString()]
$UserGroupIds,
[switch]$OnlyRequired
)
Begin{
$AssignmentAssociations = @()
}
Process{
#TEST $Assignment = $AssignmentPayload[0]
Foreach($Assignment in $AssignmentPayload){
#determine to add or remove assignment based on target type
If($OnlyRequired -and $Assignment.intent -ne "Required"){
Write-Verbose ("Ignoring group id [{0}]. It is assigned as [{2}] for [{1}]" -f $Assignment.groupId,$Assignment.Name,$Assignment.Intent)
}Else{
switch($Assignment.AssignmentType){
'groupAssignmentTarget' {
If($Assignment.groupId -in $DeviceGroupIds){
Write-Verbose ("Adding device group id [{0}] to associated assignment list for [{1}]" -f $Assignment.groupId,$Assignment.Name)
$AssignmentAssociations += $Assignment
}
If($Assignment.groupId -in $UserGroupIds){
Write-Verbose ("Adding User group id [{0}] to associated assignment list for [{1}]" -f $Assignment.groupId,$Assignment.Name)
$AssignmentAssociations += $Assignment
}
}
'exclusionGroupAssignmentTarget' {
If($Assignment.groupId -in $DeviceGroupIds){
Write-Verbose ("Excluding device group id [{0}] from associated assignment list for [{1}]" -f $Assignment.groupId,$Assignment.Name)
$AssignmentAssociations = $AssignmentAssociations | Where-Object{($_.groupId -NotIn $DeviceGroups.Id)}
}
If($Assignment.groupId -in $UserGroupIds){
Write-Verbose ("Excluding user group id [{0}] from associated assignment list for [{1}]" -f $Assignment.groupId,$Assignment.Name)
$AssignmentAssociations = $AssignmentAssociations | Where-Object{($_.groupId -NotIn $UserGroupIds)}
}
}
'allDevicesAssignmentTarget' {
Write-Verbose ("Adding [All devices] group to associated assignment list for [{0}]" -f $Assignment.Name)
$AssignmentAssociations += $Assignment
}
'allLicensedUsersAssignmentTarget' {
Write-Verbose ("Adding [All Users] group to associated assignment list for [{0}]" -f $Assignment.Name)
#sometimes this is already in the list
If(-NOT($AssignmentAssociations | Where-Object Name -eq $Assignment.Name)){
$AssignmentAssociations += $Assignment
}
}
}#end switch
}
}
}
End{
Write-Verbose ("Total Assignments associated with payload: {0}" -f $AssignmentAssociations.count)
return $AssignmentAssociations
}
}
Function Write-Action{
Param(
[Parameter(Mandatory = $true,Position = 1)]
$Statement,
[switch]$ExitAsError
)
If($ExitAsError){
$color = 'Red'
}Else{
$color = 'Yellow'
}
Write-Host "`n------------------------------------------------------------------------------------------" -ForegroundColor $color
Write-Host ("ACTION: {0}" -f $Statement) -ForegroundColor $color
Write-Host "------------------------------------------------------------------------------------------" -ForegroundColor $color
If($ExitAsError){
Exit
}
}
##*=============================================
##* INSTALL MODULES
##*=============================================
# Get WindowsAutopilotIntune module (and dependencies)
Write-Host (" |---Checking for module dependencies...")
$Modules = @(
'Microsoft.Graph.Authentication'
)
$i=0
Foreach($Module in $Modules){
$i++
Write-Host (" |---[{0} of {1}]: Installing module {2}..." -f $i,$Modules.count,$Module) -NoNewline:$noNewLine
#Write-Host ('{0}{1}' -f $msg,(Set-GapCharacter -MessageLength $msg.Length)) -NoNewline
if ( Get-Module -FullyQualifiedName $Module -ListAvailable ) {
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark))
}
else {
Try{
# Needs to be installed as an admin so that the module will execute
Install-Module -Name $Module -Scope AllUsers -ErrorAction Stop
Import-Module -Name $Module -Scope Global
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark))
}
Catch {
Write-Host ("{0}. {1}" -f (Get-Symbol -Symbol RedX),$_.Exception.message)
exit
}
}
}
#Install-Module WindowsAutopilotIntune -MinimumVersion 5.3
## ================================
## MAIN
## ================================
Write-Host (" |---Connecting to tenant...") -NoNewline:$NoNewLine
#REFERNCE: https://learn.microsoft.com/en-us/graph/permissions-reference
try{
$Scopes = @(
'Device.Read.All'
'Directory.Read.All'
'GroupMember.Read.All'
'Group.Read.All'
'User.Read.All'
'DeviceManagementApps.Read.All'
'DeviceManagementConfiguration.Read.All'
'DeviceManagementManagedDevices.Read.All'
'DeviceManagementServiceConfig.Read.All'
)
If($PSBoundParameters.ContainsKey('CheckUserLicense')){
$Scopes += @(
'Organization.Read.All' #Required for graph endpoint: subscribedSkus
'Policy.Read.All'
)
}
If($PSBoundParameters.ContainsKey('CheckAzureAdvSettings')){
$Scopes += @(
#'AuditLog.Read.All' #Required for graph endpoint: directoryAudits
'Policy.Read.All'
)
}
$null = Connect-MgGraph -Environment $GraphEnvironment -Scopes ($Scopes | Select-Object -Unique) -Verbose:$false
$MGContext = Get-MgContext
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark))
Write-Host (" |---Connected as: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $MGContext.Account) -ForegroundColor Cyan
}Catch{
Write-Host ("{0} " -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red -NoNewline
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-Host "`nUnable to connect to tenant. Can't continue!" -ForegroundColor Red
Exit
}
# Get list of Microsoft Azure licenses
#------------------------------------------------------------------------------------------
If($PSBoundParameters.ContainsKey('CheckUserLicense')){
Write-Host (" |---Attempting to retrieve license names from Microsoft...") -NoNewline:$noNewLine
Try{
#REFERENCE: https://rakhesh.com/azure/m365-licensing-displayname-to-sku-name-mapping/
$licenseCsvURL = 'https://download.microsoft.com/download/e/3/e/e3e9faf2-f28b-490a-9ada-c6089a1fc5b0/Product%20names%20and%20service%20plan%20identifiers%20for%20licensing.csv'
$licenseHashTable = @{}
(Invoke-WebRequest -Uri $licenseCsvURL).ToString() | ConvertFrom-Csv | ForEach-Object {
$licenseHashTable[$_.GUID] = [ordered]@{
"FriendlyDisplayName" = $_.Service_Plans_Included_Friendly_Names
"ProductDisplayName" = $_.Product_Display_Name
"SkuPartNumber" = $_.String_Id
}
}
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
Write-Host (" |---Total License Skus found: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $licenseHashTable.count) -ForegroundColor Cyan
}Catch{
Write-Host ("{0}. {1}" -f (Get-Symbol -Symbol Information), $_.Exception.Message) -ForegroundColor Yellow
$ShowWarning = $true
}
Write-Host (" |---Retrieving licenses from Azure tenant [{0}]..." -f $MGContext.TenantId)
Try{
$TenantAssignedLicenses = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/subscribedSkus").Value
Write-Host (" |---Total License Skus in Tenant: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $TenantAssignedLicenses.count) -ForegroundColor Cyan
$IntuneLicenses = @()
$WindowsLicenses = @()
#test $TenantSku = $TenantAssignedLicenses[0]
#test $TenantSku = $TenantAssignedLicenses[4]
#test $TenantSku = $TenantAssignedLicenses[-1]
Foreach($TenantSku in $TenantAssignedLicenses){
$LicenseType = @()
If($licenseHashTable.count -gt 0){
$LicenseName = ($licenseHashTable[$TenantSku.skuId].GetEnumerator() | Where-Object Name -eq ProductDisplayName).Value
}Else{
$LicenseName = $TenantSku.skuPartNumber
}
Write-Host (" |---License: ") -ForegroundColor Gray -NoNewline:$noNewLine
Write-Verbose ("license: {0}" -f $TenantSku.skuPartNumber)
Write-Verbose ("Service Plan: {0}" -f ($TenantSku.servicePlans.servicePlanName -join ','))
If( $INTUNELICENSE = $TenantSku.servicePlans.servicePlanName | Where-Object {$_ -match 'Intune'} ){
#$IntuneLicenses += $TenantSku
Write-Verbose ("Intune Plan: {0}" -f ($INTUNELICENSE -join ','))
Write-Verbose ("----------------------")
$LicenseType += 'Intune'
$IntuneLicenses += $TenantSku
}
If( $WINLICENSE = $TenantSku.servicePlans.servicePlanName | Where-Object {$_ -match 'WIN10'} ){
#$WindowsLicenseAvailable += $TenantSku
Write-Verbose ("Windows Licenses: {0}" -f ($WINLICENSE -join ','))
Write-Verbose ("----------------------")
$LicenseType += 'Windows'
$WindowsLicenses += $TenantSku
}
If($LicenseType.count -gt 0){
$LicenseType = $LicenseType -join ' + '
Write-Host ("{0} [{1}]" -f $LicenseName,$LicenseType ) -ForegroundColor Cyan
}
Else{
Write-Host ("{0}" -f $LicenseName ) -ForegroundColor White
}
}
Write-Host (" |---Available Intune licenses: ") -ForegroundColor Gray -NoNewline
If($IntuneLicenses.count -gt 0){
Write-Host ("{0}" -f $IntuneLicenses.count ) -ForegroundColor Green
}Else{
Write-Host ("{0}" -f 'none') -ForegroundColor Red
}
Write-Host (" |---Available Windows licenses: ") -ForegroundColor Gray -NoNewline
If($WindowsLicenses.count -gt 0){
Write-Host ("{0}" -f $WindowsLicenses.count ) -ForegroundColor Green
}Else{
Write-Host ("{0}" -f 'none') -ForegroundColor Red
}
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol Information)) -ForegroundColor Yellow
Write-Host ("Unable to determine available Intune licenses") -ForegroundColor Yellow
Write-host ("REASON: Your graph permissions are not allowing you to read licenses from Azure AD.") -ForegroundColor Yellow
Write-Action ("Ensure you have permissions [Directory.Read.All,Organization.Read.All] from graph and rerun script")
$ShowWarning = $true
}
}Else{
#null out hashtable
$licenseHashTable = @{}
}
Write-Host ("`nStarting Autopilot readiness check...") -ForegroundColor Cyan
# 1. Check if device is enrolled as Autopilot Device
#------------------------------------------------------------------------------------------
#check by name
If ($PSCmdlet.ParameterSetName -eq "device")
{
Write-Host (" |---Retrieving device name from Azure AD [{0}]..." -f $DeviceName) -NoNewline:$noNewLine
Try{
$AzureADDevice = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/devices?`$filter=displayName eq '$DeviceName'").Value
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read policies from Azure AD.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [Device.Read.All] from graph and rerun script") -ExitAsError
}
If($AzureADDevice.count -eq 1){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
Write-Host (" |---AzureAD Object id: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $AzureADDevice.Id) -ForegroundColor Cyan
#iterate through each PhysicalIds to get Autopilot one
Foreach($PhysicalIds in $AzureADDevice.PhysicalIds | Where-Object {$_ -match 'ZTDID'}){
$ZTDID = [System.Text.RegularExpressions.Regex]::Match($PhysicalIds,'\[ZTDID\]:(?<ztdid>.*)').Groups['ztdid'].value
}
}Else{
Write-Host ("{0} " -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red -NoNewline
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-Host ("Unable to retrieve device in Azure by device name [{0}]" -f $DeviceName) -ForegroundColor Red
Write-host ("REASON: If its an new device and it has been imported as Autopilot device, the device name should be the serial number and in Azure AD.") -ForegroundColor Red
Write-Action ("Upload hash and rerun script!") -ExitAsError
}
#if the ztdid is there, it should match ap device id
If($null -ne $ZTDID){
Write-Host ("`n |---Retrieving Autopilot ZTDID attribute from device object [{0}]..." -f $AzureADDevice.Id) -NoNewline:$noNewLine
Try{
$AutopilotDevice = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/deviceManagement/windowsAutopilotDeviceIdentities/$ZTDID")
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read Autopilot devices from Intune.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [DeviceManagementManagedDevices.Read.All,DeviceManagementServiceConfig.Read.All] from graph and rerun script") -ExitAsError
}
If($null -ne $AutopilotDevice){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
Write-Host (" |---ZTDID: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $ZTDID) -ForegroundColor Cyan
IF([string]::IsNullOrEmpty($AutopilotDevice.groupTag)){
Write-Host (" |---Group tag: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f "none") -ForegroundColor Yellow
$ShowWarning = $true
}Else{
Write-Host (" |---Group tag: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $AutopilotDevice.groupTag) -ForegroundColor Green
}
}Else{
Write-Host ("{0} " -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red -NoNewline
Write-Host ("Unable to determine if the device name [{0}] is registered as an Autopilot device" -f $DeviceName) -ForegroundColor Red
Write-host ("REASON: If its an new device and it has been imported as Autopilot device, the device name should have a [ZTDID] as a PhysicalId attribute in Azure AD") -ForegroundColor Red
Write-Action ("Ensure device has this attribute and rerun script.") -ExitAsError
}
}Else{
Write-Host ("{0} " -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red -NoNewline
Write-Host ("Device [{0}] is not registered as an Autopilot device" -f $DeviceName) -ForegroundColor Red
Write-host ("REASON: If its an new device and it has been imported as Autopilot device, the device name should have a [ZTDID] as a PhysicalId attribute in Azure AD") -ForegroundColor Red
Write-Action ("Ensure device has this attribute and rerun script.") -ExitAsError
}
Write-Host ("`n |---Retrieving device name from Intune [{0}]..." -f $DeviceName) -NoNewline:$noNewLine
Try{
$IntuneDevice = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/deviceManagement/managedDevices?`$filter=deviceName eq '$DeviceName'").Value
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read devices from Intune.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [DeviceManagementManagedDevices.Read.All,DeviceManagementConfiguration.Read.All] from graph and rerun script") -ExitAsError
}
If($IntuneDevice.count -eq 1){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
Write-Host (" |---Managed device id: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $IntuneDevice.id) -ForegroundColor Cyan
If($IntuneDevice.ownerType -eq 'company'){
Write-Host (" |---Managed device type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Corporate') -ForegroundColor Cyan
}
Else{
Write-Host (" |---Managed device Type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Personal') -ForegroundColor Red
}
If( $IntuneDevice.userPrincipalName){
Write-Host (" |---Managed primary user: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $IntuneDevice.userPrincipalName) -ForegroundColor Green
}Else{
Write-Host (" |---Managed primary user: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'none') -ForegroundColor Yellow
$ShowWarning = $true
}
}Else{
Write-Host ("{0} " -f (Get-Symbol -Symbol Information)) -ForegroundColor Yellow -NoNewline
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-Host ("Device does not exist in Intune, could be new device...") -ForegroundColor Yellow
$ShowWarning = $true
}
}
#check by serial
If ($PSCmdlet.ParameterSetName -eq "serial")
{
Write-Host (" |---Retrieving Autopilot device details from serial [{0}]..." -f $Serial) -NoNewline:$noNewLine
Try{
$AutopilotDevice = (Invoke-MgGraphRequest -Method GET `
-Uri "$script:GraphEndpoint/beta/deviceManagement/windowsAutopilotDeviceIdentities?`$filter=contains(serialNumber,'$Serial')").Value
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read Autopilot devices from Intune.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [DeviceManagementServiceConfig.Read.All] from graph and rerun script") -ExitAsError
}
If($AutopilotDevice.Count -eq 1){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
Write-Host (" |---AzureAD Object Id: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $AutopilotDevice.azureAdDeviceId) -ForegroundColor Cyan
If($AutopilotDevice.managedDeviceId -eq '00000000-0000-0000-0000-000000000000'){
Write-Host (" |---Intune device Id: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Not enrolled') -ForegroundColor Yellow
$ShowWarning = $true
}Else{
Write-Host (" |---Intune device Id: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $AutopilotDevice.managedDeviceId) -ForegroundColor Cyan
$IntuneEnrolled = $true
}
IF([string]::IsNullOrEmpty($AutopilotDevice.groupTag)){
Write-Host (" |---Group tag: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f "none") -ForegroundColor Yellow
$ShowWarning = $true
}Else{
Write-Host (" |---Group tag: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $AutopilotDevice.groupTag) -ForegroundColor Green
}
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-Host ("`nUnable to retrieve Autopilot device from serial. Make sure the serial is correct and try again") -ForegroundColor Red
Write-Action ("Re-import hardware hash and rerun script.") -ExitAsError
}
Write-Host ("`n |---Retrieving Azure AD device id [{0}]..." -f $AutopilotDevice.AzureAdDeviceId) -NoNewline:$noNewLine
Try{
$AzureADDevice = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/devices?`$filter=deviceId eq '$($AutopilotDevice.AzureAdDeviceId)'").Value
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read devices from Azure AD.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [Device.Read.All] from graph and rerun script") -ExitAsError
}
If($AzureADDevice.Count -eq 1){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
Write-Host (" |---Device Name: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $AzureADDevice.displayName) -ForegroundColor Cyan
If( $AzureADDevice.deviceOwnership -eq 'Company'){
Write-Host (" |---Device Type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Corporate') -ForegroundColor Cyan
}
Elseif([string]::IsNullOrEmpty($AzureADDevice.deviceOwnership)){
Write-Host (" |---Device Type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Unknown') -ForegroundColor Yellow
$ShowWarning = $true
}
Else{
Write-Host (" |---Device Type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Personal') -ForegroundColor Red
}
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-Host ("Unable to retrieve device in Azure by device id [{0}]" -f $AutopilotDevice.AzureAdDeviceId) -ForegroundColor Red
Write-host ("REASON: If its an new device and it has been imported as Autopilot device, the device name should be the serial number and in Azure AD.") -ForegroundColor Red
Write-Action ("Re-import hardware hash and rerun script.") -ExitAsError
}
If($IntuneEnrolled){
Write-Host ("`n |---Retrieving device details from Intune [{0}]..." -f $AutopilotDevice.managedDeviceId) -NoNewline:$noNewLine
Try{
$IntuneDevice = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/deviceManagement/managedDevices/$($AutopilotDevice.managedDeviceId)" -ErrorVariable GraphError)
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
}Catch{
Write-Verbose ("{0} " -f $_.Exception.Message)
If($GraphError.InnerException.Response.StatusCode -eq 'NotFound'){
Write-Host ("{0} " -f (Get-Symbol -Symbol Information)) -ForegroundColor Yellow
Write-Host (" |---Managed device status: ") -ForegroundColor White -NoNewline
Write-Host ("Not found. Could this be a new device?") -ForegroundColor Yellow
$ShowWarning = $true
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-host ("REASON: Your graph permissions are not allowing you to read devices from Intune.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [DeviceManagementManagedDevices.Read.All,DeviceManagementConfiguration.Read.All] from graph and rerun script") -ExitAsError
}
}
If($null -ne $IntuneDevice){
Write-Host (" |---Managed device id: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $IntuneDevice.id) -ForegroundColor Cyan
If($IntuneDevice.ownerType -eq 'company'){
Write-Host (" |---Managed device type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Corporate') -ForegroundColor Green
}
Else{
Write-Host (" |---Managed device Type: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'Personal') -ForegroundColor Red
}
If( $IntuneDevice.userPrincipalName){
Write-Host (" |---Currently assigned primary user: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $IntuneDevice.userPrincipalName) -ForegroundColor Green
}Else{
Write-Host (" |---Currently assigned primary user: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f 'none') -ForegroundColor Yellow
$ShowWarning = $true
}
}
}
}
If($AzureADDevice.count -eq 1){
# Get all Azure AD group the device is a member of
#------------------------------------------------------------------------------------------
Write-Host ("`n |---Retrieving groups assigned to device object [{0}]..." -f $AzureADDevice.id) -NoNewline:$noNewLine
$assignedDeviceGroups = @()
Try{
#$assignedDeviceGroups += (Invoke-MgGraphRequest -Method GET -Body (@{securityEnabledOnly=$false} | ConvertTo-Json) `
# -Uri "$script:GraphEndpoint/beta/devices/$($AzureADDevice.id)/memberOf").Value
$assignedDeviceGroups += (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/devices/$($AzureADDevice.id)/memberOf").Value
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read device groups from Azure AD.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [Device.Read.All,Group.Read.All,GroupMember.Read.All] from graph and rerun script") -ExitAsError
}
If($assignedDeviceGroups.count -ge 1){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark))
Write-Host (" |---Member of groups: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $assignedDeviceGroups.count) -ForegroundColor Cyan
#iterate through each group id for name
Foreach($Group in $assignedDeviceGroups){
Write-Host (" |---Group: ") -NoNewline -ForegroundColor Gray
#check to see if any of the groups are dynamic groups using orderid
If($Group.membershipRule -match '[ZTDID]' -or $Group.membershipRule -match "[OrderID]:$($AutopilotDevice.groupTag)"){
Write-Host ("{0}" -f $Group.displayName) -ForegroundColor Green
}Else{
Write-Host ("{0}" -f $Group.displayName) -ForegroundColor White
}
}
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
}
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Host ("Device Name [{0}] was not found in Azure AD." -f $DeviceName) -ForegroundColor Red
Write-host ("REASON: If the device is new, it should be imported as Autopilot device and the device name should be the serial number.") -ForegroundColor Red
Write-Action ("Ensure device has been registered to Autopilot.") -ExitAsError
}
# Get User Details
#------------------------------------------------------------------------------------------
If($PSBoundParameters.ContainsKey('UserPrincipalName'))
{
Write-Host ("`n |---Retrieving account details for specified user principal name [{0}]..." -f $UserPrincipalName) -NoNewline:$noNewLine
Try{
#$PrimaryAssignedUser = (Invoke-MgGraphRequest -Method GET -Body (@{securityEnabledOnly=$false} | ConvertTo-Json) `
# -Uri "$script:GraphEndpoint/beta/users?`$filter=userPrincipalName eq '$UserPrincipalName'&`$expand=memberOf").Value
#Get-MgUser -Filter "userPrincipalName eq '$UserPrincipalName'" -Debug
$PrimaryAssignedUser = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/users?`$filter=userPrincipalName eq '$UserPrincipalName'&`$expand=memberOf" -ErrorVariable GraphError).Value
#$PrimaryAssignedUser = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/users?`$select=userPrincipalName,mail,id&`$filter=userPrincipalName eq '$UserPrincipalName'&`$expand=memberOf").Value
#$PrimaryAssignedUser = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/users?`$filter=startswith(userPrincipalName,'$UserPrincipalName')&`$expand=memberOf").Value
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
}Catch{
Write-Verbose ("{0} " -f $_.Exception.Message)
switch($GraphError.InnerException.Response.StatusCode){
'NotFound' {
Write-Host ("{0}" -f (Get-Symbol -Symbol Information)) -ForegroundColor Yellow
Write-Host ("`nREASON: Primary user is not found; unable to determine if Intune license is assigned...") -ForegroundColor Yellow
Write-Action ("Continuing is risky; rerun this script with [-UserPrincipalName] parameter")
$ShowWarning = $true
}
'BadRequest' {
Write-Host ("{0}" -f (Get-Symbol -Symbol Information)) -ForegroundColor Yellow
Write-Host ("`nREASON: Primary user is missing; unable to determine if Intune license is assigned...") -ForegroundColor Yellow
Write-Action ("Continuing is risky; rerun this script with [-UserPrincipalName] parameter")
$ShowWarning = $true
}
default {
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read users from Azure AD.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [User.Read.All] to read users from graph and rerun script") -ExitAsError
}
}
}
}
ElseIf($IntuneDevice.userPrincipalName)
{
Write-Host ("`n |---Retrieving account details for current primary user [{0}]..." -f $IntuneDevice.userPrincipalName) -NoNewline:$noNewLine
Try{
#$PrimaryAssignedUser = (Invoke-MgGraphRequest -Method GET -Body (@{securityEnabledOnly=$false} | ConvertTo-Json) `
# -Uri "$script:GraphEndpoint/beta/users?`$filter=userPrincipalName eq '$($IntuneDevice.userPrincipalName)'&`$expand=memberOf").Value
$PrimaryAssignedUser = (Invoke-MgGraphRequest -Method GET -Uri "$script:GraphEndpoint/beta/users?`$filter=userPrincipalName eq '$($IntuneDevice.userPrincipalName)'&`$expand=memberOf").Value
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark)) -ForegroundColor Green
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-Host ("Unable to retrieve user principal name from Azure [{0}]" -f $IntuneDevice.userPrincipalName) -ForegroundColor Red
Write-host ("REASON: Your graph permissions are not allowing you to read users from Azure AD.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [User.Read.All] to read users from graph and rerun script") -ExitAsError
}
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol Information)) -ForegroundColor Yellow
Write-Host ("`nNo user specified or primary user is not found; unable to determine if Intune license is assigned...") -ForegroundColor Yellow
Write-Host ("Continuing is risky; rerun this script with [-UserPrincipalName] parameter") -ForegroundColor Yellow
$ShowWarning = $true
}
#$PrimaryAssignedUser.value
If($null -ne $PrimaryAssignedUser){
#display user details
If($null -ne $PrimaryAssignedUser.onPremisesSamAccountName){
Write-Host (" |---SAM Login Name: ") -NoNewline -ForegroundColor Gray
Write-Host ("{0}" -f $PrimaryAssignedUser.onPremisesSamAccountName) -ForegroundColor Cyan
}
Write-Host (" |---User Display Name: ") -NoNewline -ForegroundColor Gray
Write-Host ("{0}" -f $PrimaryAssignedUser.displayName) -ForegroundColor Cyan
Write-Host (" |---User Email Account: ") -NoNewline -ForegroundColor Gray
Write-Host ("{0}" -f $PrimaryAssignedUser.mail) -ForegroundColor Cyan
Write-Host (" |---User is assigned to ") -ForegroundColor White -NoNewline
If($PrimaryAssignedUser.memberOf.count -gt 0){
Write-Host ("{0}" -f $PrimaryAssignedUser.memberOf.count) -ForegroundColor Cyan -NoNewline
}Else{
Write-Host ("{0}" -f $PrimaryAssignedUser.memberOf.count) -ForegroundColor Red -NoNewline
}
Write-Host (" groups") -ForegroundColor White
#iterate through each group id for name
Foreach($Group in $PrimaryAssignedUser.memberOf){
Write-Host (" |---Group: ") -NoNewline -ForegroundColor Gray
#check to see if any of the groups are dynamic groups using orderid
Write-Host ("{0}" -f $Group.displayName) -ForegroundColor Green
}
Write-Host (" |---User is assigned to ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $PrimaryAssignedUser.assignedLicenses.count) -ForegroundColor Cyan -NoNewline
Write-Host (" licenses") -ForegroundColor White
<#
If($PSBoundParameters.ContainsKey('CheckUserLicense'))
{
If($TenantAssignedLicenses.count -gt 0){
$HasIntuneLicense = $false
Foreach($License in $TenantAssignedLicenses | Where-Object {$_.skuId -in $PrimaryAssignedUser.assignedLicenses.skuId})
{
If($licenseHashTable.count -gt 0){
$LicenseName = ($licenseHashTable[$License.skuId].GetEnumerator() | Where-Object Name -eq ProductDisplayName).Value
}Else{
$LicenseName = $License.skuPartNumber
}
Write-Host (" |---License: ") -NoNewline -ForegroundColor Gray
#check to see if any of the groups are dynamic groups using orderid
$Color = "Yellow"
If($License.skuId -in $IntuneLicenses.skuId){
$Color = "Cyan"
$HasIntuneLicense = $true
}
If($License.skuId -in $WindowsLicenses.skuId){
$Color = "Cyan"
}
Write-Host ("{0}" -f $LicenseName) -ForegroundColor $Color
}
Write-Host (" |---Intune license assigned: ") -ForegroundColor White -NoNewline
If($HasIntuneLicense){
Write-Host ("Yes") -ForegroundColor Green
}Else{
#Write-Host ("No") -ForegroundColor Red
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-host ("The MDM Policy assigned group does not include the user [{0}]" -f $PrimaryAssignedUser.userPrincipalName) -ForegroundColor Red
Write-host ("REASON: If the specified user [{0}] is not assigned an Intune license; Autopilot will fail during enrollment" -f $PrimaryAssignedUser.userPrincipalName) -ForegroundColor Red
Write-Action ("Assign the user an Intune license and rerun script.") -ExitAsError
}
}
}Else{
Write-Host (" |---Intune license assigned: ") -ForegroundColor White -NoNewline
Write-Host ("Manual check is required!") -ForegroundColor Yellow
}
#>
}
# License Check
#------------------------------------------------------------------------------------------
If(($PrimaryAssignedUser.assignedLicenses.count -gt 0) -and $PSBoundParameters.ContainsKey('CheckUserLicense')){
Write-Host ("`n |---Checking Intune licenses for user [{0}]..." -f $PrimaryAssignedUser.userPrincipalName)
Foreach($AssignedLicense in $PrimaryAssignedUser.assignedLicenses){
# determine license name
If($licenseHashTable.count -gt 0){
$AssignedLicenseName = ($licenseHashTable[$AssignedLicense.skuId].GetEnumerator() | Where-Object Name -eq ProductDisplayName).Value
}Else{
$AssignedLicenseName = ($IntuneLicenses | Where-Object skuId -eq $AssignedLicense.skuId).skuPartNumber
}
# check if license is one of the Intune licenses
If($AssignedLicense.skuId -in ($IntuneLicenses.skuId | Select-Object -Unique)){
Write-Host (" |---User is assigned an Intune license: ") -NoNewline -ForegroundColor Gray
Write-Host ("{0}" -f $AssignedLicenseName) -ForegroundColor Green
$AssignedToIntuneLicense = $true
}Else{
#Write-Host (" |---Assigned license: ") -NoNewline -ForegroundColor Gray
#Write-Host ("{0}" -f $AssignedLicenseName) -ForegroundColor White
}
}
If(!$AssignedToIntuneLicense){
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-host ("No MDM license are assigned to the user [{0}]" -f $PrimaryAssignedUser.userPrincipalName) -ForegroundColor Red
Write-host ("REASON: If the specified user [{0}] is not assigned an Intune license; Autopilot will fail during enrollment" -f $PrimaryAssignedUser.userPrincipalName) -ForegroundColor Red
Write-Action ("Assign the user an Intune license and rerun script.") -ExitAsError
}
}Else{
Write-Host (" |---Intune license assigned: ") -ForegroundColor White -NoNewline
Write-Host ("Manual check is required!") -ForegroundColor Yellow
$ShowWarning = $true
}
# Get all deployment profiles
#------------------------------------------------------------------------------------------
Write-Host ("`n |---Retrieving all Autopilot deployment profiles...") -NoNewline:$noNewLine
Try{
$DeploymentProfiles = (Invoke-MgGraphRequest -Method GET `
-Uri "$script:GraphEndpoint/beta/deviceManagement/windowsAutopilotDeploymentProfiles?`$expand=assignments").Value
}Catch{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Verbose ("{0} " -f $_.Exception.Message)
Write-host ("REASON: Your graph permissions are not allowing you to read device groups from Azure AD.") -ForegroundColor Red
Write-Action ("Ensure you have permissions [DeviceManagementServiceConfig.Read.All] from graph and rerun script") -ExitAsError
}
$deploymentProfileList = Get-AssignmentList -GraphRequestPayload $DeploymentProfiles
If($deploymentProfileList.count -gt 0){
Write-Host ("{0}" -f (Get-Symbol -Symbol GreenCheckmark))
Write-Host (" |---Deployment Profiles found: ") -ForegroundColor White -NoNewline
Write-Host ("{0}" -f $deploymentProfileList.count) -ForegroundColor Cyan
}Else{
Write-Host ("{0}" -f (Get-Symbol -Symbol RedX)) -ForegroundColor Red
Write-Host ("No Deployment profiles were found!") -ForegroundColor Red
Write-host ("REASON: If there are no Autopilot deployment profiles created and assigned, the device will not be Autopilot ready!") -ForegroundColor Red
Write-Action ("Create an Autopilot deployment profile, assign it, and rerun script.") -ExitAsError
}
# get deployment profile assignments
#------------------------------------------------------------------------------------------
Write-Host ("`n |---Determining if Autopilot deployment profile is assigned to device...") -NoNewline:$NoNewLine
#$AssignmentPayload=$deploymentProfileList;$DeviceGroupIds=$assignedDeviceGroups.Id;$UserGroupIds=$PrimaryAssignedUser.memberOf.id