How can I prevent getting prompted multiple times when executing Microsoft Graph Powershell script ?

EnterpriseArchitect 5,156 Reputation points
2023-04-18T15:21:45.7966667+00:00

How do I avoid being prompted multiple times when running the Microsoft Graph Powershell script?

Connect-MgGraph -Scopes Reports.Read.All, User.Read.All, UserAuthenticationMethod.Read.All
Select-MgProfile beta

Get-MgReportAuthenticationMethodUserRegistrationDetail -Filter "IsMfaRegistered eq false and IsAdmin eq true"

With the above script, there are 40+ users returned; hence I was prompted 40 times to login with the MFA. Any assistance would be greatly appreciated.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,461 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
{count} votes

Accepted answer
  1. Andy David - MVP 145.6K Reputation points MVP
    2023-05-12T11:26:12.5866667+00:00

    Try the solution here:

    https://github.com/microsoftgraph/msgraph-sdk-powershell#known-issues

    • Customers upgrading from previous versions of the SDK may encounter auth prompts on every command call. If this happens, one can use the following steps to reset their token cache:
    • Use Disconnect-MgGraph to sign out of the current session.
    • Run Remove-Item "$env:USERPROFILE\.graph" -Recurse -Force to delete your token cache.
    • Run Connect-MgGraph to reconstruct a clean token cache.
    7 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 44,121 Reputation points
    2023-04-20T07:49:51.69+00:00

    Hello there, You can use an unattended daemon, which requires connecting to MS graph using the application's client id and certificate name. Connect-MgGraph -ClientID YOUR_APP_ID -TenantId YOUR_TENANT_ID -CertificateName YOUR_CERT_SUBJECT
    If Similar discussion here https://learn.microsoft.com/en-us/answers/questions/876524/script-to-connect-to-ms-graph-without-prompting-fo Hope this resolves your Query !! --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments