---
title: "Discussion on \"How to use Managed Identity to connect to Azure, Exchange, Graph, Intune,... in Azure Automation Runbook\" | Hashnode"
description: "Discussion on \"How to use Managed Identity to connect to Azure, Exchange, Graph, Intune,... in Azure Automation Runbook\". Updated 6.6.2024\n\nManaged Identity is definitely a better option for authentication in Azure Automation Runbooks than the RunAs account because it doesn't require certificate/secret renewal. Therefore it is maintenance-free. However, it took me a whi..."
url: "https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to use Managed Identity to connect to Azure, Exchange, Graph, Intune,... in Azure Automation Runbook](https://doitpshway.com/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook?utm_source=hashnode&utm_medium=feed)

Updated 6.6.2024 Managed Identity is definitely a better option for authentication in Azure Automation Runbooks than the RunAs account because it doesn't require certificate/secret renewal. Therefore it is maintenance-free. However, it took me a whi...

May 12, 2022 · 8 min read · doitpshway.com  
Updated Jun 6, 2024  
Read the article

-   [#powershell](https://hashnode.com/tag/powershell)
-   [#azure](https://hashnode.com/tag/azure)
-   [#automation](https://hashnode.com/tag/automation)

## Written by

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

@AndrewZtrhgf · Czech Republic

  
[View profile](https://hashnode.com/@AndrewZtrhgf)

System Administrator that loves PowerShell

I work as System Administrator for more than 15 years now and I love to make my life easier by automating work &amp; personal stuff via PowerShell (even silly things like food recipes list generation).

## Responses 3

Markdown supported · ⌘↵ to post

[Kevin L](https://hashnode.com/@klpr)

[Apr 6, 2023](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/642e721989c68819f9bab03f)

Great article! Is there any way you can connect with a managed identity locally? I think I'm coming up with an issue you mentioned with Intune where I'm trying to use Get-MgDeviceManagementRoleScopeTag and it's not bringing back anything, (using V2 preview 8) if it was run locally could possibly confirm it didn't like it. Didn't have any luck with the vscode azure automation extension run local to connect-mggraph -identity.

And I've added all the perms to the system management identity.

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[Apr 6, 2023](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/642e721989c68819f9bab03f#reply-642e7318eaad3d174f73aaaf)

Not sure what you mean by running it locally?

[Kevin L](https://hashnode.com/@klpr)

[Apr 7, 2023](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/642e721989c68819f9bab03f#reply-642f9ccf21057334de567a73)

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf) instead of testing and running the runbook from the portal would it be possible to run locally from vscode or powershell ise? Whilst authenticating with the managed identity.

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[Apr 8, 2023](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/642e721989c68819f9bab03f#reply-64317a4194d1164842d99272)

[Kevin L](https://hashnode.com/@klpr) I don't know. Sorry

[Luciano](https://hashnode.com/@moneten)

[Oct 14, 2022](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/63490acc1b098b3c50f27cd3)

Hi and thanks for your post! Do you have an idea how to connect to MicrosoftTeams and manage user telefon settins?

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[Oct 30, 2022](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/63490acc1b098b3c50f27cd3#reply-635e57aeb7bb8c3f4ee42567)

Hi, sorry haven't tried/needed that yet.

[Bruce](https://hashnode.com/@bhgf)

[May 17, 2022](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/62834b6b83e2720b553c65ec)

I could not find anything else out there on this use case!

But I have determined my runbook is getting stuck on:

$MSI = (Get-AzureADServicePrincipal -Filter "displayName eq '$automationAccountDisplayName'"

Pretty sure I got the $automationAccountDisplayName correct, so I am wondering what permissions I need to confirm exist, that perhaps you alluded to?

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[May 17, 2022](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/62834b6b83e2720b553c65ec#reply-62834d65504da209d85573f0)

Thanks!

Not sure what you mean by "runbook is getting stuck". Granting the permissions is done outside the runbook, for example in PS console where you are connected to Azure as Global Admin.

Anyway to get all service principals, so you can easily search them you can use command: Get-AzureADServicePrincipal -All:$true | Out-GridView

[Bruce](https://hashnode.com/@bhgf)

[May 17, 2022](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/62834b6b83e2720b553c65ec#reply-62835141504da209d85574e8)

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

Thanks, I confirmed the service principal name for the Azure Automation account using that command.

The runbook does not really get stuck.  
What happens is that I run a test it edit mode.  
It completes, but I can tell the whole script has not run  
(as debug output statements are missing)

Connect-AzAccount -Identity  
\-> this works

$MSI = (Get-AzAzureADServicePrincipal -Filter "displayName eq '$automationAccountDisplayName'")  
\-> nothing is executed on or after this line

[Ondrej Sebela](https://hashnode.com/@AndrewZtrhgf)

[Jun 23, 2022](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/62834b6b83e2720b553c65ec#reply-62b4c19db86f939ac81b1260)

[Bruce](https://hashnode.com/@bhgf)

So you are talking about granting the permissions section. But these commands should be run in your interactive PowerShell console, not under the managed identity in Azure Automation. I've edited the post so hopefully, it will be more clear.
