Created
February 23, 2023 10:34
-
-
Save Snaver/c4c2ad189666b8722916829dac316d88 to your computer and use it in GitHub Desktop.
Automatically following (star) all SharePoint Sites for users, based on https://www.cyberdrain.com/automating-with-powershell-automatically-following-all-sharepoint-sites-or-teas-for-all-users/
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
$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