Get-MgUserAuthenticationMethod Command doesn't return any information

Miranda-5426 20 Reputation points
2024-05-09T17:27:25.7166667+00:00

I am trying to build a PowerShell script we can use to run hourly that would basically look for users who have recently signed up for Multifactor Authenticator or have Authenticator setup on their account in general and then add them to the group that controls conditional access if they aren't in there already.

I was trying to use the Get-MgUserAuthenticationMethod -UserId 'UPN' | fl command, and when I run it, it doesn't return anything.

User's image

We have an enterprise app setup and configured with the following permissions, user.read, user.read.all, and userauthenticationmethod.readwrite.all. In order to connect to graph using the enterprise app, we have a certificate that we use to authenticate.

User's image

User's image

Does anyone have any ideas on what may be going on or have any suggestions on what I could do instead to accomplish what I mentioned above? I don't have a ton of PowerShell experience, so any suggestions or input is appreciated.

Microsoft Authenticator
Microsoft Authenticator
A Microsoft app for iOS and Android devices that enables authentication with two-factor verification, phone sign-in, and code generation.
5,619 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,774 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,134 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vasil Michev 96,516 Reputation points MVP
    2024-05-10T08:10:16.7533333+00:00

    You are connecting via the client credentials flow, but using delegate permissions - those do not work for said flow. Either use application permissions instead, or authenticate in the context of a user to leverage the delegate permissions.

    Also, if you are planning to cover methods for privileged users, your app (its service principal) needs to have the Privileged Authentication Admin or Global admin role assigned.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points
    2024-05-09T18:19:17.89+00:00

    As a first pass at debugging, try assigning the result to a variable and see if the Get-MgUserAuthenticationMethod actually returned anything:

    $x = Get-MgUserAuthenticationMethod -UserId 'UPN'
    $x.gettype()
    

    If you get this: You cannot call a method on a null-valued expression. as a result the cmdlet didn't find the UserID.