Microsoft recently released the capability to use Managed Identities to authenticate to multi-tenant applications, enabling you to manage resources in external tenants, something I recently wrote about in my previous post Microsoft now allows connecting to Multi-tenant apps using Managed Identities.
The problem with Microsoft’s announcement is that it is aimed only at developers, by providing examples in only C# code. In this post, I aim to solve that problem by showing you how you can use PowerShell in Azure Automation to leverage the new Managed Identity Federation feature of App Registrations to connect to and manage external Microsoft tenants.
Follow the steps in this article to create and federate your multi-tenant app and connect to an external tenant using Azure Automation with Microsoft Graph PowerShell.
Create a new user-assigned Managed Identity
The first step is to create a new user-assigned managed identity which you will use to federate with your multi-tenant app registration in Microsoft Entra. Follow the steps below to create a new user-assigned managed identity.
- Log in to the Microsoft Azure portal.
- Search for ‘managed identities’ and open the managed identities blade.
- Click Create.
- Select your subscription, resource group and region, then give your managed identity a name.
- Click Review & create to complete the creation of your managed identity.
Your new managed identity will then be visible from the managed identities list in the Azure portal as well as the Enterprise applications list in Microsoft Entra.
Assign the Managed Identity to your Azure resource
For the user-assigned managed identity to be properly utilised by your resource in Microsoft Azure, it must be assigned (or ‘linked’) to that resource. In this case, you will create a new Azure Automation Account and link the managed identity to it.
Follow the below steps to create a new Automation Account using the Azure Portal and assign your managed identity to it.
- Log in to the Microsoft Azure portal.
- Search for Automation Accounts and click Create.
- Select your Subscription and Resource group, give your Automation Account a name and select the region.
- Click Next.
- On the Advanced tab, unselect System assigned and check the box next to User assigned managed identity.
- Click Add user assigned identities, then find and select your managed identity and click Add.
- Lastly, click Review + Create, then click Create.
Create a new multi-tenant app registration
To facilitate your access to another Microsoft 365 tenant, from your Microsoft 365 tenant, you need to create a new multi-tenant app registration. The Global Administrator from the external Microsoft 365 tenant can then consent to this application, granting it access to its resources. This will enable you to perform actions against resources in the external tenant while being authenticated to the app registration in your tenant via your managed identity.
Follow the below steps to create a new multi-tenant app registration in Microsoft Entra.
- Log in to the Microsoft Entra admin portal.
- Select Identity > Applications > App registrations.
- Click New registration.
- Enter a name and select the account type Accounts in any organizational directory.
- Click Register.
By default, there will be no owners of the application. You should register yourself as the owner of the application by selecting Owners > Add owner.
You will also need to configure a redirect URL for your application, so once the consent is complete, the admin user is redirected to a valid web page. In this case, I will redirect the admin user back to the Microsoft admin portal.
- Open your application using the Microsoft Entra admin portal.
- Under Manage, select Authentication.
- Click Add a platform.
- Select Web and enter https://admin.microsoft.com.
- Click Configure, then Save.
Lastly, the permissions which are consented to on the application, are the permissions that you will have in the target tenant, once the Global administrator of the target tenant consents to the application. To add permission to you application, follow the below steps:
- Open your application using the Microsoft Entra admin portal.
- Under Manage and click API permissions.
- Click Add a permission.
- Select Microsoft Graph > Application permissions.
- Find the permission from the list and check the box next to it, then click Add permission.
- Lastly, on the permissions page, click Grant admin consent for ‘yourorg’.
Federate your multi-tenant app with your managed identity
Now your app registration has been created, you will need to federate it with your managed identity. Essentially doing so ‘Links’ your managed identity to the application which allows your managed identity token to be exchanged for an app-only access token, allowing access to the resources.
Follow the below steps to federate your multi-tenant application with your managed identity.
- Open your app registration from the Microsoft Entra admin portal.
- Select Certificates and secrets.
- Select Federated credentials > Add credential.
- Next to Federated credential scenario select Other issuer.
- In the Issuer field, enter https://login.microsoftonline.com/%Home-Tenant-ID%/v2.0 and replace %Home-Tenant-ID% with the ID of the tenant you created the managed identity. This should be the same tenant you created your app registration in.
- In the Value field, enter your user-assigned managed identity’s Object (Principal) ID.
- Ensure you give your credential a name and a meaningful description, then click Add.
The result should look like the following:
Consent to your multi-tenant application from the external tenant
To enable you to access external tenants using your multi-tenant application, a global administrator in the external (or ‘target’) tenant must provide admin consent to your application. To do this, you must construct a URL and send it to the target.
The URL will look like the following, ensure you change %ClientID% to the Application (client) ID of your multi-tenant app registration.
https://login.microsoftonline.com/organizations/adminconsent?client_id=%ClientID%
The application ID can be found on the Overview tab of your app registration:
Once you have created the URL, it needs to be sent to the Global Administrator of the target tenant. When they open the URL, they will be prompted to sign in as a Global Administrator and will receive the below page which they must use to Accept consent to the application.
Once accepted, the application will show as an Enterprise application in the target tenants Enterprise applications list in the Microsoft Entra admin portal.
Connect to your target tenant with PowerShell
Now everything is in place, we can return back to our Automation Account to complete the remaining tasks:
- Create a new runtime environment.
- Import the necessary PowerShell modules.
- Connect to Microsoft Graph via the Managed Identity.
The runtime environment within Azure Automation is the environment in which a job is executed, within it, you can define the runtime language (such as PowerShell or Python), which version of that language and which package is available within the environment (such as a PowerShell module). To create a new runtime environment and import the necessary PowerShell modules, follow the below steps.
- Open your Automation Account from the Microsoft Azure Portal.
- Expand Process Automation and select Runtime Environments > Create.
- Choose a Name, then select PowerShell versions 7.4 and click Next.
- On the Packages page, select Add from gallery and add the following modules:
- Microsoft.Graph.Authentication
- Microsoft.Graph.Users
- TokenMagician
- Then click Next and Create.
The TokenMagician module created by Stephan van Rooij, is a PowerShell module wrapper for the Azure.Identity library that supports obtaining a Managed Identity token and exchanging it for an Access Token, as defined in the Microsoft documentation here.
Once the environment has been created, you need to create a new Runbook linked to that environment.
- Under Process Automation, select Runbooks.
- Click Create.
- Give the Runbook a name and set the Runbook type to PowerShell.
- Next to Runtime environment, click Select from existing and choose the environment you created.
- Click Review + Create.
The runbook will then need to be edited to include the required code to connect to another tenant using Microsoft Graph.
- Open the Runbook, then on the Overview tab, click Edit > Edit in portal.
- Enter the following code:
- TenantId = The ID of your target tenant.
- ClientId = The “
- UserAssignedIdentity = The Object ID of your user-assigned managed identity.
- Scope = The resource scope (keep this as it is).
#Import the TokenMagician module
Import-Module TokenMagician
#Gets access token for Microsoft Graph in exchange for managed identity token
$token = Get-TmMsiToken `
-TenantId = "235e5e63-c104-417e-9ed6-e96d9a456a9d" `
-ClientId = "f36f643f-d066-42f5-bfda-86ce6de6d7e3" `
-UserAssignedIdentity = "390b937d-b256-47ed-9567-45de6d617457" `
-Scope = "https://graph.microsoft.com/.default"
#Converts access token to secure string
$secureToken = ConvertTo-SecureString -String $token -AsPlainText
#Connects to Microsoft Graph with access token
Connect-MgGraph -AccessToken $secureToken
#Verify you connection and permissions within the console
Get-MgContext
Alternatively, you can use a system-assigned managed identity to obtain an access token for Microsoft Graph using the Get-TmMsiToken cmdlet. In this scenario, ensure your system-assigned managed identity is enabled on your Automation Account and remove the -UserAssignedIdentity parameter from your command.

