Hello john,
Welcome to Q&A, this error is telling you that the resource with the ID 1***1 can't be found or something it's referencing doesn't exist. Here's what's probably going on:
- Make sure that the "Office 365 SharePoint Online" service principal exists and includes the Sites.Selected role. If the AppRoles property doesn't have that role, you'll need to add it.
- Confirm that your Azure Function's managed identity and the Office 365 SharePoint Online service principal are in the same tenant. Cross-tenant scenarios can get tricky.
- Be sure you're using the latest version of the Microsoft Graph PowerShell module—it just makes everything smoother. You can test directly from azure cloud shell:
Get-MgServicePrincipal -Filter "displayName eq 'Azure Function Name'"
It's basically a matter of double-checking your setup and permissions. If everything looks right you can run the powershell command again but step by step to identify where is your issue.
$managedIdentityObjectId = "1***1"
$scopeName = "Sites.Selected"
# Get the service principal for Office 365 SharePoint Online
$resourceAppPrincipalObj = Get-MgServicePrincipal -Filter "displayName eq 'Office 365 SharePoint Online'"
# Find the app role matching the scope name
$targetAppPrincipalAppRole = $resourceAppPrincipalObj.AppRoles | Where-Object { $_.Value -eq $scopeName }
# Assign the app role directly
New-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $managedIdentityObjectId -AppRoleId $targetAppPrincipalAppRole.Id -ResourceId $resourceAppPrincipalObj.Id | Format-List
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
Regards,
Luis