[BUG] - Adding Site Collection Admins using Set-PnPTenantSite throws unauthorized error
Notice
Many bugs reported are actually related to the PnP Framework which is used behind the scenes. Consider carefully where to report an issue:
- Are you using
Invoke-PnPSiteTemplateorGet-PnPSiteTemplate? The issue is most likely related to the Provisioning Engine. The Provisioning engine is not located in the PowerShell repo. Please report the issue here: https://github.com/pnp/pnpframework/issues. - Is the issue related to the cmdlet itself, its parameters, the syntax, or do you suspect it is the code of the cmdlet that is causing the issue? Then please continue reporting the issue in this repo.
- If you think that the functionality might be related to the underlying libraries that the cmdlet is calling (We realize that might be difficult to determine), please first double check the code of the cmdlet, which can be found here: https://github.com/pnp/powershell/tree/master/src/Commands. If related to the cmdlet, continue reporting the issue here, otherwise report the issue at https://github.com/pnp/pnpframework/issues
Reporting an Issue or Missing Feature
Reporting an issue with setting owners for a sitecollection using Set-PnPTenantSite
Expected behavior
Adding a user with SharePoint Admin role to all the site collections as admin using Set-PnPTenantSite -Identity <> -Owners <> cmdlet
Actual behavior
Get an error "Set-PnPTenantSite: Attempted to perform an unauthorized operation."
Steps to reproduce behavior
Run under a context of user with SharePoint Admin role assigned Use Connect-PnPOnline to connect to SiteCollection to which the admin should be added as SiteCollectionAdmin Use Set-PnPTenantSite to add a user as SiteCollectionAdmin Error : "Set-PnPTenantSite: Attempted to perform an unauthorized operation."
What is the version of the Cmdlet module you are running?
1.6.26-nightly
Which operating system/environment are you running PnP PowerShell on?
- [ ] Windows
- [ ] Linux
- [ X] MacOS
- [ ] Azure Cloud Shell
- [ ] Azure Functions
- [ ] Other : please specify
@sairao77 , @siddharth-vaghasia - I think this is by design as follows the same behavior as that in UI. You need to an admin of the site collection to add other admins.
The unauthorized error means that the SharePoint admins don't have access to the site collection, so you need to login to that site collection itself and add the admin.
@gautamdsheth - In my case, I am first connecting to SharePoint Admin URL with user credential who is SharePoint Admin... and so ideally all future commands should run under the context of the SharePoint Admin....
Also, I can confirm that the same script and command works fine in the legacy module 'SharePointPnPPowerShellOnline'... For now, I have moved to the older module...it is just I think the same thing should supported in the new version also....
Function SetUserPermission(){
try{
Write-Host "Setting User Permission..." -ForegroundColor Yellow
Connect-PnPOnline -Url https://tenant-admin.sharepoint.com/ -Credentials $Credentials
Set-PnPTenantSite -Url https://tenant.sharepoint.com/sites/TestSiteWithoutAccess -Owners '[email protected]'
Write-Host "Setting User Permission Successful..." -ForegroundColor Yellow
}
catch{
Write-Host "Error in setting user permission'" $_.Exception.Message -ForegroundColor Red
}
}
@gautamdsheth - Adding on to what @siddharth-vaghasia stated. According to the docs link below we would use Set-PnPTenantSite when we are not a site collection admin of the site and use Add-PnPSiteCollectionAdmin if we are already an admin on that SC. https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnpsitecollectionadmin?view=sharepoint-ps
I'm seeing this same behavior since updating to the latest Pnp.Powershell module. Previously this script worked fine, but now its throwing unauthorized even though I'm logged in as a Sharepoint Admin:
PS C:\PowerShell> Set-PnPTenantSite -Identity $OneDriveSiteUrl -Owners $adminEmail Set-PnPTenantSite : The remote server returned an error: (401) Unauthorized. At line:1 char:1
- Set-PnPTenantSite -Identity $OneDriveSiteUrl -Owners $adminEmail
Something clearly changed in the latest updates, as of 1.5 it was working and now 1.7 is throwing this error, and my code hasn't changed. EDIT: Rolling back my modules to required version 1.5.0 has allowed the connections to work as expected, 1.6 was also broken, apparently I hadn't updated since 1.5 version.
I'm having the same Problem trying to update the sharing permissions for SiteCollections. I'm running my Script under SharePoint App-only principal with tenant permissions image The script was running for month now but stopped working 2-5 days ago. With 401 error. I even tried to create a new App permission, but it's giving me the same error. I was able to pin-point the problem even further for me. This call is working fine: Set-PnPTenantSite -Identity https://$($tenantName).sharepoint.com/sites/$($EMail) -SharingAllowedDomainList "xxx.com,YYY.com" -SharingDomainRestrictionMode AllowList but if I include -SharingCapability ExistingExternalUserSharingOnly as needed, I get a 401 message. Seems to be really strange, that just one parameter isn't working, as of last week
The issue still can be reproduced by using the following cmdlet and throwing Unauthorized or Forbidden Error.
Set-PnPTenantSite -Identity https://domain.sharepoint.com/sites/mysite1 -Owners [email protected]
Same with using Set-PnPSite Set-PnPSite -Identity https://domain.sharepoint.com/sites/mysite1 -Owners [email protected]
I hope this can be fix soon as we got stuff running in the background using the cmdlets
I'm experiencing the same issue. I'm using an account with SharePoint admin role and perform this action using UI but not PnP version 1.7.0
Use the following steps to reproduce the issue:
- Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -UseWebLogin (i'm using MFA enabled account)
- Set-PnPTenantSite -Identity "https://yourtenant.sharepoint.com/sites/yoursite" -Owners "[email protected]"
I get the following error: Set-PnPTenantSite : The remote server returned an error: (403) Forbidden.
Any updates? It seems this has been open awhile now with no action or response. We are thinking of moving off of PnP back to SharePoint Online Module as we don't want to manually assign owners to 300 sites that have been created during the time we've been waiting for this to get addressed.
@TA1973 - This is a community driven module, so there's no ETA on resolving issues as we also have day jobs or unforeseen priorities, but we usually do our best. If the SharePoint Online module works, then that's at least an immediate solution for you. Feel free.
Issue has been fixed, please try with the latest nightly and reopen this if it still exists !
This was resolved as of version 1.8.0, but is somehow an issue again in v1.10.0. Was this fix inadvertently reverted?
This issue still exists in 1.12.0
I have the same issue in PnP.PowerShell 2.4.0 Version, how to bypass this?
i need to add one group of my "SharePoint Online Admins" to all my site collection, someone know a workaround?
I thought I was having an issue with this, but it turns the LockState property on some of the SharePoint sites was set to "ReadOnly", so no changes could be made (including adding or removing admins). Setting it back to "Unlock" got it working.
It's 2024, I'm on PnP 2.12.0, and I'm here searching for the same issue... Anyway, I managed to find a workaround.
Suppose you have PowerShell variables:
$adminCentreUrl: your admin centre, e.g. https://contoso-admin.sharepoint.com$siteUrl: your site URL, e.g. https://contoso.sharepoint.com/teams/mysite$clientId: client ID for use with PnP with appropriate permissions
You also need to know the GUID of the account or group you want to use, so you can build a claims account name.
Then you can try code like this:
Connect-PnPOnline $adminCentreUrl -Interactive -ClientId $clientId
$siteAdminClaims = "c:0t.c|tenant|c560df76-1b93-405d-a379-6f12b8c1816b"
$context = Get-PnPContext
$tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant $context
$context.Load($tenant)
$context.ExecuteQuery()
$tenant.SetSiteAdmin($siteUrl, $siteAdminClaims, $true) | Out-Null
$context.ExecuteQuery()
Still reproducible in verion 3.1.0.
Still reproducible in version 3.2.0