Run Unattended MgGraph PowerShell Code with Azure Arc Managed Identity

Introduction

Microsoft Graph API is a powerful tool for automating and managing resources within the Microsoft 365 ecosystem. However, running unattended automation scripts with Microsoft Graph presents security and authentication challenges. Traditionally, service principals (app registrations) and client secrets or certificates are used to authenticate API requests. However, these methods introduce security risks, such as secret leakage, and require lifecycle management.

A more secure and efficient alternative is Azure Arc-enabled managed identity for on-premises servers, which allows seamless authentication without managing service principal credentials.

With Azure Arc, you can onboard on-premises or non-Azure servers to Azure Resource Manager (ARM). This allows the server to leverage managed identities, providing a seamless and secure authentication mechanism to access Microsoft Graph API.

How Azure Arc-Enabled Managed Identity Solves These Challenges

✅ Eliminates Secrets and Certificates – No need to store and rotate secrets.
✅ Native Integration with Microsoft Entra ID (formerly Azure AD) – Enables secure API authentication.
✅ Applies Least Privilege Principle – You can assign only necessary permissions.
✅ Securely Extends Azure Capabilities to On-Premises – Brings Azure-native security to on-prem resources.

 

Onboard Your On-Premises Server to Azure Arc

Azure Arc Setup is included by default on new Windows Server 2022 installations and later. 

It typically sits under %windir%\AzureArcSetup\ArcSetup\AzureArcSetup.exe 

For more information click to learn more on how to Connect Windows Server machines to Azure through Azure Arc Setup

Grant Graph API Permissions to Arc-enabled on-prem Server Managed Identity

Once the machine has connected to Azure, head to your Azure Portal (Portal.azure.com) and search for Azure Arc, then Machines

This will allow you to assign API permissions to the server similar to any Azure App Registration or Service Principal with a Managed System Identity.

Searching the server name in Enterprise Applications (filtering by Managed Identity), you should be able to find it listed

Clicking on the server name, and going to Permissions, there is no way to grant API permissions directly through the console. For that we will need to use Powershell!

To grant permissions to your Server Managed Identity, open up Powershell on your PC, and run the following code.
You will need to replace the variable values for $tenantID and $ManagedIdenityID. Check the inline comments in the code for more details.

The output should look like this

Go back to the Azure portal and refresh the page on which the permissions are granted, and it should show that the server managed identity has been successfully granted permissions to call Microsoft Graph for the permissions specified 

Head to your Arc-enabled server and login as Administrator and fire up PowerShell ISE (or VSCode) 

Ensure you have installed the relevant module from Microsoft.Graph for your needs. In this example, I am going to query Intune Managed devices in my tenant so will be installing Microsoft.Graph.DeviceManagement

Now, we will connect to MgGraph without using any credentials! we will simply use the identity of the server to connect.

The output should look as follows. Notice that the scopes shows the access token has the correct permissions for your automation needs and the AuthType and TokenCredentialType are ManagedIdentity. 

Let’s query Intune managed devices and get the lastsyncdatetime and OS name for them

 

Conclusion

Using Azure Arc-enabled managed identity for running unattended Microsoft Graph automation on an on-premises server removes the need for credentials, enhances security, and simplifies authentication. It ensures a zero-trust approach while maintaining compliance and efficiency.

By leveraging Azure Arc, organizations can extend cloud-native security and identity management to their on-premises infrastructure, making automation with Microsoft Graph both seamless and secure.



 

Run Unattended MgGraph PowerShell Code with Azure Arc Managed Identity
Tagged on:                         

Amir Joseph Sayes

Cloud | EUC | Virtualisation | Enterprise Mobility | DevOps | Automation | Part-Time Blogger | Fitness

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Amir Sayes

Subscribe now to keep reading and get access to the full archive.

Continue reading