Last active
July 30, 2019 13:15
-
-
Save poiriersimon/7038d6de892779513634ff5c4ab6a135 to your computer and use it in GitHub Desktop.
Add User to group in batch with Graph
This file contains hidden or 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
#You need Powershell API module @ https://github.com/poiriersimon/PowershellAPIModule | |
Import-Module "C:\Modules\PowershellAPI\PowershellAPI.psd1" | |
$AdminAccount = "[email protected]" | |
$clientid = "1b730954-1685-4b74-9bfd-dac224a7b894"; | |
$RessourceURI = "https://graph.windows.net"; | |
$RedirectURI = "urn:ietf:wg:oauth:2.0:oob"; | |
#FindGroupId | |
$Groupids = Invoke-GraphApi -Resource groups -UserPrincipalName $AdminAccount -ClientID $Clientid -redirectUri $RedirectURI | select Displayname,id | |
# Choose une from here Need to be a Office 365 group or a security group based on https://docs.microsoft.com/en-us/graph/api/group-post-members?view=graph-rest-1.0&tabs=http | |
$GroupID = "2fddd7e5-eed3-41ef-8a2a-555fea462451" | |
$Users = Invoke-GraphApi -Resource users -UserPrincipalName $AdminAccount -ClientID $Clientid -redirectUri $RedirectURI |where{$_.userprincipalname -like "user*"} |select userprincipalname | |
##Batch Request | |
$Batchheaders = [pscustomobject]@{ | |
"Content-Type" = "application/json" | |
} | |
$myBatchRequests = @() | |
[int]$requestID = 0 | |
Foreach($user in $users){ | |
$requestID ++ | |
$BBody = [pscustomobject]@{ | |
"@odata.id" = "https://graph.microsoft.com/v1.0/users/$($user.userprincipalname)" | |
} | |
$myRequest = [pscustomobject][ordered]@{ | |
url = "/groups/$GroupID/members/`$ref" | |
method = "POST" | |
id = $requestID | |
body = $BBody | |
headers = $Batchheaders | |
} | |
$myBatchRequests += $myRequest | |
} | |
$allBatchRequests = @() | |
$allBatchRequests += [pscustomobject][ordered]@{ | |
requests = $myBatchRequests | |
} | |
$batchBody = $allBatchRequests | ConvertTo-Json -Depth 4 | |
$Result = Invoke-GraphApi -Resource `$batch -UserPrincipalName $AdminAccount -ClientID $Clientid -redirectUri $RedirectURI -Body $batchBody -Method Post | |
$Result.responses | |
#If Error | |
$Result.responses |select -ExpandProperty body | select -ExpandProperty error | fl message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment