|
| 1 | +# This sample script displays users and groups assigned to the specified Application Proxy application. |
| 2 | +# |
| 3 | +# .\display-users-group-of-an-app.ps1 -ObjectId <ObjectId of the application> |
| 4 | +# |
| 5 | +# This script requires PowerShell 5.1 (x64) and one of the following modules: |
| 6 | +# AzureAD 2.0.2.52 |
| 7 | +# AzureADPreview 2.0.2.53 |
| 8 | +# |
| 9 | +# Before you begin: |
| 10 | +# Run Connect-AzureAD to connect to the tenant domain. |
| 11 | +# Required Azure AD role: Global Administrator or Application Administrator |
| 12 | + |
| 13 | +param( |
| 14 | +[string] $ObjectId = "null" |
| 15 | +) |
| 16 | + |
| 17 | +$aadapServPrincObjId=$ObjectId |
| 18 | + |
| 19 | +If ($aadapServPrincObjId -eq "null") { |
| 20 | + |
| 21 | + Write-Host "Parameter is missing." -BackgroundColor "Black" -ForegroundColor "Green" |
| 22 | + Write-Host " " |
| 23 | + Write-Host ".\display-users-group-of-an-app.ps1 -ObjectId <ObjectId of the application>" -BackgroundColor "Black" -ForegroundColor "Green" |
| 24 | + Write-Host " " |
| 25 | + |
| 26 | + Exit |
| 27 | +} |
| 28 | + |
| 29 | +Write-Host "Reading users. This operation might take longer..." -BackgroundColor "Black" -ForegroundColor "Green" |
| 30 | + |
| 31 | +$users=Get-AzureADUser -Top 1000000 |
| 32 | + |
| 33 | +Write-Host "Reading groups. This operation might take longer..." -BackgroundColor "Black" -ForegroundColor "Green" |
| 34 | + |
| 35 | +$groups = Get-AzureADGroup -Top 1000000 |
| 36 | + |
| 37 | +$aadapApp = $aadapServPrinc | ForEach-Object { $allApps -match $_.AppId } |
| 38 | + |
| 39 | +Write-Host "Displaying users and groups assigned to the specified Application Proxy application..." -BackgroundColor "Black" -ForegroundColor "Green" |
| 40 | +Write-Host " " |
| 41 | + |
| 42 | +try { $app = Get-AzureADServicePrincipal -ObjectId $aadapServPrincObjId} |
| 43 | + |
| 44 | +catch { |
| 45 | + |
| 46 | + Write-Host "Possibly the ObjetId is incorrect." -BackgroundColor "Black" -ForegroundColor "Red" |
| 47 | + Write-Host " " |
| 48 | + |
| 49 | + Exit |
| 50 | +} |
| 51 | + |
| 52 | +Write-Host ("Application: " + $app.DisplayName + "(ServicePrinc. ObjID:" + $aadapServPrincObjId + ")") |
| 53 | +Write-Host ("") |
| 54 | +Write-Host ("Assigned (directly and through group membership) users:") |
| 55 | +Write-Host ("") |
| 56 | + |
| 57 | +$number=0 |
| 58 | + |
| 59 | +foreach ($item in $users) { |
| 60 | + |
| 61 | + $listOfAssignments = Get-AzureADUserAppRoleAssignment -ObjectId $item.ObjectId |
| 62 | + |
| 63 | + $assigned = $false |
| 64 | + |
| 65 | + foreach ($item2 in $listOfAssignments) { If ($item2.ResourceID -eq $aadapServPrincObjId) { $assigned = $true } } |
| 66 | + |
| 67 | + If ($assigned -eq $true) { |
| 68 | + Write-Host ("DisplayName: " + $item.DisplayName + " UPN: " + $item.UserPrincipalName + " ObjectID: " + $item.ObjectID) |
| 69 | + $number = $number + 1 |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +Write-Host ("") |
| 74 | +Write-Host ("Number of (directly and through group membership) users: " + $number) |
| 75 | +Write-Host ("") |
| 76 | +Write-Host ("") |
| 77 | +Write-Host ("Assigned groups:") |
| 78 | +Write-Host ("") |
| 79 | + |
| 80 | +$number=0 |
| 81 | + |
| 82 | +foreach ($item in $groups) { |
| 83 | + |
| 84 | + $listOfAssignments = Get-AzureADGroupAppRoleAssignment -ObjectId $item.ObjectId |
| 85 | + |
| 86 | + $assigned = $false |
| 87 | + |
| 88 | + foreach ($item2 in $listOfAssignments) { If ($item2.ResourceID -eq $aadapServPrincObjId) { $assigned = $true } } |
| 89 | + |
| 90 | + If ($assigned -eq $true) { |
| 91 | + Write-Host ("DisplayName: " + $item.DisplayName + " ObjectID: " + $item.ObjectID) |
| 92 | + $number=$number+1 |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +Write-Host ("") |
| 98 | +Write-Host ("Number of assigned groups: " + $number) |
| 99 | +Write-Host ("") |
| 100 | + |
| 101 | +Write-Host ("") |
| 102 | +Write-Host ("Finished.") -BackgroundColor "Black" -ForegroundColor "Green" |
| 103 | +Write-Host ("") |
| 104 | + |
0 commit comments