powershell
powershell copied to clipboard
[BUG] New-PnpSite with Managed Identity gives "Object reference not set to an instance of an object. " but still creates the Site
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:
- Microsoft Graph [Directory.ReadWrite.All]
- Microsoft Graph [Team.Create]
- Microsoft Graph [Group.Create]
- Microsoft Graph [TeamMember.ReadWrite.Al]
- Office 365 SharePoint Online [Sites.FullControl.All]
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 "[email protected]" -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?
- [x] Windows
- [ ] Linux
- [ ] MacOS
- [x] Azure Cloud Shell
- [ ] Azure Functions
- [ ] Other : please specify
Could you try passing -ErrorAction:Stop as additional parameter and then logging more exception details with Get-PnPException | Select-Object * ?
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
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 ?
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?
@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 ?
I don't have managed identity set up so I can't test it.
@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:
Is there a way to make this work?
About the object reference error, can you please try with tomorrow's or later nightly builds and let us know ? Have refactored the code which also adds some new parameters , should help.
As for groupify, we checked it. Unfortunately, app-only isn't supported. It requires a delegated user access token, can't do anything about that.
Closing as no response.