1

我在运行此脚本时遇到问题。我应该使用强身份验证要求进行调用,这意味着如果我没记错的话,将显示使用 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

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

4

1 回答 1

0

从您收到的错误中很明显,您应该纠正什么:

'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”,因为它为空。

为了摆脱这种情况,重要的是过滤掉ServicePrincipal成员Get-MsolRoleMember(例如,我有 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

从我在这里读到的内容看来,它似乎StrongAuthenticationRequirements适用于每用户 MFA。如果您的租户正在使用基于条件访问的 MFA,则该属性可能为空(在我的租户上检查)。所以我觉得StrongAuthenticationMethods更靠谱。


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

于 2019-10-10T06:48:18.097 回答