Reading the wonderful series on Azure Multi-Factor Authentication (MFA) by Sander Berkouwer gave me the idea of sharing a PowerShell function that allows you to enable this feature for a single user or multiple users. The ability to automate enabling MFA is very powerful for configuring all users the same way. Not having to do this through the GUI also saves valuable time.

Graham Beer

Graham is an experienced system engineer with a passion for PowerShell and automation. You can follow his blog or reach him on Twitter at @GKBeer.

To connect to Azure Active Directory to manage our users, install the PowerShell module MSOnline. If you have PowerShell version 5, you can use the PowerShell Gallery to install the module:

After installing the module, you need to make a connection to your Azure Active Directory. First capture your credentials like so:

The credential username is my administrator account on my Office 365 trial account. Microsoft lets you create an Office Business 365 account for a free one-month trial. It's really useful if you wish to create a development area to test.

With your captured credentials, use the Connect-MsolService cmdlet to initiate a connection to Azure Active Directory:

A successful connection won't give you a confirmation message. You can test it by performing a simple user search:

When enabling MFA, you will find (or not!) there isn't a cmdlet for this. You will need to use PowerShell's New-Object cmdlet to create an instance of the Microsoft.Online.Administration.StrongAuthenticationRequirement object:

Let's examine what properties we have to work with. Using Get-Member on $mfaobject shows us this:

GetMember properties of an mfaobject

GetMember properties of an mfaobject

The two properties we need to populate are RelyingParty and State. For the RelyingParty property, we can set this to "*".

We now have to set the State. Microsoft says, "A user's state reflects whether an admin has enrolled them in Azure MFA, and whether they completed the registration process."

So what options do we have? Again, I'll refer to the Microsoft documentation:

Disabled = The default state for a new user not enrolled in Azure MFA.

Enabled = The user has been enrolled in Azure MFA, but has not registered. They receive a prompt to register the next time they sign in.

Enforced = The user has been enrolled and has completed the registration process for Azure MFA.

So we have three options to choose from. In this example, we will set our user to Enabled:

We capture our MFA authentication settings in a new variable, cast as an array.

So we can go ahead and set this on the user. Let's make this process slightly easier. From what we have talked about, we know the properties we need to set to get our MFA template. We can do this in a line of code. The -Property parameter on New-Object allows us to use a hash table for the property names and their values. This is how it looks:

I like this approach since it is much cleaner. If you want to see what you have set, type in the variable $MFASetting. This is what you will see:

Configured settings of the variable MFASetting

Configured settings of the variable MFASetting

Now that we have our MFA template, we are ready to apply it to our user. We achieve this via the Set-MsolUser cmdlet. Pass the UserPrincipalName and add the StrongAuthenticationRequirements parameter with our Multifactor authentication template variable.

To confirm we've applied this option to our user, we can expand the StrongAuthenticationRequirements settings we just populated and look at the State property:

We can confirm the system has set this value.

It's very easy to upscale this approach to more than a single user. I've created the function below, which takes a list of User Principal Name (UPN) addresses and allows the Enabled and Enforced state options. The function will apply the settings to the user and perform a check at the end to confirm it's made the changes.

Within the code you might spot the -ReadCount setting on the Get-Content cmdlet. When processing large files, you should use the -ReadCount parameter to specify a block size of -1. Doing so will cause it to load the entire file and process it at once, which is much faster.

To use this function, you pipe the file to it:

Or by:

Join the 4sysops PowerShell group!

3+

Users who have LIKED this post:

  • avatar
Share
4 Comments
  1. Marcus Abarbanel 2 years ago

    Great article.  We're working with MFA in Azure now, but we're performing it through the GUI.  I also found a diamond in the rough with the -readcount tidbit you gave us in the last paragraph.  I'll use that going forward for sure.

    0

  2. Graham Beer 2 years ago

    Thank you Marcus. Glad it helped.

    0

  3. Sander Berkouwer 2 years ago

    Awesome article, Graham! 🙂

    1+

  4. John Langston 1 year ago

    I like it because it tells me more than what I knew.  I am clearly missing something though or a little slow.  I am looking for a way to navigate MFA executing a scheduled PS script in which I copy databases (400+) and then creates accounts/groups and assigns permission in the dark of night.  The creation of accounts/groups and their permissions has to be done in Azure under the context of an AAD account.  Having to interactively enter information in order for the script to run defeats scheduling something in the wee hours.

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account