skip to Main Content

I am currently busy to convert my Azure AD PowerShell scripts to Microsoft Graph PowerShell. I have already some scripts that I want to run within Azure Automation, but I try to figure out how to connect to Azure Automation.

With Azure AD PowerShell, I have a connected service account in Azure Automation. With Microsoft Graph PowerShell I’m trying to use a RunAs account within the Azure Automation Account with the following connection:

$Connection = Get-AutomationConnection -Name AzureRunAsConnection
# Get certificate from the automation account
$Certificate = Get-AutomationCertificate -Name AzureRunAsCertificate
# Connect to the Graph SDK endpoint using the automation account
Connect-MgGraph -ClientID $Connection.ApplicationId -TenantId $Connection.TenantId -CertificateThumbprint $Connection.CertificateThumbprint

When I run the RunBook to create the connection I get an error:

Connect-MgGraph: C:Tempos4k24vd.4csxxxxxxxxxxxxxxxxxxx.ps1:5
Line | 5 | Connect-MgGraph -ClientID $Connection.ApplicationId -TenantId $Connec …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0,
| Culture=neutral, PublicKeyToken=xxxxxxx'. The system cannot | find the file specified.

I have the following module installed that is needed for Connect-MgGraph Microsoft.Graph.Authentication >> Runtime: 7.1 When I search on the error, it have something to do that .NET could not find the Json.NET library. But which module I’m missing in Azure Automation, or are there other ways to connect Microsoft Graph PowerShell with Azure Automation?

2

Answers


  1. I hope you are using App only Access approach to connect the Azure Automation. if not refer MSDOC – App only Authentication

    To get the Certificate and AppID you can use the below command let

    #To get App Id
    $AppId = Get-AutomationVariable -Name '<Your AppID>'
    
    # Get TenentId
    $TenantId = Get-AutomationVariable -Name '< your tenantId>'
    
    # Get Certificate
    $CertificateName = Get-AutomationCertificate -Name '<Your Certificate>'
    
    #Connect the mgGraph 
    Connect-MgGraph -ClientID $AppId -TenantId $TenantId -CertificateName $CertificateName ## Or -CertificateThumbprint 
    
    

    Still, you are facing issue please give a try Automation Hybrid Runbook Worker for more flexibility.

    Login or Signup to reply.
  2. The problem was not the first connect script, but the runtime version. After changing to PS 5.1 instead of 7.1 it all works. The Runbook now shows ‘Welcome to Welcome To Microsoft Graph!’

    $Connection = Get-AutomationConnection -Name AzureRunAsConnection
    
    # Connect to the Graph SDK endpoint using the automation account
    Connect-MgGraph -ClientID $Connection.ApplicationId -TenantId $Connection.TenantId -CertificateThumbprint $Connection.CertificateThumbprint
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search