powershell icon indicating copy to clipboard operation
powershell copied to clipboard

[BUG] New-PnPSite using -HubSite parameter returns System.UnauthorizedAccessException

Open Nazul opened this issue 3 years ago • 0 comments

Issue

Using: New-PnPSite -Type CommunicationSite -Title '<SiteTitle>' -Url 'https://<tenant>.sharepoint.com/sites/<siteAlias>' -Description '<Description>' -Lcid '1033' -HubSiteId '<Guid>' -Connection $connectionVariable

Fails with this exception: New-PnPSite: {"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":"Attempted to perform an unauthorized operation."}}

However, by doing this:

$newSite = New-PnPSite -Type CommunicationSite -Title '<SiteTitle>' -Url 'https://<tenant>.sharepoint.com/sites/<siteAlias>' -Description '<Description>' -Lcid '1033' -Connection $connectionVariable
Add-PnPHubSiteAssociation -Site $newSite -HubSite '<HubSiteUrl>' -Connection $connectionVariable

Works without error.

This fails running it locally using both, Windows PowerShell 5.1, PowerShell 7.2 and also using Azure Functions (Windows, runtime 4 with PowerShell 7.0, 64-bit).

Expected behavior

New-PnPSite with -HubSite parameter creates a new site associated with the matching hub site.

Actual behavior

Fails with this exception: New-PnPSite: {"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":"Attempted to perform an unauthorized operation."}}

Steps to reproduce behavior

  1. Create a new connection to SharePoint admin, using a SharePoint administrator account, via $connectionVariable = Connect-PnPOnline -Credentials $credential -Url https://<tenant>-admin.sharepoint.com -ReturnConnection
  2. Use New-PnPSite with -HubSite parameter trying to create a new site that will be associated with an existing hub site
  3. Exception is received, and site is not created

What is the version of the Cmdlet module you are running?

PnP.PowerShell version 1.11.0

Which operating system/environment are you running PnP PowerShell on?

  • [X] Windows
  • [ ] Linux
  • [ ] MacOS
  • [ ] Azure Cloud Shell
  • [X] Azure Functions
  • [ ] Other : please specify

Nazul avatar Jul 26 '22 19:07 Nazul