Forum Discussion
Tim_Opie
Mar 08, 2023Copper Contributor
Adding B2B guests and adding to security group
Hi, New-ish to Powershell so hoping this isn't a dumb question. It all seems to work as parts but wont run as a whole. The inviting the guest works, just wont add the user to the test security g...
Tim_Opie
Mar 14, 2023Copper Contributor
Thank you very much for your reply!
I have tried the changes you have suggested but is now giving this error below. I wonder if it is because its trying to add the user before its been created? (If the user isnt created yet then the RefObjectID being equal to null would make sence).
Add-AzureADGroupMember : Cannot bind argument to parameter 'RefObjectId' because it is null.
At line:19 char:22
+ ... RefObjectID ((Get-AzureAdUser -All $true | Where-Object {$_.DisplayNa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-AzureADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Open.AzureAD16.PowerShell.AddGroupMem
ber
Tim_Opie
Mar 15, 2023Copper Contributor
Think I have somehow got it, this seems to run with a single user, will test with multiples soon. Probably not technically right but it works so far so thats the main thing!
$invitations = import-csv C:\B2BUploads\Test.csv
$messageInfo = New-Object Microsoft.Open.MSGraph.Model.InvitedUserMessageInfo
$messageInfo.customizedMessageBody = "Welcome message"
$GroupID = ((Get-AzureADGroup -Filter "Displayname eq 'Test'").ObjectID)
$newuser = foreach ($email in $invitations) {
New-AzureADMSInvitation `
-InvitedUserEmailAddress $email.InvitedUserEmailAddress `
-InvitedUserDisplayName $email.Name `
-InviteRedirectUrl https://google.com `
-InvitedUserMessageInfo $messageInfo `
-SendInvitationMessage $true
}
$newuser `
Start-Sleep -Seconds 2
foreach ($email in $invitations) {
Add-AzureADGroupMember `
-ObjectId $GroupID `
-RefObjectID ((Get-AzureAdUser -All $true | Where-Object {$_.DisplayName -in $email.Name}).ObjectID)
}
- Tim_OpieMar 15, 2023Copper ContributorSeems to work for multiple users also, thansk so much for helping me get there!