forked from ruudmens/LazyAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMFAEnableForUser.ps1
74 lines (63 loc) · 1.88 KB
/
MFAEnableForUser.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Function Set-MFAforUser {
<#
.Synopsis
Enables MFA for an Office 365 User
.DESCRIPTION
Enable MFA for an user, you can turn it on for a single user or input a list of users
.NOTES
Name: Set-MFAforUser
Author: R. Mens - LazyAdmin.nl
Version: 1.0
DateCreated: jan 2021
Purpose/Change: Initial script development
.LINK
https://lazyadmin.nl
.EXAMPLE
Set-MFAforUser -UserPrincipalName [email protected]
Enable MFA for the user John Doe
.EXAMPLE
Import-Csv -Delimiter ";" -Path ("path\to\file\users-to-enable.csv") | Foreach-Object { Set-MFAforUser $_.UserPrincipalName }
Enable MFA for all users in a CSV file
#>
[CmdletBinding(DefaultParameterSetName="Default")]
param(
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ParameterSetName = "UserPrincipalName",
Position = 0
)]
# Enter a single UserPrincipalName or a comma separted list of UserPrincipalNames
[string[]]$UserPrincipalName
)
Begin {
# Src: https://docs.microsoft.com/en-us/azure/active-directory/authentication/howto-mfa-userstates
$sa = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement
$sa.RelyingParty = "*"
$sa.State = "Enabled"
$sar = @($sa)
}
Process {
if ($PSBoundParameters.ContainsKey('UserPrincipalName')) {
foreach ($user in $UserPrincipalName) {
try {
# Change the following UserPrincipalName to the user you wish to change state
Set-MsolUser -UserPrincipalName $user -StrongAuthenticationRequirements $sar -ErrorAction Stop
[PSCustomObject]@{
UserPrincipalName = $user
MFAEnabled = $true
}
}
catch {
[PSCustomObject]@{
UserPrincipalName = $user
MFAEnabled = $false
}
}
}
}else{
Write-Verbose "No UserPrincipalName given"
}
}
}