Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathRegisterAppForUserAuth.ps1
More file actions
Latest commit
89 lines (76 loc) · 2.59 KB
/
Copy pathRegisterAppForUserAuth.ps1
File metadata and controls
89 lines (76 loc) · 2.59 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
# <ScriptBody>
param(
[Parameter(Mandatory=$true,
HelpMessage="The friendly name of the app registration")]
[String]
$AppName,
[Parameter(Mandatory=$false,
HelpMessage="The sign in audience for the app")]
[ValidateSet("AzureADMyOrg","AzureADMultipleOrgs",`
"AzureADandPersonalMicrosoftAccount","PersonalMicrosoftAccount")]
[String]
$SignInAudience="AzureADandPersonalMicrosoftAccount",
[Parameter(Mandatory=$false)]
[Switch]
$StayConnected=$false
)
# Tenant to use in authentication.
# See https://learn.microsoft.com/azure/active-directory/develop/v2-oauth2-device-code#device-authorization-request
$authTenant=switch ($SignInAudience)
{
"AzureADMyOrg" { "tenantId" }
"AzureADMultipleOrgs" { "organizations" }
"AzureADandPersonalMicrosoftAccount" { "common" }
"PersonalMicrosoftAccount" { "consumers" }
default { "invalid" }
}
if ($authTenant-eq"invalid")
{
Write-Host-ForegroundColor Red "Invalid sign in audience:"$SignInAudience
Exit
}
# Requires an admin
Connect-MgGraph-Scopes "Application.ReadWrite.All User.Read"-UseDeviceAuthentication -ErrorAction Stop
# Get context for access to tenant ID
$context=Get-MgContext-ErrorAction Stop
if ($authTenant-eq"tenantId")
{
$authTenant=$context.TenantId
}
# Create app registration
$appRegistration=New-MgApplication-DisplayName $AppName-SignInAudience $SignInAudience`
-IsFallbackPublicClient -ErrorAction Stop
Write-Host-ForegroundColor Cyan "App registration created with app ID"$appRegistration.AppId
# Create corresponding service principal
if ($SignInAudience-ne"PersonalMicrosoftAccount")
{
New-MgServicePrincipal-AppId $appRegistration.AppId-ErrorAction SilentlyContinue `
-ErrorVariable SPError |Out-Null
if ($SPError)
{
Write-Host-ForegroundColor Red "A service principal for the app could not be created."
Write-Host-ForegroundColor Red $SPError
Exit
}
Write-Host-ForegroundColor Cyan "Service principal created"
}
Write-Host
Write-Host-ForegroundColor Green "SUCCESS"
Write-Host-ForegroundColor Cyan -NoNewline "Client ID: "
Write-Host-ForegroundColor Yellow $appRegistration.AppId
Write-Host-ForegroundColor Cyan -NoNewline "Auth tenant: "
Write-Host-ForegroundColor Yellow $authTenant
if ($StayConnected-eq$false)
{
Disconnect-MgGraph|Out-Null
Write-Host"Disconnected from Microsoft Graph"
}
else
{
Write-Host
Write-Host-ForegroundColor Yellow `
"The connection to Microsoft Graph is still active. To disconnect, use Disconnect-MgGraph"
}
# </ScriptBody>