New MiToken Graph PowerShell module for multi-tenant apps

Earlier this year, I wrote an article demonstrating how to use the new federated managed identity features to connect to multi-tenant applications using Microsoft Graph PowerShell. This enables access to resources in external tenants. To achieve this, I initially used a third-party PowerShell module. However, the module was written in C# and compiled as a binary module, limiting my ability to read and customise its contents.
 
I still wanted to use a PowerShell module for simplicity, but I needed its contents to be accessible and open for contributions from the community. For this, the module had to be a script module rather than a binary module. To solve this problem, I created MiToken.

What is MiToken?

MiToken is a PowerShell module designed for use with PowerShell Azure Automation runbooks. It retrieves an access token from the managed identity endpoint within your runbook and then exchanges it for a multi-tenant app access token, using the managed identity token as an assertion.

Install the MiToken PowerShell module

Since the module is designed to run within a managed identity environment, it must be installed in a runtime environment for your Azure Automation account. I recommend creating a new runtime environment by following the steps below.

  1. Log in to portal.azure.com and navigate to your Automation Account.
  2. Expand Process Automation and select Runtime Environments.
  3. Click Create. 
  4. Enter a Name, select the PowerShell language and select the Runtime version 7.4 and click Next.
  5. On the packages screen, click Add from gallery, then select the following modules:
    1. MiToken
    2. Microsoft.Graph.Authentication
  6. Click Next > Create.

Using MiToken in Azure Automation

The module includes a single cmdlet, which requires all three core parameters to be defined in order to successfully connect to the target tenant.

Connect-MiGraph

<# Parameters

#The Id of the tenant you wish to connect to.
-TenantId

#The Id of the multi-tenant app the external client has consented to.
-ClientId

#The Id of the managed identity which is federated with the multi-tenant app.
-IdentityId

#>

Create a new Runbook in your Automation Account which leverages the Runtime Environment that contains the required PowerShell modules. Follow the below steps:

  1. Select Process Automation > Runbooks.
  2. Click Create.
  3. Enter a Name and select the PowerShell Runbook type. 
  4. Select the Runtime Environment you just created.
  5. Click Review + Create > Create.

Testing your Runbook

To test your Runbook, simply add the connection cmdlet to your Runbook code and make a request to Microsoft Graph.

Connect-MiGraph `
-TenantId "235e5e63-c104-xxxx-9ed6-e96d9a456a9d" `
-ClientId "f36f643f-d066-xxxx-bfda-86ce6de6d7e3" `
-IdentityClientId "41de2de5-30ef-476c-8702-ea0906d2c960"

Invoke-MgGraphRequest -uri "beta/users" `
-OutputType PSObject | Select -Expand Value

You can use the test functionality of your Runbook to verify the connection is successfully established. If successfully, the test console should look like the following:

MiToken successfully connected
MiToken successfully connected

Contributing

Feel free to contribute to the project on GitHub!

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

Leave a Reply