forked from ugurkocde/IntuneAssignmentChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntuneAssignmentChecker.ps1
More file actions
1300 lines (1040 loc) · 69.8 KB
/
IntuneAssignmentChecker.ps1
File metadata and controls
1300 lines (1040 loc) · 69.8 KB
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
# Intune Assignment Checker
# Author: Ugur Koc (Socials: @ugurkocde)
# Description: This script checks the assignments of Intune Configuration Policies and Device Configurations based on the groups that the user or device is a member of in Microsoft Entra (formerly Azure AD).
# Disclaimer: This script is provided AS IS without warranty of any kind. I am not responsible for any damage caused by this script. Use it at your own risk.
# You have to create an App Registration in Entra ID and grant the necessary permissions to the script.
# Note: Only Read Permissions are necessary. This script does not make any changes to the assignments or in Intune in general.
# Permissions required for the script: User.Read.All, Group.Read.All, DeviceManagementConfiguration.Read.All, DeviceManagementManagedDevices.Read.All, Device.Read.All
################################ Prerequisites #####################################################
# Fill in your App ID, Tenant ID, and Secret
$appid = '<YourAppIdHere>' # App ID of the App Registration
$tenantid = '<YourTenantIdHere>' # Tenant ID of your EntraID
$secret = '<YourAppSecretHere>' # Secret of the App Registration
####################################################################################################
# Autoupdate function
# Version of the local script
$localVersion = "1.1.0"
# URL to the version file on GitHub
$versionUrl = "https://raw.githubusercontent.com/ugurkocde/IntuneAssignmentChecker/main/version.txt"
# URL to the latest script on GitHub
$scriptUrl = "https://raw.githubusercontent.com/ugurkocde/IntuneAssignmentChecker/main/IntuneAssignmentChecker.ps1"
# Determine the script path based on whether it's run as a file or from an IDE
if ($PSScriptRoot) {
$newScriptPath = Join-Path $PSScriptRoot "IntuneAssignmentChecker.ps1"
} else {
$currentDirectory = Get-Location
$newScriptPath = Join-Path $currentDirectory "IntuneAssignmentChecker.ps1"
}
# Flag to control auto-update behavior
$autoUpdate = $true # Set to $false to disable auto-update
try {
# Fetch the latest version number from GitHub
$latestVersion = Invoke-RestMethod -Uri $versionUrl
# Compare the local version with the latest version
if ($localVersion -ne $latestVersion) {
Write-Host "There is a new version available: $latestVersion. You are running $localVersion." -ForegroundColor Green
if ($autoUpdate) {
Write-Host "AutoUpdate is enabled. Downloading the latest version..." -ForegroundColor Yellow
try {
# Download the latest version of the script
Invoke-WebRequest -Uri $scriptUrl -OutFile $newScriptPath
Write-Host "The latest version has been downloaded to $newScriptPath" -ForegroundColor Yellow
Write-Host "Please restart the script to use the updated version." -ForegroundColor Yellow
} catch {
Write-Host "An error occurred while downloading the latest version. Please download it manually from: https://github.com/ugurkocde/IntuneAssignmentChecker" -ForegroundColor Red
}
} else {
Write-Host "Auto-update is disabled. Please download the latest version manually from: https://github.com/ugurkocde/IntuneAssignmentChecker" -ForegroundColor Yellow
}
}
} catch {
Write-Host "Could not check for updates. Please ensure you have an internet connection and try again. If the issue persists, please download the latest version manually from: https://github.com/ugurkocde/IntuneAssignmentChecker" -ForegroundColor Red
}
####################################################################################################
# Do not change the following code
# Check if any of the variables are not set or contain placeholder values
if (-not $appid -or $appid -eq '<YourAppIdHere>' -or
-not $tenantid -or $tenantid -eq '<YourTenantIdHere>' -or
-not $secret -or $secret -eq '<YourAppSecretHere>') {
Write-Host "App ID, Tenant ID, or Secret is missing or not set correctly. Please fill out all the necessary details." -ForegroundColor Red
exit
}
$body = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $appid
Client_Secret = $secret
}
$connection = Invoke-RestMethod `
-Uri https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token `
-Method POST `
-Body $body
$token = $connection.access_token
$secureToken = ConvertTo-SecureString $token -AsPlainText -Force
Connect-MgGraph -AccessToken $secureToken -NoWelcome
# Loop until the user decides to exit
do {
# Main Menu for selection
Write-Host "Select the type of entity you want to check the Assignments for in Intune:" -ForegroundColor Cyan
Write-Host "1. User(s)" -ForegroundColor Yellow
Write-Host "2. Group(s)" -ForegroundColor Yellow
Write-Host "3. Device(s)" -ForegroundColor Yellow
Write-Host "4. Show all 'All User' Assignments" -ForegroundColor Yellow
Write-Host "5. Show all 'All Device' Assignments" -ForegroundColor Yellow
Write-Host "6. Check Permissions" -ForegroundColor Yellow
Write-Host "7. Exit" -ForegroundColor Red
$selection = Read-Host "Please enter your choice (1, 2, 3, 4, 5, 6 or 7)"
switch ($selection) {
'1' {
Write-Host "User selection chosen" -ForegroundColor Green
# User:
# Check groups that the user is a member of in Microsoft Entra (formerly Azure AD)
## User - Get Microsoft Entra User ID based on the User Principal Name (UPN)
# Permission: User.Read.All
# Prompt for User Principal Name (UPN)
Write-Host "Please enter the User Principal Name(s), separated by commas (,): " -ForegroundColor Cyan
$userPrincipalNamesInput = Read-Host
$userPrincipalNames = $userPrincipalNamesInput -split ',' | ForEach-Object { $_.Trim() }
foreach ($userPrincipalName in $userPrincipalNames) {
Write-Host "Checking following User: $userPrincipalName" -ForegroundColor Yellow
# Get User ID from Microsoft Entra based on UPN
$userDetailsUri = "https://graph.microsoft.com/v1.0/users?`$filter=userPrincipalName eq '$userPrincipalName'"
$userResponse = Invoke-MgGraphRequest -Uri $userDetailsUri -Method Get
$userId = $userResponse.value.id
if ($userId) {
Write-Host "User Found! -> User ID: $userId" -ForegroundColor Green
}
else {
Write-Host "User Not Found: $userPrincipalName" -ForegroundColor Red
return
}
# Get User Group Memberships
$transitiveGroupsUri = "https://graph.microsoft.com/v1.0/users/$userId/transitiveMemberOf?$select=id,displayName"
$groupResponse = Invoke-MgGraphRequest -Uri $transitiveGroupsUri -Method Get
$userGroupIds = $groupResponse.value | ForEach-Object { $_.id }
$userGroupNames = $groupResponse.value | ForEach-Object { $_.displayName }
Write-Host "User Group Memberships: $($userGroupNames -join ', ')" -ForegroundColor Green
Write-Host "Fetching Intune Profiles and Applications for the user ... (this takes a few seconds)" -ForegroundColor Yellow
# Initialize collections to hold relevant policies and applications
$userRelevantPolicies = @()
$userRelevantCompliancePolicies = @()
$userRelevantAppsRequired = @()
$userRelevantAppsAvailable = @()
$userRelevantAppsUninstall = @()
# Define URIs for Intune Configuration Policies, Device Configurations, Compliance Policies, and Applications
$policiesUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies"
$deviceConfigsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations"
$groupPolicyUri = "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations"
$complianceUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies"
$appUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps"
# Get Intune Configuration Policies
$policiesResponse = Invoke-MgGraphRequest -Uri $policiesUri -Method Get
# Check each configuration policy for assignments that match user's groups
foreach ($policy in $policiesResponse.value) {
$policyName = $policy.name
$policyId = $policy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies('$policyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allLicensedUsersAssignmentTarget') {
$assignmentReason = "All Users"
}
elseif ($userGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Attach the assignment reason to the policy
Add-Member -InputObject $policy -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$userRelevantPolicies += $policy
break
}
}
}
# Get Intune Group Policy Configurations
$groupPoliciesResponse = Invoke-MgGraphRequest -Uri $groupPolicyUri -Method Get
# Check each group policy for assignments that match user's groups
foreach ($grouppolicy in $groupPoliciesResponse.value) {
$groupPolicyName = $grouppolicy.displayName
$groupPolicyId = $grouppolicy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations('$groupPolicyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allLicensedUsersAssignmentTarget') {
$assignmentReason = "All Users"
}
elseif ($userGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
Add-Member -InputObject $grouppolicy -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$userRelevantPolicies += $grouppolicy
break
}
}
}
# Get Intune Device Configurations
$deviceConfigsResponse = Invoke-MgGraphRequest -Uri $deviceConfigsUri -Method Get
# Check each device configuration for assignments that match user's groups or all licensed users
foreach ($config in $deviceConfigsResponse.value) {
$configName = $config.displayName
$configId = $config.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations('$configId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allLicensedUsersAssignmentTarget') {
$assignmentReason = "All Users"
}
elseif ($userGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Attach the assignment reason to the config object
Add-Member -InputObject $config -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$userRelevantPolicies += $config
break
}
}
}
# Get Intune Compliance Policies
$complianceResponse = Invoke-MgGraphRequest -Uri $complianceUri -Method Get
# Check each compliance policy for assignments that match user's groups
foreach ($compliancepolicy in $complianceResponse.value) {
$compliancepolicyName = $compliancepolicy.displayName
$compliancepolicyId = $compliancepolicy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies('$compliancepolicyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
if ($userGroupIds -contains $assignment.target.groupId) {
$userRelevantCompliancePolicies += $compliancepolicy
break
}
}
}
# Get Intune Applications
$appResponse = Invoke-MgGraphRequest -Uri $appUri -Method Get
# Iterate over each application
foreach ($app in $appResponse.value) {
$appName = $app.displayName
$appId = $app.id
# Construct the URI to get assignments for the current app
$assignmentsUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps('$appId')/assignments"
# Fetch the assignments for the app
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
# Iterate over each assignment to check if the user's groups are targeted
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allLicensedUsersAssignmentTarget') {
$assignmentReason = "All Users"
}
elseif ($userGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Add a new property to the app object to store the assignment reason
Add-Member -InputObject $app -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
switch ($assignment.intent) {
"required" {
$userRelevantAppsRequired += $app
if ($assignmentReason -eq "All Users") { break }
}
"available" {
$userRelevantAppsAvailable += $app
if ($assignmentReason -eq "All Users") { break }
}
"uninstall" {
$userRelevantAppsUninstall += $app
if ($assignmentReason -eq "All Users") { break }
}
}
}
}
}
Write-Host "Intune Profiles and Apps have been successfully fetched for the user." -ForegroundColor Green
# Generating Results for User
Write-Host "Generating Results for $userPrincipalName..." -ForegroundColor Yellow
Start-Sleep -Seconds 1
Write-Host "Here are the Assignments for the User: $userPrincipalName" -ForegroundColor Green
# Separator and heading for Assigned Profiles
Write-Host "------- Assigned Configuration Profiles -------" -ForegroundColor Cyan
foreach ($policy in $userRelevantPolicies) {
$policyName = if ([string]::IsNullOrWhiteSpace($policy.name)) { $policy.displayName } else { $policy.name }
$policyId = $policy.id
$assignmentReason = $policy.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Users") {
Write-Host "Configuration Profile Name: $policyName, Policy ID: $policyId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
# If the assignment reason is not "All Users", don't include the assignment reason in the output
Write-Host "Configuration Profile Name: $policyName, Policy ID: $policyId" -ForegroundColor White
}
}
# Separator and heading for Compliance Policies
Write-Host "------- Assigned Compliance Policies -------" -ForegroundColor Cyan
foreach ($compliancepolicy in $userRelevantCompliancePolicies) {
$compliancepolicyName = if ([string]::IsNullOrWhiteSpace($compliancepolicy.name)) { $compliancepolicy.displayName } else { $compliancepolicy.name }
$compliancepolicyId = $compliancepolicy.id
$assignmentReason = $compliancepolicy.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Users") {
Write-Host "Compliance Policy Name: $compliancepolicyName, Policy ID: $compliancepolicyId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "Compliance Policy Name: $compliancepolicyName, Policy ID: $compliancepolicyId" -ForegroundColor White
}
}
# Separator and heading for Assigned Apps (Required)
Write-Host "------- Assigned Apps (Required) -------" -ForegroundColor Cyan
foreach ($app in $userRelevantAppsRequired) {
$appName = if ([string]::IsNullOrWhiteSpace($app.name)) { $app.displayName } else { $app.name }
$appId = $app.id
$assignmentReason = $app.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Users") {
Write-Host "App Name: $appName, App ID: $appId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
# Separator and heading for Assigned Apps (Available)
Write-Host "------- Assigned Apps (Available) -------" -ForegroundColor Cyan
foreach ($app in $userRelevantAppsAvailable) {
$appName = if ([string]::IsNullOrWhiteSpace($app.name)) { $app.displayName } else { $app.name }
$appId = $app.id
$assignmentReason = $app.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Users") {
Write-Host "App Name: $appName, App ID: $appId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
# Separator and heading for Assigned Apps (Uninstall)
Write-Host "------- Assigned Apps (Uninstall) -------" -ForegroundColor Cyan
foreach ($app in $deviceRelevantAppsUninstall) {
$appName = if ([string]::IsNullOrWhiteSpace($app.name)) { $app.displayName } else { $app.name }
$appId = $app.id
$assignmentReason = $app.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Users") {
Write-Host "App Name: $appName, App ID: $appId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
}
}
'2' {
Write-Host "Group selection chosen" -ForegroundColor Green
# Prompt for one or more Device Names
Write-Host "Please enter Entra ID Group Names(s), separated by commas (,): " -ForegroundColor Cyan
$GroupNamesInput = Read-Host
$GroupNames = $GroupNamesInput -split ',' | ForEach-Object { $_.Trim() }
foreach ($GroupName in $GroupNames) {
Write-Host "Checking following Group: $GroupName" -ForegroundColor Yellow
# Get Device ID from Azure AD based on Display Name
$groupUri = "https://graph.microsoft.com/v1.0/groups?`$filter=displayName eq '$GroupName'"
$groupResponse = Invoke-MgGraphRequest -Uri $groupUri -Method Get
$entragroupId = $groupResponse.value.id
if ($entragroupId) {
Write-Host "Group Found! -> Microsoft Entra Group ID: $entragroupId " -ForegroundColor Green
}
else {
Write-Host "Group Not Found: $GroupName" -ForegroundColor Red
continue
}
# Get Group Members
$transitiveGroupsUri = "https://graph.microsoft.com/v1.0/groups/$entragroupId/transitiveMembers"
$response = Invoke-MgGraphRequest -Uri $transitiveGroupsUri -Method Get
$entraGroupIdMembers = $response.value | ForEach-Object {
if ($_.userPrincipalName) {
# If userPrincipalName exists, it's a user, so output userPrincipalName
$_.userPrincipalName
}
elseif ($_.displayName) {
# If displayName exists but userPrincipalName doesn't, it's a device, so output displayName
$_.displayName
}
}
# Sort the array alphabetically
$sortedMembers = $entraGroupIdMembers | Sort-Object
# Join the sorted array into a string with each member on a new line
$membersList = $sortedMembers -join ', '
Write-Host "Group Members: $membersList" -ForegroundColor Green
Write-Host "Fetching Intune Profiles and Applications for the Group ... (this takes a few seconds)" -ForegroundColor Yellow
# Initialize collections to hold relevant policies and applications
$GroupRelevantPolicies = @()
$GroupRelevantCompliancePolicies = @()
$GroupRelevantAppsRequired = @()
$GroupRelevantAppsAvailable = @()
# Define URIs for Intune Configuration Policies, Device Configurations, Compliance Policies, and Applications
$policiesUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies"
$deviceConfigsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations"
$complianceUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies"
$appUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps"
# Get Intune Configuration Policies
$policiesResponse = Invoke-MgGraphRequest -Uri $policiesUri -Method Get
# Check each configuration policy for assignments that match device groups
foreach ($policy in $policiesResponse.value) {
$policyName = $policy.name
$policyId = $policy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies('$policyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
if ($entragroupId -contains $assignment.target.groupId) {
$GroupRelevantPolicies += $policy
break
}
}
}
# Get Intune Device Configurations
$deviceConfigsResponse = Invoke-MgGraphRequest -Uri $deviceConfigsUri -Method Get
# Check each device configuration for assignments that match devices groups
foreach ($config in $deviceConfigsResponse.value) {
$configName = $config.displayName
$configId = $config.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations('$configId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
if ($entragroupId -contains $assignment.target.groupId) {
$GroupRelevantPolicies += $config
break
}
}
}
# Get Intune Compliance Policies
$complianceResponse = Invoke-MgGraphRequest -Uri $complianceUri -Method Get
# Check each compliance policy for assignments that match devices groups
foreach ($compliancepolicy in $complianceResponse.value) {
$compliancepolicyName = $compliancepolicy.displayName
$compliancepolicyId = $compliancepolicy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies('$compliancepolicyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
if ($entragroupId -contains $assignment.target.groupId) {
$GroupRelevantCompliancePolicies += $compliancepolicy
break
}
}
}
# Get Intune Applications
$appResponse = Invoke-MgGraphRequest -Uri $appUri -Method Get
# Iterate over each application
foreach ($app in $appResponse.value) {
$appName = $app.displayName
$appId = $app.id
# Construct the URI to get assignments for the current app
$assignmentsUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps('$appId')/assignments"
# Fetch the assignments for the app
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
# Iterate over each assignment to check if the devices groups are targeted
foreach ($assignment in $assignmentResponse.value) {
if ($entragroupId -contains $assignment.target.groupId) {
if ($assignment.intent -eq "required") {
$GroupRelevantAppsRequired += $app
# Break out of the loop once a relevant app is found to avoid duplicates
break
}
elseif ($assignment.intent -eq "available") {
$GroupRelevantAppsAvailable += $app
# Continue checking in case the app has both "required" and "available" intents for different groups
}
elseif ($assignment.intent -eq "uninstall") {
$GroupRelevantAppsUninstall += $app
# Continue checking in case the app has both "required" and "available" intents for different groups
}
}
}
}
Write-Host "Intune Profiles and Apps have been successfully fetched for the device." -ForegroundColor Green
# Generating Results for the Device
Write-Host "Generating Results for $DeviceName..." -ForegroundColor Yellow
Start-Sleep -Seconds 1
Write-Host "Here are the Assignments for the Device: $DeviceName" -ForegroundColor Green
# Separator and heading for Assigned Profiles
Write-Host "------- Assigned Configuration Profiles -------" -ForegroundColor Cyan
foreach ($policy in $GroupRelevantPolicies) {
$policyName = if ([string]::IsNullOrWhiteSpace($policy.name)) { $policy.displayName } else { $policy.name }
$policyId = $policy.id
Write-Host "Configuration Profile Name: $policyName, Policy ID: $($policyId)" -ForegroundColor White
}
# Separator and heading for Compliance Policies
Write-Host "------- Assigned Compliance Policies -------" -ForegroundColor Cyan
foreach ($compliancepolicy in $GroupRelevantCompliancePolicies) {
# Check if displayName is not null or empty, otherwise use name
$compliancepolicyName = $compliancepolicy.displayName
$compliancepolicyId = $compliancepolicy.id
Write-Host "Compliance Policy Name: $compliancepolicyName, App ID: $compliancepolicyId" -ForegroundColor White
}
# Separator and heading for Assigned Apps
Write-Host "------- Assigned Apps (Required) -------" -ForegroundColor Cyan
foreach ($app in $GroupRelevantAppsRequired) {
$appName = $app.displayName
$appId = $app.id
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
# Separator and heading for Assigned Apps
Write-Host "------- Assigned Apps (Available) -------" -ForegroundColor Cyan
foreach ($app in $GroupRelevantAppsAvailable) {
$appName = $app.displayName
$appId = $app.id
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
}
'3' {
Write-Host "Device selection chosen" -ForegroundColor Green
## Devices:
# Check groups that the device is member of in EntraID.
## Device - Get Entra device id based on the device display name
# Endpoint: https://graph.microsoft.com/v1.0/devices?$filter=displayName eq 'DeviceName'
# Permission: Device.Read.All
# Prompt for one or more Device Names
Write-Host "Please enter Device Name(s), separated by commas (,): " -ForegroundColor Cyan
$deviceNamesInput = Read-Host
$deviceNames = $deviceNamesInput -split ',' | ForEach-Object { $_.Trim() }
foreach ($DeviceName in $deviceNames) {
Write-Host "Checking following DeviceName: $DeviceName" -ForegroundColor Yellow
# Get Device ID from Azure AD based on Display Name
$deviceUri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$filter=deviceName eq '$DeviceName'"
$deviceResponse = Invoke-MgGraphRequest -Uri $deviceUri -Method Get
$entradeviceId = $deviceResponse.value.azureADDeviceId
if ($entradeviceId) {
Write-Host "Device Found! -> Microsoft Entra Device ID: $entradeviceId " -ForegroundColor Green
}
else {
Write-Host "Device Not Found: $DeviceName" -ForegroundColor Red
continue
}
# Get Device Group Memberships
$transitiveGroupsUri = "https://graph.microsoft.com/v1.0/devices(deviceId='$entradeviceId')/transitiveMemberOf?$select=id"
$response = Invoke-MgGraphRequest -Uri $transitiveGroupsUri -Method Get
# Collect all group IDs
$entradeviceGroupIds = $response.value | ForEach-Object { $_.id }
$entradeviceGroupNames = $response.value | ForEach-Object { $_.displayName }
Write-Host "Device Group Memberships: $($entradeviceGroupNames -join ', ')" -ForegroundColor Green
Write-Host "Fetching Intune Profiles and Applications for the device ... (this takes a few seconds)" -ForegroundColor Yellow
# Initialize collections to hold relevant policies and applications
$deviceRelevantPolicies = @()
$deviceRelevantCompliancePolicies = @()
$deviceRelevantAppsRequired = @()
$deviceRelevantAppsAvailable = @()
$deviceRelevantAppsUninstall = @()
# Define URIs for Intune Configuration Policies, Device Configurations, Compliance Policies, and Applications
$policiesUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies"
$deviceConfigsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations"
$groupPolicyUri = "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations"
$complianceUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies"
$appUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps"
# Get Intune Configuration Policies
$policiesResponse = Invoke-MgGraphRequest -Uri $policiesUri -Method Get
# Check each configuration policy for assignments that match the device
foreach ($policy in $policiesResponse.value) {
$policyName = $policy.name
$policyId = $policy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies('$policyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allDevicesAssignmentTarget') {
$assignmentReason = "All Devices"
}
elseif ($entradeviceGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Attach the assignment reason to the policy
Add-Member -InputObject $policy -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$deviceRelevantPolicies += $policy
break
}
}
}
# Get Intune Group Policy Configurations
$groupPoliciesResponse = Invoke-MgGraphRequest -Uri $groupPolicyUri -Method Get
# Check each group policy for assignments that match user's groups
foreach ($grouppolicy in $groupPoliciesResponse.value) {
$groupPolicyName = $grouppolicy.displayName
$groupPolicyId = $grouppolicy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations('$groupPolicyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allDevicesAssignmentTarget') {
$assignmentReason = "All Devices"
}
elseif ($entradeviceGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
Add-Member -InputObject $grouppolicy -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$deviceRelevantPolicies += $grouppolicy
break
}
}
}
# Get Intune Device Configurations
$deviceConfigsResponse = Invoke-MgGraphRequest -Uri $deviceConfigsUri -Method Get
# Check each device configuration for assignments that match devices groups or are assigned to all users or all devices
foreach ($config in $deviceConfigsResponse.value) {
$configName = $config.displayName
$configId = $config.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations('$configId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allDevicesAssignmentTarget') {
$assignmentReason = "All Devices"
}
elseif ($entradeviceGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Attach the assignment reason to the config object
Add-Member -InputObject $config -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$deviceRelevantPolicies += $config
break
}
}
}
# Get Intune Compliance Policies
$complianceResponse = Invoke-MgGraphRequest -Uri $complianceUri -Method Get
# Check each compliance policy for assignments that match devices groups
foreach ($compliancepolicy in $complianceResponse.value) {
$compliancepolicyName = $compliancepolicy.displayName
$compliancepolicyId = $compliancepolicy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies('$compliancepolicyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allDevicesAssignmentTarget') {
$assignmentReason = "All Devices"
}
elseif ($entradeviceGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Attach the assignment reason to the compliance policy
Add-Member -InputObject $compliancepolicy -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
$deviceRelevantCompliancePolicies += $compliancepolicy
break
}
}
}
# Get Intune Applications
$appResponse = Invoke-MgGraphRequest -Uri $appUri -Method Get
# Iterate over each application
foreach ($app in $appResponse.value) {
$appName = $app.displayName
$appId = $app.id
# Construct the URI to get assignments for the current app
$assignmentsUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps('$appId')/assignments"
# Fetch the assignments for the app
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
# Iterate over each assignment to check if the devices groups are targeted
foreach ($assignment in $assignmentResponse.value) {
$assignmentReason = $null # Clear previous reason
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allDevicesAssignmentTarget') {
$assignmentReason = "All Devices"
}
elseif ($entradeviceGroupIds -contains $assignment.target.groupId) {
$assignmentReason = "Group Assignment"
}
if ($assignmentReason) {
# Add a new property to the app object to store the assignment reason
Add-Member -InputObject $app -NotePropertyName 'AssignmentReason' -NotePropertyValue $assignmentReason -Force
switch ($assignment.intent) {
"required" {
$deviceRelevantAppsRequired += $app
if ($assignmentReason -eq "All Devices") { break }
}
"available" {
$deviceRelevantAppsAvailable += $app
if ($assignmentReason -eq "All Devices") { break }
}
"uninstall" {
$deviceRelevantAppsUninstall += $app
if ($assignmentReason -eq "All Devices") { break }
}
}
}
}
}
Write-Host "Intune Profiles and Apps have been successfully fetched for the device." -ForegroundColor Green
# Generating Results for the Device
Write-Host "Generating Results for $DeviceName..." -ForegroundColor Yellow
Start-Sleep -Seconds 1
Write-Host "Here are the Assignments for the Device: $DeviceName" -ForegroundColor Green
# Separator and heading for Assigned Profiles
Write-Host "------- Assigned Configuration Profiles -------" -ForegroundColor Cyan
foreach ($policy in $deviceRelevantPolicies) {
$policyName = if ([string]::IsNullOrWhiteSpace($policy.name)) { $policy.displayName } else { $policy.name }
$policyId = $policy.id
$assignmentReason = $policy.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Devices") {
Write-Host "Configuration Profile Name: $policyName, Policy ID: $policyId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
# If the assignment reason is not "All Devices", don't include the assignment reason in the output
Write-Host "Configuration Profile Name: $policyName, Policy ID: $policyId" -ForegroundColor White
}
}
# Separator and heading for Compliance Policies
Write-Host "------- Assigned Compliance Policies -------" -ForegroundColor Cyan
foreach ($compliancepolicy in $deviceRelevantCompliancePolicies) {
$compliancepolicyName = if ([string]::IsNullOrWhiteSpace($compliancepolicy.name)) { $compliancepolicy.displayName } else { $compliancepolicy.name }
$compliancepolicyId = $compliancepolicy.id
$assignmentReason = $compliancepolicy.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Devices") {
Write-Host "Compliance Policy Name: $compliancepolicyName, Policy ID: $compliancepolicyId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "Compliance Policy Name: $compliancepolicyName, Policy ID: $compliancepolicyId" -ForegroundColor White
}
}
# Separator and heading for Assigned Apps (Required)
Write-Host "------- Assigned Apps (Required) -------" -ForegroundColor Cyan
foreach ($app in $deviceRelevantAppsRequired) {
$appName = if ([string]::IsNullOrWhiteSpace($app.name)) { $app.displayName } else { $app.name }
$appId = $app.id
$assignmentReason = $app.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Devices") {
Write-Host "App Name: $appName, App ID: $appId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
# Separator and heading for Assigned Apps (Available)
Write-Host "------- Assigned Apps (Available) -------" -ForegroundColor Cyan
foreach ($app in $deviceRelevantAppsAvailable) {
$appName = if ([string]::IsNullOrWhiteSpace($app.name)) { $app.displayName } else { $app.name }
$appId = $app.id
$assignmentReason = $app.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Devices") {
Write-Host "App Name: $appName, App ID: $appId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
# Separator and heading for Assigned Apps (Uninstall)
Write-Host "------- Assigned Apps (Uninstall) -------" -ForegroundColor Cyan
foreach ($app in $deviceRelevantAppsUninstall) {
$appName = if ([string]::IsNullOrWhiteSpace($app.name)) { $app.displayName } else { $app.name }
$appId = $app.id
$assignmentReason = $app.AssignmentReason
# Output formatting based on assignment reason
if ($assignmentReason -eq "All Devices") {
Write-Host "App Name: $appName, App ID: $appId, Assignment Reason: $assignmentReason" -ForegroundColor White
}
else {
Write-Host "App Name: $appName, App ID: $appId" -ForegroundColor White
}
}
}
}
'4' {
Write-Host "'Show all `All User` Assignments' chosen" -ForegroundColor Green
Write-Host "Fetching Intune Profiles and Applications ... (this takes a few seconds)" -ForegroundColor Yellow
# Initialize collections to hold relevant policies and applications
$allUserPolicies = @()
$allUserDeviceConfigs = @()
$allUserCompliancePolicies = @()
$allUserApps = @()
# Define URIs for Intune Configuration Policies, Device Configurations, Compliance Policies, and Applications
$policiesUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies"
$deviceConfigsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations"
$complianceUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies"
$appUri = "https://graph.microsoft.com/beta/deviceAppManagement/mobileApps"
# Fetch and process Configuration Policies
$policiesResponse = Invoke-MgGraphRequest -Uri $policiesUri -Method Get
foreach ($policy in $policiesResponse.value) {
$policyId = $policy.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/configurationPolicies('$policyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allLicensedUsersAssignmentTarget') {
$allUserPolicies += $policy
break
}
}
}
# Fetch and process Device Configurations
$deviceConfigsResponse = Invoke-MgGraphRequest -Uri $deviceConfigsUri -Method Get
foreach ($config in $deviceConfigsResponse.value) {
$configId = $config.id
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations('$configId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get
foreach ($assignment in $assignmentResponse.value) {
if ($assignment.target.'@odata.type' -eq '#microsoft.graph.allLicensedUsersAssignmentTarget') {
$allUserPolicies += $policy
break
}
}
}
# Fetch and process Compliance Policies
$complianceResponse = Invoke-MgGraphRequest -Uri $complianceUri -Method Get
foreach ($compliancepolicy in $complianceResponse.value) {
$compliancepolicyId = $compliancepolicy.id
$compliancepolicyName = $compliancepolicy.displayName
$assignmentsUri = "https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies('$compliancepolicyId')/assignments"
$assignmentResponse = Invoke-MgGraphRequest -Uri $assignmentsUri -Method Get