2017-03-04 03:59:51 +03:00
# Copyright (c) Microsoft Corporation. All rights reserved.
# See LICENSE.txt in the project root for license information.
2017-03-04 10:23:55 +03:00
<#
. Synopsis
Get the Guid of the directory tenant
. DESCRIPTION
This function fetches the OpenID configuration metadata from the identity system and parses the Directory TenantID out of it .
Azure Stack AD FS is configured to be a single tenanted identity system with a TenantID .
. EXAMPLE
2017-06-15 03:17:38 +03:00
Get-AzsDirectoryTenantIdentifier -authority https : / / login . windows . net / microsoft . onmicrosoft . com
2017-03-04 10:23:55 +03:00
. EXAMPLE
2017-06-15 03:17:38 +03:00
Get-AzsDirectoryTenantIdentifier -authority https : / / adfs . local . azurestack . external / adfs
2017-03-04 10:23:55 +03:00
#>
2017-06-15 03:17:38 +03:00
function Get-AzsDirectoryTenantidentifier {
2017-03-04 10:23:55 +03:00
[ CmdletBinding ( ) ]
Param
(
# Param1 help description
2017-04-04 02:23:31 +03:00
[ Parameter ( Mandatory = $true ,
Position = 0 ) ]
2017-03-04 10:23:55 +03:00
$Authority
)
2017-04-04 02:23:31 +03:00
2017-03-04 10:23:55 +03:00
return $ ( Invoke-RestMethod $ ( " {0}/.well-known/openid-configuration " -f $authority . TrimEnd ( '/' ) ) ) . issuer . TrimEnd ( '/' ) . Split ( '/' ) [ -1 ]
}
2017-06-22 03:39:12 +03:00
Export-ModuleMember -Function 'Get-AzsDirectoryTenantidentifier'
2017-03-04 03:59:51 +03:00
<#
. Synopsis
This function is used to create a Service Principal on teh AD Graph
. DESCRIPTION
The command creates a certificate in the cert store of the local user and uses that certificate to create a Service Principal in the Azure Stack Stamp Active Directory .
. EXAMPLE
2017-06-15 03:17:38 +03:00
$servicePrincipal = New-AzsAdGraphServicePrincipal -DisplayName " mySPApp " -AdminCredential $ ( Get-Credential ) -Verbose
2017-03-04 03:59:51 +03:00
. EXAMPLE
2017-06-15 03:17:38 +03:00
$servicePrincipal = New-AzsAdGraphServicePrincipal -DisplayName " mySPApp " -AdminCredential $ ( Get-Credential ) -DeleteAndCreateNew -Verbose
2017-03-04 03:59:51 +03:00
#>
2017-06-15 03:17:38 +03:00
function New-AzsAdGraphServicePrincipal {
2017-04-04 02:23:31 +03:00
[ CmdletBinding ( ) ]
Param
(
# Display Name of the Service Principal
[ ValidatePattern ( " [a-zA-Z0-9-]{3,} " ) ]
[ Parameter ( Mandatory = $true ,
Position = 0 ) ]
$DisplayName ,
2017-03-04 03:59:51 +03:00
2017-04-04 02:23:31 +03:00
# Adfs Machine name
2017-06-22 03:39:12 +03:00
[ Parameter ( Mandatory = $true , Position = 1 ) ]
2017-04-04 02:23:31 +03:00
[ string ]
2017-06-22 03:39:12 +03:00
$AdfsMachineName ,
2017-04-04 02:23:31 +03:00
# Domain Administrator Credential to create Service Principal
[ Parameter ( Mandatory = $true ,
Position = 2 ) ]
[ System.Management.Automation.PSCredential ]
$AdminCredential ,
# Switch to delete existing Service Principal with Provided Display Name and recreate
[ Parameter ( Mandatory = $false ) ]
[ switch ]
$DeleteAndCreateNew
)
Write-Verbose " Creating a Certificate for the Service Principal.. "
$clientCertificate = New-SelfSignedCertificate -CertStoreLocation " cert:\CurrentUser\My " -Subject " CN= $DisplayName " -KeySpec KeyExchange
$scriptBlock = {
param ( [ string ] $DisplayName , [ System.Security.Cryptography.X509Certificates.X509Certificate2 ] $ClientCertificate , [ bool ] $DeleteAndCreateNew )
$VerbosePreference = " Continue "
$ErrorActionPreference = " stop "
Import-Module 'ActiveDirectory' -Verbose: $false 4 > $null
# Application Group Name
$applicationGroupName = $DisplayName + " -AppGroup "
$applicationGroupDescription = " Application group for $DisplayName "
$shellSiteDisplayName = $DisplayName
$shellSiteRedirectUri = " https://localhost/ " . ToLowerInvariant ( )
$shellSiteApplicationId = [ guid ] :: NewGuid ( ) . ToString ( )
$shellSiteClientDescription = " Client for $DisplayName "
$defaultTimeOut = New-TimeSpan -Minutes 5
if ( $DeleteAndCreateNew ) {
$applicationGroup = Get-GraphApplicationGroup -ApplicationGroupName $applicationGroupName -Timeout $defaultTimeOut
Write-Verbose $applicationGroup
if ( $applicationGroup ) {
Write-Warning -Message " Deleting existing application group with name ' $applicationGroupName '. "
Remove-GraphApplicationGroup -TargetApplicationGroup $applicationGroup -Timeout $defaultTimeOut
2017-03-04 03:59:51 +03:00
}
2017-04-04 02:23:31 +03:00
}
Write-Verbose -Message " Creating new application group with name ' $applicationGroupName '. "
$applicationParameters = @ {
2017-06-22 03:39:12 +03:00
Name = $applicationGroupName
Description = $applicationGroupDescription
ClientType = 'Confidential'
ClientId = $shellSiteApplicationId
ClientDisplayName = $shellSiteDisplayName
2017-04-04 02:23:31 +03:00
ClientRedirectUris = $shellSiteRedirectUri
2017-06-22 03:39:12 +03:00
ClientDescription = $shellSiteClientDescription
2017-04-04 02:23:31 +03:00
ClientCertificates = $ClientCertificate
}
$defaultTimeOut = New-TimeSpan -Minutes 10
$applicationGroup = New-GraphApplicationGroup @applicationParameters -PassThru -Timeout $defaultTimeOut
Write-Verbose -Message " Shell Site ApplicationGroup: $( $applicationGroup | ConvertTo-Json ) "
return [ pscustomobject ] @ {
2017-06-22 03:39:12 +03:00
ObjectId = $applicationGroup . Identifier
2017-04-04 02:23:31 +03:00
ApplicationId = $applicationParameters . ClientId
2017-06-22 03:39:12 +03:00
Thumbprint = $ClientCertificate . Thumbprint
2017-04-04 02:23:31 +03:00
}
2017-03-04 03:59:51 +03:00
}
$domainAdminSession = New-PSSession -ComputerName $AdfsMachineName -Credential $AdminCredential -Authentication Credssp -Verbose
$output = Invoke-Command -Session $domainAdminSession -ScriptBlock $scriptBlock -ArgumentList @ ( $DisplayName , $ClientCertificate , $DeleteAndCreateNew . IsPresent ) -Verbose -ErrorAction Stop
Write-Verbose " AppDetails: $( ConvertTo-Json $output -Depth 2 ) "
return $output
2017-04-04 02:23:31 +03:00
}
# Helper Functions
function Initialize-AzureRmEnvironment([string]$EnvironmentName , [ string ] $ResourceManagerEndpoint , [ string ] $DirectoryTenantName ) {
$endpoints = Invoke-RestMethod -Method Get -Uri " $( $ResourceManagerEndpoint . ToString ( ) . TrimEnd ( '/' ) ) /metadata/endpoints?api-version=2015-01-01 " -Verbose
Write-Verbose -Message " Endpoints: $( ConvertTo-Json $endpoints ) " -Verbose
# resolve the directory tenant ID from the name
$directoryTenantId = ( New-Object uri ( Invoke-RestMethod " $( $endpoints . authentication . loginEndpoint . TrimEnd ( '/' ) ) / $DirectoryTenantName /.well-known/openid-configuration " ) . token_endpoint ) . AbsolutePath . Split ( '/' ) [ 1 ]
$azureEnvironmentParams = @ {
2017-05-06 03:00:17 +03:00
Name = $EnvironmentName
ActiveDirectoryEndpoint = $endpoints . authentication . loginEndpoint . TrimEnd ( '/' ) + " / "
2017-04-04 02:23:31 +03:00
ActiveDirectoryServiceEndpointResourceId = $endpoints . authentication . audiences [ 0 ]
2017-05-06 03:00:17 +03:00
AdTenant = $directoryTenantId
ResourceManagerEndpoint = $ResourceManagerEndpoint
GalleryEndpoint = $endpoints . galleryEndpoint
GraphEndpoint = $endpoints . graphEndpoint
GraphAudience = $endpoints . graphEndpoint
2017-04-04 02:23:31 +03:00
}
Remove-AzureRmEnvironment -Name $EnvironmentName -Force -ErrorAction Ignore | Out-Null
$azureEnvironment = Add-AzureRmEnvironment @azureEnvironmentParams
$azureEnvironment = Get-AzureRmEnvironment -Name $EnvironmentName
return $azureEnvironment
}
function Resolve-AzureEnvironment([Microsoft.Azure.Commands.Profile.Models.PSAzureEnvironment]$azureStackEnvironment ) {
$azureEnvironment = Get-AzureRmEnvironment |
Where GraphEndpointResourceId -EQ $azureStackEnvironment . GraphEndpointResourceId |
Where Name -In @ ( 'AzureCloud' , 'AzureChinaCloud' , 'AzureUSGovernment' , 'AzureGermanCloud' )
# Differentiate between AzureCloud and AzureUSGovernment
if ( $azureEnvironment . Count -ge 2 ) {
$name = if ( $azureStackEnvironment . ActiveDirectoryAuthority -eq 'https://login-us.microsoftonline.com/' ) { 'AzureUSGovernment' } else { 'AzureCloud' }
$azureEnvironment = $azureEnvironment | Where Name -EQ $name
}
return $azureEnvironment
}
2017-05-06 03:00:17 +03:00
function Initialize-AzureRmUserAccount([Microsoft.Azure.Commands.Profile.Models.PSAzureEnvironment]$azureEnvironment , [ string ] $SubscriptionName , [ string ] $SubscriptionId , [ pscredential ] $AutomationCredential ) {
2017-04-04 02:23:31 +03:00
2017-05-06 03:00:17 +03:00
$params = @ {
EnvironmentName = $azureEnvironment . Name
TenantId = $azureEnvironment . AdTenant
}
if ( $AutomationCredential )
{
$params + = @ { Credential = $AutomationCredential }
}
# Prompts the user for interactive login flow if automation credential is not specified
$azureAccount = Add-AzureRmAccount @params
if ( $SubscriptionName )
{
2017-04-04 02:23:31 +03:00
Select-AzureRmSubscription -SubscriptionName $SubscriptionName | Out-Null
}
2017-05-06 03:00:17 +03:00
elseif ( $SubscriptionId )
{
2017-04-04 02:23:31 +03:00
Select-AzureRmSubscription -SubscriptionId $SubscriptionId | Out-Null
}
return $azureAccount
}
2017-05-06 03:00:17 +03:00
function Resolve-GraphEnvironment([Microsoft.Azure.Commands.Profile.Models.PSAzureEnvironment]$azureEnvironment )
{
2017-04-04 02:23:31 +03:00
$graphEnvironment = switch ( $azureEnvironment . ActiveDirectoryAuthority ) {
'https://login.microsoftonline.com/' { 'AzureCloud' }
'https://login.chinacloudapi.cn/' { 'AzureChinaCloud' }
'https://login-us.microsoftonline.com/' { 'AzureUSGovernment' }
'https://login.microsoftonline.de/' { 'AzureGermanCloud' }
Default { throw " Unsupported graph resource identifier: $_ " }
}
return $graphEnvironment
}
2017-05-06 03:00:17 +03:00
function Get-AzureRmUserRefreshToken([Microsoft.Azure.Commands.Profile.Models.PSAzureEnvironment]$azureEnvironment , [ string ] $directoryTenantId , [ pscredential ] $AutomationCredential )
{
$params = @ {
EnvironmentName = $azureEnvironment . Name
TenantId = $directoryTenantId
}
if ( $AutomationCredential )
{
$params + = @ { Credential = $AutomationCredential }
}
# Prompts the user for interactive login flow if automation credential is not specified
$azureAccount = Add-AzureRmAccount @params
2017-04-04 02:23:31 +03:00
# Retrieve the refresh token
$tokens = [ Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache ] :: DefaultShared . ReadItems ( )
$refreshToken = $tokens |
Where Resource -EQ $azureEnvironment . ActiveDirectoryServiceEndpointResourceId |
Where IsMultipleResourceRefreshToken -EQ $true |
Where DisplayableId -EQ $azureAccount . Context . Account . Id |
2017-05-06 03:00:17 +03:00
Sort ExpiresOn |
Select -Last 1 -ExpandProperty RefreshToken |
2017-04-04 02:23:31 +03:00
ConvertTo-SecureString -AsPlainText -Force
return $refreshToken
}
2017-05-06 03:00:17 +03:00
# Exposed Functions
2017-04-04 02:23:31 +03:00
<#
. Synopsis
Adds a Guest Directory Tenant to Azure Stack .
. DESCRIPTION
Running this cmdlet will add the specified directory tenant to the Azure Stack whitelist .
. EXAMPLE
$adminARMEndpoint = " https://adminmanagement.local.azurestack.external "
$azureStackDirectoryTenant = " <homeDirectoryTenant>.onmicrosoft.com "
$guestDirectoryTenantToBeOnboarded = " <guestDirectoryTenant>.onmicrosoft.com "
2017-06-28 00:06:54 +03:00
Register-AzsGuestDirectoryTenant -AdminResourceManagerEndpoint $adminARMEndpoint -DirectoryTenantName $azureStackDirectoryTenant -GuestDirectoryTenantName $guestDirectoryTenantToBeOnboarded
2017-04-04 02:23:31 +03:00
#>
2017-06-15 03:17:38 +03:00
2017-06-28 00:06:54 +03:00
function Register-AzsGuestDirectoryTenant {
2017-04-04 02:23:31 +03:00
[ CmdletBinding ( ) ]
param
(
# The endpoint of the Azure Stack Resource Manager service.
[ Parameter ( Mandatory = $true ) ]
[ ValidateNotNull ( ) ]
[ ValidateScript ( { $_ . Scheme -eq [ System.Uri ] :: UriSchemeHttps } ) ]
[ uri ] $AdminResourceManagerEndpoint ,
# The name of the home Directory Tenant in which the Azure Stack Administrator subscription resides.
[ Parameter ( Mandatory = $true ) ]
[ ValidateNotNullOrEmpty ( ) ]
[ string ] $DirectoryTenantName ,
2017-05-06 03:00:17 +03:00
# The names of the guest Directory Tenants which are to be onboarded.
2017-04-04 02:23:31 +03:00
[ Parameter ( Mandatory = $true ) ]
[ ValidateNotNullOrEmpty ( ) ]
2017-05-06 03:00:17 +03:00
[ string[] ] $GuestDirectoryTenantName ,
2017-04-04 02:23:31 +03:00
2017-05-06 03:00:17 +03:00
# The location of your Azure Stack deployment.
[ Parameter ( Mandatory = $true ) ]
2017-04-04 02:23:31 +03:00
[ ValidateNotNullOrEmpty ( ) ]
2017-05-06 03:00:17 +03:00
[ string ] $Location ,
2017-04-04 02:23:31 +03:00
# The identifier of the Administrator Subscription. If not specified, the script will attempt to use the set default subscription.
[ ValidateNotNull ( ) ]
[ string ] $SubscriptionId = $null ,
# The display name of the Administrator Subscription. If not specified, the script will attempt to use the set default subscription.
[ ValidateNotNull ( ) ]
[ string ] $SubscriptionName = $null ,
[ Parameter ( ) ]
[ ValidateNotNullOrEmpty ( ) ]
[ string ] $ResourceGroupName = 'system' ,
2017-05-06 03:00:17 +03:00
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
2017-04-04 02:23:31 +03:00
[ Parameter ( ) ]
2017-05-06 03:00:17 +03:00
[ ValidateNotNull ( ) ]
[ pscredential ] $AutomationCredential = $null
2017-04-04 02:23:31 +03:00
)
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'
# Install-Module AzureRm -RequiredVersion '1.2.8'
Import-Module 'AzureRm.Profile' -Force -Verbose: $false 4 > $null
2017-05-06 03:00:17 +03:00
2017-04-04 02:23:31 +03:00
# Initialize the Azure PowerShell module to communicate with Azure Stack. Will prompt user for credentials.
2017-05-06 03:00:17 +03:00
$azureEnvironment = Initialize-AzureRmEnvironment -EnvironmentName 'AzureStackAdmin' -ResourceManagerEndpoint $AdminResourceManagerEndpoint -DirectoryTenantName $DirectoryTenantName
$azureAccount = Initialize-AzureRmUserAccount -azureEnvironment $azureEnvironment -SubscriptionName $SubscriptionName -SubscriptionId $SubscriptionId -AutomationCredential $AutomationCredential
2017-04-04 02:23:31 +03:00
2017-05-06 03:00:17 +03:00
foreach ( $directoryTenantName in $GuestDirectoryTenantName )
{
# Resolve the guest directory tenant ID from the name
$directoryTenantId = ( New-Object uri ( Invoke-RestMethod " $( $azureEnvironment . ActiveDirectoryAuthority . TrimEnd ( '/' ) ) / $directoryTenantName /.well-known/openid-configuration " ) . token_endpoint ) . AbsolutePath . Split ( '/' ) [ 1 ]
2017-04-04 02:23:31 +03:00
2017-05-06 03:00:17 +03:00
# Add (or update) the new directory tenant to the Azure Stack deployment
2017-04-04 02:23:31 +03:00
$params = @ {
2017-05-06 03:00:17 +03:00
ApiVersion = '2015-11-01'
ResourceType = " Microsoft.Subscriptions.Admin/directoryTenants "
2017-04-04 02:23:31 +03:00
ResourceGroupName = $ResourceGroupName
2017-05-06 03:00:17 +03:00
ResourceName = $directoryTenantName
Location = $Location
Properties = @ { tenantId = $directoryTenantId }
2017-04-04 02:23:31 +03:00
}
2017-05-06 03:00:17 +03:00
$directoryTenant = New-AzureRmResource @params -Force -Verbose -ErrorAction Stop
Write-Verbose -Message " Directory Tenant onboarded: $( ConvertTo-Json $directoryTenant ) " -Verbose
2017-04-04 02:23:31 +03:00
}
}
2017-06-22 03:39:12 +03:00
Export-ModuleMember -Function 'Publish-AzsApplicationsToARM'
2017-04-04 02:23:31 +03:00
<#
. Synopsis
Consents to the given Azure Stack instance within the callers ' s Azure Directory Tenant .
. DESCRIPTION
Consents to the given Azure Stack instance within the callers 's Azure Directory Tenant. This is needed to propagate Azure Stack applications into the user' s directory tenant .
. EXAMPLE
$tenantARMEndpoint = " https://management.local.azurestack.external "
$myDirectoryTenantName = " <guestDirectoryTenant>.onmicrosoft.com "
2017-06-15 03:17:38 +03:00
Register-AzsWithMyDirectoryTenant -TenantResourceManagerEndpoint $tenantARMEndpoint `
2017-04-04 02:23:31 +03:00
-DirectoryTenantName $myDirectoryTenantName -Verbose -Debug
#>
2017-06-15 03:17:38 +03:00
function Register-AzsWithMyDirectoryTenant {
2017-04-04 02:23:31 +03:00
[ CmdletBinding ( ) ]
param
(
# The endpoint of the Azure Stack Resource Manager service.
[ Parameter ( Mandatory = $true ) ]
[ ValidateNotNull ( ) ]
[ ValidateScript ( { $_ . Scheme -eq [ System.Uri ] :: UriSchemeHttps } ) ]
[ uri ] $TenantResourceManagerEndpoint ,
# The name of the directory tenant being onboarded.
[ Parameter ( Mandatory = $true ) ]
[ ValidateNotNullOrEmpty ( ) ]
2017-05-06 03:00:17 +03:00
[ string ] $DirectoryTenantName ,
# Optional: The identifier (GUID) of the Resource Manager application. Pass this parameter to skip the need to complete the guest signup flow via the portal.
[ Parameter ( Mandatory = $false ) ]
[ ValidateNotNullOrEmpty ( ) ]
[ string ] $ResourceManagerApplicationId ,
# Optional: A credential used to authenticate with Azure Stack. Must support a non-interactive authentication flow. If not provided, the script will prompt for user credentials.
[ Parameter ( ) ]
[ ValidateNotNull ( ) ]
[ pscredential ] $AutomationCredential = $null
2017-04-04 02:23:31 +03:00
)
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'
# Install-Module AzureRm -RequiredVersion '1.2.8'
Import-Module 'AzureRm.Profile' -Force -Verbose: $false 4 > $null
2017-05-06 03:00:17 +03:00
Import-Module " $PSScriptRoot \GraphAPI\GraphAPI.psm1 " -Force -Verbose: $false 4 > $null
2017-04-04 02:23:31 +03:00
# Initialize the Azure PowerShell module to communicate with the Azure Resource Manager corresponding to their home Graph Service. Will prompt user for credentials.
$azureStackEnvironment = Initialize-AzureRmEnvironment -EnvironmentName 'AzureStack' -ResourceManagerEndpoint $TenantResourceManagerEndpoint -DirectoryTenantName $DirectoryTenantName
$azureEnvironment = Resolve-AzureEnvironment $azureStackEnvironment
2017-05-06 03:00:17 +03:00
$refreshToken = Get-AzureRmUserRefreshToken -azureEnvironment $azureEnvironment -directoryTenantId $azureStackEnvironment . AdTenant -AutomationCredential $AutomationCredential
2017-04-04 02:23:31 +03:00
# Initialize the Graph PowerShell module to communicate with the correct graph service
2017-06-15 03:17:38 +03:00
$graphEnvironment = ResolveGraphEnvironment $azureEnvironment
2017-04-04 02:23:31 +03:00
Initialize-GraphEnvironment -Environment $graphEnvironment -DirectoryTenantId $DirectoryTenantName -RefreshToken $refreshToken
2017-05-06 03:00:17 +03:00
# Initialize the service principal for the Azure Stack Resource Manager application (allows us to acquire a token to ARM). If not specified, the sign-up flow must be completed via the Azure Stack portal first.
if ( $ResourceManagerApplicationId )
{
$resourceManagerServicePrincipal = Initialize-GraphApplicationServicePrincipal -ApplicationId $ResourceManagerApplicationId
}
# Authorize the Azure Powershell module to act as a client to call the Azure Stack Resource Manager in the onboarding directory tenant
Initialize-GraphOAuth2PermissionGrant -ClientApplicationId ( Get-GraphEnvironmentInfo ) . Applications . PowerShell . Id -ResourceApplicationIdentifierUri $azureStackEnvironment . ActiveDirectoryServiceEndpointResourceId
Write-Host " Delaying for 15 seconds to allow the permission for Azure PowerShell to be initialized... "
Start-Sleep -Seconds 15
2017-04-04 02:23:31 +03:00
# Authorize the Azure Powershell module to act as a client to call the Azure Stack Resource Manager in the onboarded tenant
Initialize-GraphOAuth2PermissionGrant -ClientApplicationId ( Get-GraphEnvironmentInfo ) . Applications . PowerShell . Id -ResourceApplicationIdentifierUri $azureStackEnvironment . ActiveDirectoryServiceEndpointResourceId
# Call Azure Stack Resource Manager to retrieve the list of registered applications which need to be initialized in the onboarding directory tenant
$armAccessToken = ( Get-GraphToken -Resource $azureStackEnvironment . ActiveDirectoryServiceEndpointResourceId -UseEnvironmentData ) . access_token
$applicationRegistrationParams = @ {
2017-06-22 03:39:12 +03:00
Method = [ Microsoft.PowerShell.Commands.WebRequestMethod ] :: Get
2017-04-04 02:23:31 +03:00
Headers = @ { Authorization = " Bearer $armAccessToken " }
2017-06-22 03:39:12 +03:00
Uri = " $( $TenantResourceManagerEndpoint . ToString ( ) . TrimEnd ( '/' ) ) /applicationRegistrations?api-version=2014-04-01-preview "
2017-04-04 02:23:31 +03:00
}
2017-06-22 03:39:12 +03:00
$applicationRegistrations = Invoke-RestMethod @applicationRegistrationParams | Select-Object -ExpandProperty value
2017-04-04 02:23:31 +03:00
2017-05-06 03:00:17 +03:00
# Identify which permissions have already been granted to each registered application and which additional permissions need consent
$permissions = @ ( )
foreach ( $applicationRegistration in $applicationRegistrations )
{
# Initialize the service principal for the registered application
2017-04-04 02:23:31 +03:00
$applicationServicePrincipal = Initialize-GraphApplicationServicePrincipal -ApplicationId $applicationRegistration . appId
2017-05-06 03:00:17 +03:00
# Initialize the necessary tags for the registered application
if ( $applicationRegistration . tags )
{
2017-04-04 02:23:31 +03:00
Update-GraphApplicationServicePrincipalTags -ApplicationId $applicationRegistration . appId -Tags $applicationRegistration . tags
}
2017-05-06 03:00:17 +03:00
# Lookup the permission consent status for the application permissions (either to or from) that the registered application requires
foreach ( $appRoleAssignment in $applicationRegistration . appRoleAssignments )
{
$params = @ {
ClientApplicationId = $appRoleAssignment . client
ResourceApplicationId = $appRoleAssignment . resource
PermissionType = 'Application'
PermissionId = $appRoleAssignment . roleId
2017-04-04 02:23:31 +03:00
}
2017-05-06 03:00:17 +03:00
$permissions + = New-GraphPermissionDescription @params -LookupConsentStatus
2017-04-04 02:23:31 +03:00
}
2017-05-06 03:00:17 +03:00
# Lookup the permission consent status for the delegated permissions (either to or from) that the registered application requires
foreach ( $oauth2PermissionGrant in $applicationRegistration . oauth2PermissionGrants )
{
$resourceApplicationServicePrincipal = Initialize-GraphApplicationServicePrincipal -ApplicationId $oauth2PermissionGrant . resource
foreach ( $scope in $oauth2PermissionGrant . scope . Split ( ' ' ) )
{
$params = @ {
ClientApplicationId = $oauth2PermissionGrant . client
ResourceApplicationServicePrincipal = $resourceApplicationServicePrincipal
PermissionType = 'Delegated'
PermissionId = ( $resourceApplicationServicePrincipal . oauth2Permissions | Where value -EQ $scope ) . id
}
$permissions + = New-GraphPermissionDescription @params -LookupConsentStatus
}
2017-04-04 02:23:31 +03:00
}
}
2017-05-06 03:00:17 +03:00
# Show the user a display of the required permissions
$permissions | Show-GraphApplicationPermissionDescriptions
if ( $permissions | Where isConsented -EQ $false | Select -First 1 )
{
# Grant the required permissions to the corresponding applications
$permissions | Where isConsented -EQ $false | Grant-GraphApplicationPermission
}
Write-Host " `r `n All permissions required for registered Azure Stack applications or scenarios have been granted! " -ForegroundColor Green
2017-04-04 02:23:31 +03:00
}
2017-05-06 03:00:17 +03:00
Export-ModuleMember -Function @ (
" Register-AzureStackWithMyDirectoryTenant " ,
" Register-GuestDirectoryTenantToAzureStack " ,
" Get-DirectoryTenantIdentifier " ,
2017-07-05 01:23:06 +03:00
" New-AzsADGraphServicePrincipal "
2017-06-28 00:06:54 +03:00
)