pnp / powershell

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

[BUG] New-PnpSite with Managed Identity gives "Object reference not set to an instance of an object. " but still creates the Site #4082

Open CGrossNteam opened 1 month ago

CGrossNteam commented 1 month ago

Reporting an Issue or Missing Feature

I'm using the "New-PnPSite" cmdlet inside of a Runbook, which is used with a Managed Identity.

The Managed Idetity has the following Permissions:

I connect to the SharePoint Admin Site the following way:

$ConnectionAdminCenter = Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com" -ManagedIdentity -ReturnConnection

And then use the "New-PnpSite" cmdlet this way:

New-PnPSite -Type TeamSite -Alias "Site-Alias" -Title "Site-Name" -Description "Description" -Owners "owner@tenant.com" -Lcid 1031 -Wait -Connection $ConnectionAdminCenter

When I try to create a "TeamSiteWithoutMicrosoft365Group" it works without issue.

Expected behavior

The Site will be created with the parameters given. After that the runbook will continue with what comes afterwards.

Actual behavior

The Site is created and after about 10 minutes the Runbook shows the Status "Error" and in the "Exceptions" Tab I see:

Object reference not set to an instance of an object. (Object reference not set to an instance of an object.)

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

I'm running Version 2.5.12-nightly of the PnP.Module.

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

jackpoz commented 1 month ago

Could you try passing -ErrorAction:Stop as additional parameter and then logging more exception details with Get-PnPException | Select-Object * ?

CGrossNteam commented 1 month ago

This is the Exception I'm getting:

Message : Object reference not set to an instance of an object. Stacktrace : at PnP.PowerShell.Commands.NewSite.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Admin\NewSite.cs:line 145 at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 53 ScriptLineNumber : 160 InvocationInfo : System.Management.Automation.InvocationInfo Exception : System.NullReferenceException: Object reference not set to an instance of an object. at PnP.PowerShell.Commands.NewSite.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Admin\NewSite.cs:line 145 at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 53 CorrelationId : 01de3aa1-7025-9000-74f1-529dedd7ac88 TimeStampUtc : 7/10/2024 8:23:19 AM

jackpoz commented 1 month ago

That shows that it's line https://github.com/pnp/powershell/blob/15497cce527d05de301b3b2659392a24613cf74b/src/Commands/Admin/NewSite.cs#L145 and it happens because something else went wrong during the creation, with returnedContext being null. Do you know if the site was created ?

CGrossNteam commented 1 month ago

The site and the coresponding Group are being created.

I can navigate to it. The Owner is set.

But your comment brought me to a possible cause. The SharePoint is almost at its limit space wise. Could that cause the lead to the cmdlet not being able to find the URL after creation?

gautamdsheth commented 2 weeks ago

@jackpoz / @CGrossNteam - can you please try again with tomorrow's or later nightly builds and let us know ? We have made quite a few changes related to managed identity auth , maybe it helps ?

jackpoz commented 1 week ago

I don't have managed identity set up so I can't test it.

CGrossNteam commented 3 days ago

@gautamdsheth

Sorry for the late response.

Even after the Update to the newest Nightly Build (2.10.7) he problem still persists. We resolved the space issue. but we still get the same error message:

Message : Object reference not set to an instance of an object. Stacktrace : at PnP.PowerShell.Commands.NewSite.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Admin\NewSite.cs:line 145 at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 53 ScriptLineNumber : 160 InvocationInfo : System.Management.Automation.InvocationInfo Exception : System.NullReferenceException: Object reference not set to an instance of an object. at PnP.PowerShell.Commands.NewSite.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Admin\NewSite.cs:line 145 at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 53 CorrelationId : 68534ca1-707b-9000-d979-879679e33561 TimeStampUtc : 9/2/2024 2:11:53 PM

Since we can create a "Team Site without Microsoft 365 Group", I tried another way. After creation of the Site I tried to Groupify the Site with "Add-PnPMicrosoft365GroupToSite". But this also didn't work. I'm getting the following error:

Message : An error occurred while processing this request. Stacktrace : at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServerAsync(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryAsync() at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQueryAsync() at Microsoft.SharePoint.Client.ClientContext.ExecuteQueryAsync() at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, String userAgent) at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryRetry(ClientRuntimeContext clientContext, Int32 retryCount, String userAgent) at Microsoft.SharePoint.Client.TenantExtensions.GroupifySite(Tenant tenant, String siteUrl, TeamSiteCollectionGroupifyInformation siteCollectionGroupifyInformation) at PnP.PowerShell.Commands.Admin.AddMicrosoft365GroupToSite.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Admin\AddMicrosoft365GroupToSite.cs:line 63 at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 53 ScriptLineNumber : 166 InvocationInfo : System.Management.Automation.InvocationInfo Exception : Microsoft.SharePoint.Client.ServerException: An error occurred while processing this request. at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServerAsync(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryAsync() at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQueryAsync() at Microsoft.SharePoint.Client.ClientContext.ExecuteQueryAsync() at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext clientContext, Int32 retryCount, String userAgent) at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryRetry(ClientRuntimeContext clientContext, Int32 retryCount, String userAgent) at Microsoft.SharePoint.Client.TenantExtensions.GroupifySite(Tenant tenant, String siteUrl, TeamSiteCollectionGroupifyInformation siteCollectionGroupifyInformation) at PnP.PowerShell.Commands.Admin.AddMicrosoft365GroupToSite.ExecuteCmdlet() in D:\a\powershell\powershell\src\Commands\Admin\AddMicrosoft365GroupToSite.cs:line 63 at PnP.PowerShell.Commands.Base.PnPConnectedCmdlet.ProcessRecord() in D:\a\powershell\powershell\src\Commands\Base\PnPConnectedCmdlet.cs:line 53 CorrelationId : 2c8f4ca1-a0f3-9000-d979-80725ca95d29 TimeStampUtc : 9/3/2024 7:25:13 AM

I read in another thread, that the user to Groupify the Site has to be Site Collection Admin. But I'm using a Managed Identity. These are the Permissions granted to the Managed Identity:

image

Is there a way to make this work?