---
title: "Comment by Bruce on \"How to use Managed Identity to connect to Azure, Exchange, Graph, Intune,... in Azure Automation Runbook\" | Hashnode"
description: "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 &qu"
url: "https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161/comment/62834b6b83e2720b553c65ec"
---

[Discussion](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161)

Comment [How to use Managed Identity to connect to Azure, Exchange, Graph, Intune,... in Azure Automation Runbook](https://hashnode.com/posts/how-to-use-managed-identity-to-connect-to-azure-exchange-graph-intune-in-azure-automation-runbook/627d01164903bec29b587161)

[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.
