Created
April 9, 2018 11:51
-
-
Save ehrnst/074319aa34f67d05f28072c6a8b984d0 to your computer and use it in GitHub Desktop.
Create Azure Active Directory application with powershell and set reader permission on subscription
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Creates an azure ad application and sets reader permissions on subscription | |
.NOTES | |
Script is provided as an example, it has no error handeling and is not production ready. App name and permissions is hard coded. | |
#> | |
param( | |
[Parameter(Mandatory)] | |
[String] | |
$TenantId, | |
[Parameter(Mandatory)] | |
[String] | |
$SubscriptionId, | |
[Parameter(Mandatory)] | |
[System.Management.Automation.PSCredential]$cred | |
) | |
$ErrorActionPreference = "Continue" # Added cuse of the while loop | |
# connecting to Azure Ad | |
Import-Module AzureRM | |
Import-Module AzureAD | |
$AADConnection = Connect-AzureAD -Credential $cred -TenantId $TenantId | |
#region App Permissions | |
# Building resource access objects | |
# Used get-azureadServicePrincipal -All $true to find the correct api's | |
## Azure Management API | |
$AzureMgmtPrincipal = Get-AzureADServicePrincipal -All $true | Where-Object {$_.DisplayName -eq "Windows Azure Service Management API"} | |
$AzureMgmtAccess = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess" | |
$AzureMgmtAccess.ResourceAppId = $AzureMgmtPrincipal.AppId | |
# Permissions | |
# Explored permissions with 365mgmtPrincipal.AppRoles and .Oauth2Permissions | |
## Delegated Permissions | |
# Azure Mgmt | |
$AzureSvcMgmt = New-Object -TypeName "microsoft.open.azuread.model.resourceAccess" -ArgumentList "41094075-9dad-400e-a0bd-54e686782033", "Scope" | |
# Add permission objects to the resource access object | |
$AzureMgmtAccess.ResourceAccess = $AzureSvcMgmt | |
#endregion | |
# Create the new apps | |
$AdatumDemoApp = New-AzureADApplication -DisplayName "Adatum Demo App" -IdentifierUris "https://localhost/AdatumDemoApp" -HomePage "https://localhost/Adatum" -ReplyUrls "https://localhost/Adatum" -RequiredResourceAccess @($AzureMgmtAccess) | |
# Create and set access key | |
$AdatumDemoAppKeySecret = New-AzureADApplicationPasswordCredential -ObjectId $AdatumDemoApp.ObjectId -CustomKeyIdentifier "Access Key" -EndDate (get-date).AddYears(1) | |
# Add service principal to our app. Note the tag: https://docs.microsoft.com/en-us/powershell/module/azuread/new-azureadserviceprincipal?view=azureadps-2.0 | |
$AdatumDemoAppAppSPN = New-AzureADServicePrincipal -AppId $AdatumDemoApp.AppId -Tags @("WindowsAzureActiveDirectoryIntegratedApp") | |
# Splunk needs to read the Azure activity logs. Adding Subscription Read access to the application | |
# https://docs.microsoft.com/en-us/azure/active-directory/role-based-access-control-manage-access-powershell | |
$AzureRMConnection = Add-AzureRmAccount -TenantId ($AADConnection.TenantId.Guid).ToString() -Credential $cred | |
# Create a loop to allow azure to create the SPN before setting role | |
while ($addRole.DisplayName -ne $AdatumDemoApp.DisplayName ) { | |
Write-Verbose "Waiting for SPN to create" | |
Start-Sleep -Seconds 5 | |
$addRole = New-AzureRmRoleAssignment -ObjectId $AdatumDemoAppAppSPN.ObjectId -RoleDefinitionName "Reader" -Scope "/subscriptions/$SubScriptionId" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment