Forum Discussion
B2B Invite error
Hi, Tim.
Did you import the module first before calling New-Object?
Import-Module -Name Microsoft.Graph.Identity.SignIns;
Here's a couple of extra curiosities:
- Under the 2.21.1 modules, the [Microsoft.Graph.PowerShell.Models.MicrosoftGraphInvitation] type is missing from the Microsoft.Graph.Beta.Identity.SignIns module. However, it remains available in the Microsoft.Graph.Identity.SignIns module;
- Apparently, you can use a HashTable instead:
$InvitedUserMessageInfo = [Microsoft.Graph.PowerShell.Models.MicrosoftGraphInvitation]@{
CustomizedMessageBody = "Welcome!";
}
New-MgInvitation -InvitedUserDisplayName "{Name}" -InvitedUserEmailAddress "{Email_Address}" -InviteRedirectUrl "{URI}" -InvitedUserMessageInfo $InvitedUserMessageInfo;
I haven't tested the HashTable approach but may do so later - specifically with the beta module, given it currently lacks the formal type definition.
Cheers,
Lain
- Tim_OpieAug 11, 2024Copper Contributor
Ah right, thanks! I have tried loading the module, but I still get the same error.
I also tried the HashTable method you have suggested which technically worked (account was added as a B2B guest) but didn't send the invite to my test account, any suggestions why it might not?Appreciate the assistance!
Tim
- Tim_OpieAug 12, 2024Copper Contributor
Found another script which seems to get around that issue. Think I might have been using the wrong syntax for the account creation also haha. Used to work anyway. Cheers for the help!
# Connect to Microsoft Graph Connect-MgGraph -Scopes "User.Invite.All" # Path to the CSV file $csvPath = "C:/temp/file.csv" # Import the CSV file $guestUsers = Import-Csv -Path $csvPath $invitedUserMessage = "Welcome" foreach ($guestUser in $guestUsers) { # Read guest user details from CSV $guestUserEmail = $guestUser.Email $invitedUserDisplayName = $guestUser.DisplayName # Create the invitation $invitation = New-MgInvitation -InvitedUserEmailAddress $guestUserEmail -InvitedUserDisplayName $invitedUserDisplayName -InviteRedirectUrl "URL" -SendInvitationMessage -InvitedUserMessageInfo @{customizedMessageBody = $invitedUserMessage} # Output the result if ($invitation.Status -eq "PendingAcceptance") { Write-Output "Invitation sent successfully to $guestUserEmail" } else { Write-Output "Failed to send invitation to $guestUserEmail" } }- LainRobertsonAug 12, 2024Silver Contributor
I made a mistake with the HashTable-only example (I was careless and copied the typed HashTable). It should have looked like this
$InvitedUserMessageInfo = @{ CustomizedMessageBody = "Welcome!"; }Cheers,
Lain