I'm following the https://learn.microsoft.com/en-us/azure/active-directory/external-identities/b2b-quickstart-invite-powershell article, but getting an "Object reference not set to an instance of an object" error when running the New-AzureADMSInvitation command.
I'm running this from a on-prem vm. The goal is to auto-invite B2B users that are added to an application we have on-prem.
I have a registered app that I'm using to connect and have granted admin consent for the following MSGraph API permissions: User.Invite.All, User.ManageIdentities.All, User.ReadWrite.All
I load all the variables for the registered app, tenant, etc. , then this...
Import-Module -Name AzureADPreview -force
# Get Access Token
$azurePassword = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($AplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $TenantId -ServicePrincipal
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken
Connect-AzureAD -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id
After running the above it displays
If I run Get-AzureADUser -All $true
it shows all the users in my tenant correctly and it will let me add/delete regular users.
But if I try to run the following i get New-AzureADMSInvitation : Object reference not set to an instance of an object.
New-AzureADMSInvitation -InvitedUserEmailAddress $email.'InvitedUserEmailAddress' -InvitedUserDisplayName $email.Name -InviteRedirectUrl https://myapps.microsoft.com -SendInvitationMessage:$true