Created
May 25, 2023 14:02
-
-
Save machv/f63f571e78075b28864fd9af4cbfcb0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install-module Microsoft.Graph.Users, Microsoft.Graph.Identity.SignIns, Microsoft.Graph.Groups | |
Connect-MgGraph -Scopes 'User.ReadWrite.All', 'GroupMember.ReadWrite.All', 'Group.ReadWrite.All' | |
$groupId = "c259b03c-6b11-44ea-a768-76b077d2cb93" | |
$invite = [PSCustomObject]@{ | |
mail = "[email protected]" | |
name = "Franta Novák" | |
} | |
$invitations = Import-Csv -Path "uzivatele.csv" -Delimiter ";" -Encoding UTF8 | |
foreach($invite in $invitations) { | |
$mail = $invite.mail | |
$user = Get-MgUser -Filter "startsWith(mail, '$mail')" | |
if($user) { | |
"[$mail] user exists -> adding to group" | |
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $user.Id | |
} else { | |
"[$mail] not exists -> create" | |
$guest = New-MgInvitation -InvitedUserDisplayName $invite.name -InvitedUserEmailAddress $mail -InviteRedirectUrl "https://myapplications.microsoft.com" -SendInvitationMessage:$false | |
if($guest) { #.Status -eq "PendingAcceptance") { | |
"[$mail] add to group" | |
New-MgGroupMember -GroupId $groupId -DirectoryObjectId $guest.InvitedUser.Id | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment