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.
- Log in to portal.azure.com and navigate to your Automation Account.
- Expand Process Automation and select Runtime Environments.
- Click Create.
- Enter a Name, select the PowerShell language and select the Runtime version 7.4 and click Next.
- On the packages screen, click Add from gallery, then select the following modules:
- MiToken
- Microsoft.Graph.Authentication
- 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:
- Select Process Automation > Runbooks.
- Click Create.
- Enter a Name and select the PowerShell Runbook type.
- Select the Runtime Environment you just created.
- 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:
Contributing
Feel free to contribute to the project on GitHub!

