问题描述
你好堆栈溢出,
我遇到了以下问题。 我正在尝试创建一个脚本,该脚本将检查 O365 租户上存在哪些管理员帐户并自动为他们启用 MFA,以便他们下次登录时会提示设置 MFA。
代码如下:
$mfa1 = Get-Msoluser | Select-Object UserPrincipalName,StrongAuthenticationMethods,StrongAuthenticationRequirements | Where-object {$_.UserPrincipalName -notin $exclude }
foreach ($item in $mfa1) {
if ($null -ne $item.StrongAuthenticationMethods){
$st = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement
$st.RelyingParty = "*"
$st.State = "Enable"
$sta = @($st)
Set-Msoluser -UserPrincipalName $item.UserPrincipalName -StrongAuthenticationRequirements $sta
Write-Host "test1"
}
else {
Write-Host "test2"
}
}
让我知道我错在哪里,我已经在几乎所有的互联网上搜索了解决方案,而无需从 CSV 上传用户
提前致谢!
解决方法
您似乎遇到了一些问题,但下面基于您的代码的代码非常适合我。
为了快速测试,我指定了一个用户来完成这个过程:
$mfa1 = Get-MsolUser | Select-Object UserPrincipalName,StrongAuthenticationMethods,StrongAuthenticationRequirements | Where-object {$_.UserPrincipalName -eq '<User Principal Name>' }
foreach ($item in $mfa1) {
#if there is no StrongAuthenticationMethods,enable MFA
if ($item.StrongAuthenticationMethods.Count -eq 0){
$st = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement
$st.RelyingParty = "*"
#here is the issue that you can't set MFA successfully,the value should be "Enabled"
$st.State = "Enabled"
$sta = @($st)
Set-MsolUser -UserPrincipalName $item.UserPrincipalName -StrongAuthenticationRequirements $sta
Write-Host "test1"
}
else {
Write-Host "test2"
}
}
当用户启用 MFA 并设置 MFA 方法时:
当用户没有 MFA 方法时:
如果您有任何其他问题,请告诉我。