|
1 | | -# Copyright (c) Microsoft Corporation. All rights reserved. |
2 | | -# Licensed under the MIT license. |
3 | | - |
4 | | -# <ScriptBody> |
5 | | -param( |
6 | | - [Parameter(Mandatory=$true, |
7 | | - HelpMessage="The app ID of the app registration")] |
8 | | - [String] |
9 | | - $AppId, |
10 | | - |
11 | | - [Parameter(Mandatory=$true, |
12 | | - HelpMessage="The application permission scopes to configure on the app registration")] |
13 | | - [String[]] |
14 | | - $GraphScopes, |
15 | | - |
16 | | - [Parameter(Mandatory=$false)] |
17 | | - [Switch] |
18 | | - $StayConnected = $false |
19 | | -) |
20 | | - |
21 | | -$graphAppId = "00000003-0000-0000-c000-000000000000" |
22 | | - |
23 | | -# Requires an admin |
24 | | -Connect-MgGraph -Scopes "Application.ReadWrite.All AppRoleAssignment.ReadWrite.All User.Read" ` |
25 | | - -UseDeviceAuthentication -ErrorAction Stop |
26 | | - |
27 | | -# Get context for access to tenant ID |
28 | | -$context = Get-MgContext -ErrorAction Stop |
29 | | - |
30 | | -# Get the application and service principal |
31 | | -$appRegistration = Get-MgApplication -Filter ("appId eq '" + $AppId +"'") -ErrorAction Stop |
32 | | -$appServicePrincipal = Get-MgServicePrincipal -Filter ("appId eq '" + $AppId + "'") -ErrorAction Stop |
33 | | - |
34 | | -# Lookup available Graph application permissions |
35 | | -$graphServicePrincipal = Get-MgServicePrincipal -Filter ("appId eq '" + $graphAppId + "'") -ErrorAction Stop |
36 | | -$graphAppPermissions = $graphServicePrincipal.AppRoles |
37 | | - |
38 | | -$resourceAccess = @() |
39 | | - |
40 | | -foreach($scope in $GraphScopes) |
41 | | -{ |
42 | | - $permission = $graphAppPermissions | Where-Object { $_.Value -eq $scope } |
43 | | - if ($permission) |
44 | | - { |
45 | | - $resourceAccess += @{ Id = $permission.Id; Type = "Role"} |
46 | | - } |
47 | | - else |
48 | | - { |
49 | | - Write-Host -ForegroundColor Red "Invalid scope:" $scope |
50 | | - Exit |
51 | | - } |
52 | | -} |
53 | | - |
54 | | -# Add the permissions to required resource access |
55 | | -Update-MgApplication -ApplicationId $appRegistration.Id -RequiredResourceAccess ` |
56 | | - @{ ResourceAppId = $graphAppId; ResourceAccess = $resourceAccess } -ErrorAction Stop |
57 | | -Write-Host -ForegroundColor Cyan "Added application permissions to app registration" |
58 | | - |
59 | | -# Add admin consent |
60 | | -foreach ($appRole in $resourceAccess) |
61 | | -{ |
62 | | - New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $appServicePrincipal.Id ` |
63 | | - -PrincipalId $appServicePrincipal.Id -ResourceId $graphServicePrincipal.Id ` |
64 | | - -AppRoleId $appRole.Id -ErrorAction SilentlyContinue -ErrorVariable SPError | Out-Null |
65 | | - if ($SPError) |
66 | | - { |
67 | | - Write-Host -ForegroundColor Red "Admin consent for one of the requested scopes could not be added." |
68 | | - Write-Host -ForegroundColor Red $SPError |
69 | | - Exit |
70 | | - } |
71 | | -} |
72 | | -Write-Host -ForegroundColor Cyan "Added admin consent" |
73 | | - |
74 | | -# Add a client secret |
75 | | -$clientSecret = Add-MgApplicationPassword -ApplicationId $appRegistration.Id -PasswordCredential ` |
76 | | - @{ DisplayName = "Added by PowerShell" } -ErrorAction Stop |
77 | | - |
78 | | -Write-Host |
79 | | -Write-Host -ForegroundColor Green "SUCCESS" |
80 | | -Write-Host -ForegroundColor Cyan -NoNewline "Tenant ID: " |
81 | | -Write-Host -ForegroundColor Yellow $context.TenantId |
82 | | -Write-Host -ForegroundColor Cyan -NoNewline "Client secret: " |
83 | | -Write-Host -ForegroundColor Yellow $clientSecret.SecretText |
84 | | -Write-Host -ForegroundColor Cyan -NoNewline "Secret expires: " |
85 | | -Write-Host -ForegroundColor Yellow $clientSecret.EndDateTime |
86 | | - |
87 | | -if ($StayConnected -eq $false) |
88 | | -{ |
89 | | - Disconnect-MgGraph |
90 | | - Write-Host "Disconnected from Microsoft Graph" |
91 | | -} |
92 | | -else |
93 | | -{ |
94 | | - Write-Host |
95 | | - Write-Host -ForegroundColor Yellow ` |
96 | | - "The connection to Microsoft Graph is still active. To disconnect, use Disconnect-MgGraph" |
97 | | -} |
98 | | -# </ScriptBody> |
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +# <ScriptBody> |
| 5 | +param( |
| 6 | + [Parameter(Mandatory=$true, |
| 7 | + HelpMessage="The friendly name of the app registration")] |
| 8 | + [String] |
| 9 | + $AppName, |
| 10 | + |
| 11 | + [Parameter(Mandatory=$true, |
| 12 | + HelpMessage="The application permission scopes to configure on the app registration")] |
| 13 | + [String[]] |
| 14 | + $GraphScopes, |
| 15 | + |
| 16 | + [Parameter(Mandatory=$false)] |
| 17 | + [Switch] |
| 18 | + $StayConnected = $false |
| 19 | +) |
| 20 | + |
| 21 | +$graphAppId = "00000003-0000-0000-c000-000000000000" |
| 22 | + |
| 23 | +# Requires an admin |
| 24 | +Connect-MgGraph -Scopes "Application.ReadWrite.All User.Read" -UseDeviceAuthentication -ErrorAction Stop |
| 25 | + |
| 26 | +# Get context for access to tenant ID |
| 27 | +$context = Get-MgContext -ErrorAction Stop |
| 28 | +$authTenant = $context.TenantId |
| 29 | + |
| 30 | +# Create app registration |
| 31 | +$appRegistration = New-MgApplication -DisplayName $AppName -SignInAudience "AzureADMyOrg" -ErrorAction Stop |
| 32 | +Write-Host -ForegroundColor Cyan "App registration created with app ID" $appRegistration.AppId |
| 33 | + |
| 34 | +# Create corresponding service principal |
| 35 | +$appServicePrincipal = New-MgServicePrincipal -AppId $appRegistration.AppId -ErrorAction SilentlyContinue ` |
| 36 | + -ErrorVariable SPError |
| 37 | +if ($SPError) |
| 38 | +{ |
| 39 | + Write-Host -ForegroundColor Red "A service principal for the app could not be created." |
| 40 | + Write-Host -ForegroundColor Red $SPError |
| 41 | + Exit |
| 42 | +} |
| 43 | + |
| 44 | +Write-Host -ForegroundColor Cyan "Service principal created" |
| 45 | + |
| 46 | +# Lookup available Graph application permissions |
| 47 | +$graphServicePrincipal = Get-MgServicePrincipal -Filter ("appId eq '" + $graphAppId + "'") -ErrorAction Stop |
| 48 | +$graphAppPermissions = $graphServicePrincipal.AppRoles |
| 49 | + |
| 50 | +$resourceAccess = @() |
| 51 | + |
| 52 | +foreach($scope in $GraphScopes) |
| 53 | +{ |
| 54 | + $permission = $graphAppPermissions | Where-Object { $_.Value -eq $scope } |
| 55 | + if ($permission) |
| 56 | + { |
| 57 | + $resourceAccess += @{ Id = $permission.Id; Type = "Role"} |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + Write-Host -ForegroundColor Red "Invalid scope:" $scope |
| 62 | + Exit |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +# Add the permissions to required resource access |
| 67 | +Update-MgApplication -ApplicationId $appRegistration.Id -RequiredResourceAccess ` |
| 68 | + @{ ResourceAppId = $graphAppId; ResourceAccess = $resourceAccess } -ErrorAction Stop |
| 69 | +Write-Host -ForegroundColor Cyan "Added application permissions to app registration" |
| 70 | + |
| 71 | +# Add admin consent |
| 72 | +foreach ($appRole in $resourceAccess) |
| 73 | +{ |
| 74 | + $appServicePrincipal |
| 75 | + New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $appServicePrincipal.Id ` |
| 76 | + -PrincipalId $appServicePrincipal.Id -ResourceId $graphServicePrincipal.Id ` |
| 77 | + -AppRoleId $appRole.Id -ErrorAction SilentlyContinue -ErrorVariable SPError | Out-Null |
| 78 | + if ($SPError) |
| 79 | + { |
| 80 | + Write-Host -ForegroundColor Red "Admin consent for one of the requested scopes could not be added." |
| 81 | + Write-Host -ForegroundColor Red $SPError |
| 82 | + Exit |
| 83 | + } |
| 84 | +} |
| 85 | +Write-Host -ForegroundColor Cyan "Added admin consent" |
| 86 | + |
| 87 | +# Add a client secret |
| 88 | +$clientSecret = Add-MgApplicationPassword -ApplicationId $appRegistration.Id -PasswordCredential ` |
| 89 | + @{ DisplayName = "Added by PowerShell" } -ErrorAction Stop |
| 90 | + |
| 91 | +Write-Host |
| 92 | +Write-Host -ForegroundColor Green "SUCCESS" |
| 93 | +Write-Host -ForegroundColor Cyan -NoNewline "Client ID: " |
| 94 | +Write-Host -ForegroundColor Yellow $appRegistration.AppId |
| 95 | +Write-Host -ForegroundColor Cyan -NoNewline "Tenant ID: " |
| 96 | +Write-Host -ForegroundColor Yellow $authTenant |
| 97 | +Write-Host -ForegroundColor Cyan -NoNewline "Client secret: " |
| 98 | +Write-Host -ForegroundColor Yellow $clientSecret.SecretText |
| 99 | +Write-Host -ForegroundColor Cyan -NoNewline "Secret expires: " |
| 100 | +Write-Host -ForegroundColor Yellow $clientSecret.EndDateTime |
| 101 | + |
| 102 | +if ($StayConnected -eq $false) |
| 103 | +{ |
| 104 | + Disconnect-MgGraph |
| 105 | + Write-Host "Disconnected from Microsoft Graph" |
| 106 | +} |
| 107 | +else |
| 108 | +{ |
| 109 | + Write-Host |
| 110 | + Write-Host -ForegroundColor Yellow ` |
| 111 | + "The connection to Microsoft Graph is still active. To disconnect, use Disconnect-MgGraph" |
| 112 | +} |
| 113 | +# </ScriptBody> |
0 commit comments