pnp / powershell

PnP PowerShell
https://pnp.github.io/powershell
MIT License
651 stars 336 forks source link

[BUG] New-PnPSite does not work correctly with managed identity. #3606

Open cjj8Finch opened 9 months ago

cjj8Finch commented 9 months ago

Reporting an Issue or Missing Feature

Please confirm what it is that your reporting I'm having an issue when creating a new Team site using a managed identity within an Azure Runbook. The azure runbook is PowerShell 7.2, PnP.PowerShell is 2.2.0 - added from the gallery. I can run the same logic used in the runbook interactively without error in PowerShell 7.2 on my local machine.

I believe this to be a bug because issuing the command New-PnPSite errors out with a '401 Unauthorised' message. However, the site is created, the users specified are added as owners but interestingly the new site is not associated with the hub site specified with the -hubsiteid parameter.

I have granted my managed identity the following permissions using 'Add-PnPAzureADServicePrincipalAppRole': 'Sites.FullControl.All' Type: 'SharePointOnline' 'Directory.ReadWrite.All' Type 'MicrosoftGraph'

I found another user having the same issue here:

2716

Expected behavior

The new site to be created, the url of the new site returned so it can be used in subsequent steps

Actual behavior

The new site is created. However, the command 'fails' with a '401 Unauthorized' message the value returned by the command is null (presumably because it fails)

Steps to reproduce behavior

` [array]$owners = ("abc1@mytenant.ac.uk")

[string]$title = "A site title"

[string]$shorturl = "ASiteTitle"

Connect-PnPOnline -Url https://*mytenantsite*-admin.sharepoint.com -ManagedIdentity

$hubsite = Get-PnPHubSite -Identity "https://*mytenantsite*.sharepoint.com/sites/mytargethubsite"

$site = New-PnPSite -Type TeamSite -Owners $owners -Title $title -Alias $shorturl -Description $Title -TimeZone UTC_GREENWICH_MEAN_TIME_DUBLIN_EDINBURGH_LISBON_LONDON -HubSiteId $hubsite.siteid.guid `

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

2.2.0

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

cjj8Finch commented 9 months ago

I forgot to add. I found old posts where this command failed if you specified a Hubsite to join so I tried it without specifying a hubsite to join planning to join it in a subsequent step but sadly even without the hubsiteid param the command fails out with 'The remote server returned an error: (401) Unauthorized.'

cjj8Finch commented 9 months ago

Further to this I can workaround the issue since the site is created despite the error by using "-erroraction continue" on the new-pnpsite command. Then waiting a little bit of time for everything to provision and since I already know what the url is going to be I then just connect to the new site without an issue. Its far from ideal and probably not something I can use in a live environment since its just a workaround but just in case anyone else has this issue.

wfi-jonas commented 5 months ago

same issue: https://github.com/pnp/powershell/issues/3594

also in this I found an issue related to the timezone that provides unauthorized errors: https://github.com/pnp/powershell/issues/3634

gkDoSt commented 1 month ago

I am facing the same issue within an Azure Automation Account with the latest "PnP.PowerShell" version (2.5.0). Is there already an update for this?