Forum Discussion

Matt_P_Standing's avatar
Matt_P_Standing
Brass Contributor
Feb 13, 2024
Solved

Object reference not set when using New-MgInvitation with Runbook and Managed Identities

I am trying to send an AzureAD guest invitation using an Azure Runbook and GraphAPI. I have created a system managed identity for this (can successfully run Connect-MgGraph -identity in the runbook).
When I try and execute:

 

New-MgInvitation -InvitedUserDisplayName "John Doe" -InvitedUserEmailAddress email address removed for privacy reasons -InviteRedirectUrl "https://myapplications.microsoft.com" -SendInvitationMessage:$true

 

I get the following error:

New-MgInvitation : Object reference not set to an instance of an object. At line:4 char:1 + New-MgInvitation -InvitedUserDisplayName "John Doe" -InvitedUser ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [New-MgInvitation_CreateExpanded], NullReferenceException + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.NewMgInvitation_CreateExpanded 

 

I have a managed identity for the runbook, with the following permissions:

 

Microsoft.Graph.Identity.SignIns and Microsoft.Graph.Authentication modules are both loaded in my automation account.

 

If I run the code in a local PowerShell session it works. Has anyone had any experience with this?

 

Thanks in advance,

Matt

  • I wasn't able to resolve this but found a workaround. Instead of using the dedicated PowerShell command I was able to use the REST API to generate the same request:

     

    $body = ConvertTo-Json -InputObject @{InvitedUserEmailAddress="email address removed for privacy reasons";InviteRedirectURL="https://myapps.microsoft.com";SendInvitationMessage= $true}
    
    $uri="https://graph.microsoft.com/v1.0/invitations"
    
    $response = Invoke-MgGraphRequest -method POST -Uri $URi -Body $Body

1 Reply

  • I wasn't able to resolve this but found a workaround. Instead of using the dedicated PowerShell command I was able to use the REST API to generate the same request:

     

    $body = ConvertTo-Json -InputObject @{InvitedUserEmailAddress="email address removed for privacy reasons";InviteRedirectURL="https://myapps.microsoft.com";SendInvitationMessage= $true}
    
    $uri="https://graph.microsoft.com/v1.0/invitations"
    
    $response = Invoke-MgGraphRequest -method POST -Uri $URi -Body $Body

Resources