How to set scope after connecting to Microsoft Graph using non-interactive method?

Sampath 0 Reputation points
2024-10-03T20:55:14.6966667+00:00

I've successfully connected to Microsoft Graph using non-interactive method (Client Id, Client Secret), but I'm receiving Insufficient privileges error when running Get-Mguser due to not setting the scope. I have tried running the commands mentioned in other similar GitHub issues and forum questions, but none of them seem to work.

Is there any way to set the scope via PowerShell after connecting to Microsoft Graph using non-interactive method?

Get-MgUser : Insufficient privileges to complete the operation.
Status: 403 (Forbidden)
ErrorCode: Authorization_RequestDenied
Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $ClientSecretCredential 
Welcome to Microsoft Graph!
Connected via apponly access using 0afdcbe7-4b54-491c-8c86-15bf9a538b15
Readme: https://aka.ms/graph/sdk/powershell
SDK Docs: https://aka.ms/graph/sdk/powershell/docs
API Docs: https://aka.ms/graph/docs
NOTE: You can use the -NoWelcome parameter to suppress this message.
PS P:\> Get-MgContext
ClientId                                : 0afdcbe7-4b54-491c-8c86-15bf9a538b15
TenantId                                : 4880fa0f-81de-4d97-bcd6-0a32cda166f3
Scopes                                  :
AuthType                                : AppOnly
TokenCredentialType                     : ClientSecret
CertificateThumbprint                   :
CertificateSubjectName                  :            
SendCertificateChain                    : False
Account                                 :
AppName                                 : 
UserContextScope                        : Process
Certificate                             :
PSHostVersion                           : 5.1.19041.4894
ManagedIdentityId                       :
ClientSecret                            : System.Security.SecureString
Environment                             : Global
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,002 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,509 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,697 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andy David - MVP 147.9K Reputation points MVP
    2024-10-03T21:07:58.64+00:00

    Your connection should be setting the scope and the app needs to have the consented API perms or assigned the relevant role before you connect with the app.

    Guess Im confused maybe what the issue is?


  2. Hitesh Pachipulusu - MSFT 1,985 Reputation points Microsoft Vendor
    2024-10-04T06:28:22.3633333+00:00

    Hello Sampath,

    Thank you for reaching out to Microsoft Support!

    It looks like you’re encountering an issue with insufficient privileges due to missing permissions when using the Microsoft Graph PowerShell SDK with app-only authentication. Permissions must be specified before the initial connection.

    Here’s how you can specify the required permissions when connecting: Make sure that the application you registered in Azure AD has the necessary API permissions granted and admin consented for the scopes you are requesting.

    If you need to add or modify permissions, you can do so in the Azure portal:

    1. Go to Azure Active Directory > App registrations.
    2. Select your application.
    3. Under API permissions, select Microsoft Graph, Application permissions, add the required permissions. (User.Read.All). Refer to documentation.
    4. Click Grant admin consent for the permissions.

    After updating the permissions, try reconnecting to Graph.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    0 comments No comments

  3. Yakun Huang-MSFT 4,640 Reputation points Microsoft Vendor
    2024-10-04T08:31:03.0933333+00:00

    Hello Sampath,

    Thank you for reaching out to Microsoft Support!

    You need to add application permissions to your app in Azure, as shown below:

    User's image

    After connecting to the Graph via PowerShell, the contents of the scope parameter listed are the permissions that the application has:

    Screenshot 2024-10-04 082920

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.