【问题标题】:Script Powershell for MFA call用于 MFA 调用的脚本 Powershell
【发布时间】:2019-10-10 01:48:25
【问题描述】:

我在运行此脚本时遇到问题。我应该使用强身份验证要求进行调用,这意味着如果我没记错的话,将显示使用 MFA 门户启用 MFA 的用户。

Connect-MsolService
$role = getMsolRole -rolename "Company Administrator"
$rm = get-MsolRoleMember -RoleObjectId $role.ObjectId

foreach ($c in $rm)

{


Get-MsolUser -UserPrincipalName $c.EmailAddress | Select DisplayName, UserPrincipalName, @{N="MFA Status"; E={ if($_.StrongAuthenticationRequirements.Count -ne 0) { $_.StrongAuthenticationRequirements.State.toString() } else 'Disabled' }}

错误是

At line:9 char:225
+ ... { $_.StrongAuthenticationRequirements.State.toString() } else 'Disabl ...
+                                                                  ~
Missing statement block after 'else' keyword.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingStatementBlockAfterElse

编辑:

如果您甚至可以阐明使用 StrongAuthenticationMethods 调用 MFA 和使用 StrongAuthenticationRequirements 之间的区别,那就太好了。所以我可以重现这段代码。

我们的 MFA 审核代码的问题在于,它显示系统管理员禁用了 MFA,即使他们声称自己已启用。

这是返回带有通配符管理员的管理员被禁用的审核代码。

Function Get-O365AdminMFAStatus{
    $AdminData=@()
    $objRole=@()
    $Domain = $(get-addomain).dnsroot
    $Log = "C:\temp\Audit\$Domain O365 Admin MFA Status $(get-date -f yyyy-MM-dd).csv"

    try{
        $Roles = Get-MsolRole | where {$_.name -LIKE "*Administrator*"}
        $Roles = ($Roles).name

        foreach ($Role in $Roles){
            $Members = Get-MsolRoleMember -RoleObjectId (Get-MsolRole -RoleName $Role).ObjectId 
            foreach ($Member in $Members){
                $MsUser = $Member | Get-MsolUser
                if($MsUser.StrongAuthenticationMethods.Count -eq 0) {
                    $Enabled = "False"
                    write-host $Role - $Member.DisplayName "No MFA enabled" -foregroundcolor red
                }
                Else{
                    $Enabled = "True"
                    write-host $Role - $Member.DisplayName "MFA enabled" -foregroundcolor green
                }   

                Try{
                    $Exist = [bool](Get-mailbox $MsUser.UserPrincipalName -erroraction SilentlyContinue)
                    if ($Exist){
                        $MBStats = Get-MailboxStatistics $MsUser.UserPrincipalName
                        $LastLogon = $MBstats.LastLogonTime
                    }
                    Else{
                        $LastLogon = "N/A"
                    }
                }
                Catch{
                    $LastLogon = "N/A"
                }

                $objRole = New-Object -TypeName PSObject
                $objRole | Add-Member -MemberType NoteProperty -Name "Role Name" -Value $Role
                $objRole | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $Member.DisplayName
                $objRole | Add-Member -MemberType NoteProperty -Name "UPN" -Value $Member.UserPrincipalName
                $objRole | Add-Member -MemberType NoteProperty -Name "Licensed" -Value $Member.IsLicensed
                $objRole | Add-Member -MemberType NoteProperty -Name "Last Logon" -Value $LastLogon
                $objRole | Add-Member -MemberType NoteProperty -Name "MFA Enabled?" -Value $Enabled

                $AdminData += $objRole
            }
        }

        $AdminData | Export-Csv -NoTypeInformation $Log 
        write-host ""
        write-host "CSV Export Complete to $Log" -foregroundcolor yellow
    }
    Catch{
        Write-host "There was an error: $($_.Exception.Message)"
    }
}

Get-O365AdminMFAStatus

无论如何,如果您对我将编辑的问题进行澄清。

【问题讨论】:

标签: powershell


【解决方案1】:

从您收到的错误中可以看出,您应该更正什么:

“else”关键字后缺少语句块

else 后面缺少大括号,所以应该是:

else {'Disabled'}

我根据我的帐户检查了您的 cmdlet(我启用了 2FA),StrongAuthenticationRequirements 对我来说是空对象(检查了许多帐户 - 尝试在下面澄清)。我认为您应该改用 StrongAuthenticationMethods 属性。它包含有关为 2FA 配置的通道的信息。

最后,您的代码将如下所示:

foreach ($c in $rm) {
  Get-MsolUser -UserPrincipalName $c.EmailAddress | Select DisplayName, UserPrincipalName,
  @{N="MFA Status"; E={ if($_.StrongAuthenticationMethods.Count -ne 0) { "$($_.StrongAuthenticationMethods.Count) methods found" } else {'Disabled'} }}
}

但您可能会注意到某些条目与此类信息错误:

Get-MsolUser : 无法将参数绑定到参数“UserPrincipalName”,因为它为空。

要摆脱这种情况,重要的是从Get-MsolRoleMember 中过滤掉ServicePrincipal 成员(例如,我有RMS 和PowerBI 信息服务,你可能没有或不同):

foreach ($c in $rm | Where-Object {$_.rolemembertype -eq 'user'}) {
  Get-MsolUser -UserPrincipalName $c.EmailAddress | Select DisplayName, UserPrincipalName,
  @{N="MFA Status"; E={ if($_.StrongAuthenticationMethods.Count -ne 0) { "$($_.StrongAuthenticationMethods.Count) methods found" } else {'Disabled'} }}
}

关于StrongAuthenticationMethodsStrongAuthenticationRequirements的说明

从我阅读的here 看来,StrongAuthenticationRequirements 似乎适用于每用户 MFA。如果您的租户正在使用基于条件访问的 MFA,则该属性可能为空(在我的租户上检查)。所以我猜StrongAuthenticationMethods更可靠。


注意:我还测试了您发布的部分长代码,它对我来说可以正常工作。你在getMsolRole 中有错字 - 应该是Get-MsolRole

【讨论】:

  • 感谢您的回复。因此,在启用 MFA 的情况下,长审核脚本适用于您返回的管理员*。即使管理员说他们拥有它,我的仍然说没有 MFA。这就是让我困惑的地方。所以我必须区分 StrongAuthentication 方法和需求。
  • 当您为它们检查 Get-MsolUser 时,您可以做的是检查这些属性的值是什么。
猜你喜欢
  • 2021-09-15
  • 2022-07-07
  • 2015-07-07
  • 1970-01-01
  • 2018-08-12
  • 2014-02-28
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多