Skip to content

Instantly share code, notes, and snippets.

@Snaver
Created February 23, 2023 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snaver/c4c2ad189666b8722916829dac316d88 to your computer and use it in GitHub Desktop.
Save Snaver/c4c2ad189666b8722916829dac316d88 to your computer and use it in GitHub Desktop.
$TenantID = 'CUSTOMERTENANT.ONMICROSOFT.COM'
$ApplicationId = "YOURPPLICTIONID"
$ApplicationSecret = "YOURAPPLICATIONSECRET"
$body = @{
'resource' = 'https://graph.microsoft.com'
'client_id' = $ApplicationId
'client_secret' = $ApplicationSecret
'grant_type' = "client_credentials"
'scope' = "openid"
}
# Target users in a specific group only
$groupId = "xxxxx-xxx-xxx-xxx-xxxxx"
$ClientToken = Invoke-RestMethod -Method post -Uri "https://login.microsoftonline.com/$($tenantid)/oauth2/token" -Body $body -ErrorAction Stop
$headers = @{ "Authorization" = "Bearer $($ClientToken.access_token)" }
# Retrieve the list of users who are members of the specified group
$users = @()
$nextLink = "https://graph.microsoft.com/beta/groups/$($groupId)/members"
while ($nextLink) {
$result = Invoke-RestMethod -Uri $nextLink -Headers $Headers -Method Get -ContentType "application/json"
$users += $result.value.id
$nextLink = $result.'@odata.nextLink'
}
# # Debug specific users
# $users = @(
# "xxxxx-xxxx-xxx-xxx-xx",
# "xxxxx-xxxx-xxx-xxx-xx"
# )
$users | Measure-Object
# SharePoint Site IDs
$site1 = "xxxx.sharepoint.com,xxxxx-xxxx-xxx-xx-xx,xx-xx-xx-xx-x"
$site2 = "xxxx.sharepoint.com,xxxxx-xxxx-xxx-xx-xx,xx-xx-xx-xx-x"
foreach ($userId in $users) {
# write-host $userId
$AddSitesbody = [PSCustomObject]@{
value = [array]@(
@{
"id" = $site1
},
@{
"id" = $site2
}
)
} | ConvertTo-Json
$FavoritedSites = Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/users/$($userid)/followedSites/add" -Headers $Headers -Method POST -body $AddSitesBody -ContentType "application/json"
# $FavoritedSites | Measure-Object
# write-host $FavoritedSites
write-host "Added sites to favourites for $userId"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment