Skip to content

Instantly share code, notes, and snippets.

@machv
Created May 25, 2023 14:02
Show Gist options
  • Save machv/f63f571e78075b28864fd9af4cbfcb0d to your computer and use it in GitHub Desktop.
Save machv/f63f571e78075b28864fd9af4cbfcb0d to your computer and use it in GitHub Desktop.
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