Connect Microsoft Graph in Azure Automation

João Ramos 20 Reputation points
2023-11-13T14:24:52.8633333+00:00

Hi There,

I want to run a powershell script on azure automation, but it requires to connect to Microsoft graph. I tried in many different ways, but it gives me errors. Is it possible to do this? What is the correct way?

Thank you,

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-11-14T02:08:06.89+00:00

    Hi @João Ramos

    It would've been helpful to the community to post the script you're using and the errors you're getting to best assist you. However, connecting to Microsoft Graph depends on which PowerShell your runbook using, i.e., 5.2 or 7 as there are different modules.

    You can use the Connect-AzureAD cmdlet to connect to Microsoft Graph in PowerShell 7, which is what I recommend. This cmdlet will use the context of automation account to connect Azure AD. However, if you wanted to use a different credential, you could use the following:

    $credential = Get-AutomationPSCredential -Name 'GraphCredential'
    Connect-AzureAD -Credential $credential
    

    The Automation account should come preloaded with the Az modules but if you need to, you can use Import-Module Az to add the modules. From there, you can you use Graph cmdlets you need.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.