This PowerShell used to add any other user in SCA (Site Collection Administrator). You have to add all sites in .csv file (mention full path of .csv in $csvPath). PowerShell code will read the .csv file and it will one by one connect sites using PnP. You have to add all users in $allEmailIDs array. $allErrorSites will print all error sites.
The error will be,
- If you do not have access to that site.
- If the email id is not valid.
Use below code in PowerShell,
- $csvPath = ".CSV FILE PATH"
- $allSitesUrls = Get - Content $csvPath
- $allEmailIDs = @("EMAIL ID - 1", "EMAIL ID - 2")
- $allErrorSites = @()
- try {
- ForEach($singleSiteUrl in $allSitesUrls) {
- : InnerLoop ForEach($emailID in $allEmailIDs) {
- try {
- Write - Host "Start Process for : $($singleSiteUrl)";
- Connect - PnPOnline $singleSiteUrl - UseWebLogin
- $clientContext = Get - PnPContext
- $web = $clientContext.Web;
- $clientContext.Load($web);
- $clientContext.ExecuteQuery();
- $spUser = $clientContext.Web.EnsureUser($emailID);
- $spUser.IsSiteAdmin = $true;
- $spUser.Update();
- $clientContext.Load($spUser)
- $clientContext.ExecuteQuery()
- Write - Host "SCA access provided to: $($spUser.Email) to site $($web.Title) ($($web.Url)";
- } catch {
- #Add the object with property to an Array
- $allErrorSites += $singleSiteUrl;
- [string] $FuncName = $MyInvocation.MyCommand.Name;
- Write - Host - Message $_ - FunctionName $FuncName;
- }
- }
- }
- #It will print all sites which got error
- Write - host - f Red $allErrorSites;
- } catch {
- [string] $FuncName = $MyInvocation.MyCommand.Name;
- Write - Host - Message $_ - FunctionName $FuncName;
- }