Die deutschsprachige Community für SharePoint, Microsoft 365, Teams, Yammer und mit Azure

SharePoint Online Powershell - Context.Web.EnsureUser() fails randomly

bewertet von 0 Usern
Dieser Beitrag hat 0 Antworten | 1 Follower

Beiträge 59
Simb55 erstellt: 14 Nov 2019 16:00

Hello there,

I'm writing a PowerShell script, that is supposed to create an Azure AD Security Group and to invite some guest users  to our tenant and add them to the group. Also, the data shall be written to different SharePoint Online lists.

Here is a screenshot of my code


- $context has been initialised successfully beforehand

- Connect-AzureAD has been executed successfully beforehand


If I run this in the context of the rest of my code, 80% of the time, it throws the following error:

"The User TestCompany cannot be found"

If I rerun only those few lines marked in yellow on the first screenshot after my script is finished, it works flawlessly and a SharePoint List Item is created.


Judging from the outcome, I would consider my script to be correct. What I do not understand is, why it fails when running in the context of the rest of the script. I thought it would maybe take some time for the group to be created, so I added the Start-Sleep for 60s, but that doesn't seem to be the case, since I can see the group created in Azure AD, as soon as the New-AzureADGroup cmdlet is executed.

Any help would be greatly appreciated.

Seite 1 von 1 (1 Elemente) | RSS