Dynamics 365 CE S2S : Automate App Registration in Azure AD using PowerShell

Application user creation in Dynamics 365 CE requires an app registration in Azure AD, which is a lengthy process. You can automate it using PowerShell.

Launch PowerShell as Administrator in your local machine and run this script.

# Install AzureAD PS module in local m/c
Install-Module AzureAD

#Connect to Azure AD ā€“ Launches popup to enter username/password
Connect-AzureAD

#Get CDS object (shown as Dynamics CRM in Azure Portal)
$AzureMgmtPrincipal = Get-AzureADServicePrincipal -All $true | Where-Object {$_.DisplayName -eq "Common Data Service"}

#Create API permission to CDS
$AzureMgmtAccess = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
$AzureMgmtAccess.ResourceAppId = $AzureMgmtPrincipal.AppId
$AzureSvcMgmt = New-Object -TypeName "microsoft.open.azuread.model.resourceAccess" -ArgumentList $AzureMgmtPrincipal.Oauth2Permissions.Id, "Scope"
$AzureMgmtAccess.ResourceAccess = $AzureSvcMgmt

#Create App Registration – Update DisplayName value as needed
$App = New-AzureADApplication -DisplayName "Integration App2" -RequiredResourceAccess @($AzureMgmtAccess)

#Create App Secret
$AppSecret = New-AzureADApplicationPasswordCredential -ObjectId $App.ObjectId -CustomKeyIdentifier "Access Key" -EndDate (get-date).AddYears(1)

#Create managed application in local directory
$AppSPN = New-AzureADServicePrincipal -AppId $App.AppId -Tags @("WindowsAzureActiveDirectoryIntegratedApp")

Once the script runs successfully, you can fetch the Client ID and Secret from the script variables $App.AppId and $AppSecret respectively.

 

 

Hope this saves your time!!

Cheers

 

Leave a comment