对公司管理员 Powershell 执行 MFA

MFA Enforcement to Company Administrators Powershell

我工作的公司有审计。我只需要代码来查看 Powershell 中的公司管理员组检查并验证他们是否通过 MFA 身份验证强制执行,或者更确切地说是强制执行他们的状态。上网查了下代码的点点滴滴。如果你们能帮助我作为 It Security 工作的代码并且 Powershell 编码不是其中的一部分,那么非常感谢 Powershell 编码

Connect-MsolService
#I think this will get company admins
$role = Get-MsolRole -rolename "Company Administrator"

$rm = Get-MsolRoleMember -roleObjectId $role.ObjectId

#not sure what this code is for

foreach ($c in $rm)
{

Get-MsolUser -UserPrincipalName $c.EmailAddress | Select displayname

} 

输出将是包含名称的显示名称 UserPrincipalName 将是公司管理员的电子邮件地址 并且 MFA 状态输出将被执行

这是另一个代码

$role = Get-MsolRole -rolename "Company Administrator"
Get-MsolRoleMember -RoleOBjectId $role.ObjectId

输出将在广告中显示 Rolemember 类型的电子邮件地址 Displayname 如果用户获得许可 = true 或 false

谢谢,如果有人会回复这个

我自己无法测试,所以请先在一组测试用户中试用:

# first, get the credentials for a user that is allowed to do this
$cred = Get-Credential
Import-Module MSOnline
Import-Module ActiveDirectory
Connect-MsolService –Credential $cred

# set up a StrongAuthenticationRequirement object with the state you want the users in
$requirement = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement
$requirement.RememberDevicesNotIssuedBefore = (Get-Date)
$requirement.RelyingParty = "*"
$requirement.State        = "Enforced"

# get the members of the group (users only)
Get-ADGroupMember -Identity 'Company Administrators' | Where-Object { $_.objectClass -eq 'user' } | ForEach-Object { 
    # get the UserPrincipalName for this user
    $upn = Get-ADUser $_.SamAccountName | Select-Object -ExpandProperty UserPrincipalName
    $mfa = Get-MsolUser -UserPrincipalName $upn | Select-Object -ExpandProperty StrongAuthenticationRequirements
    if ($mfa.Count -eq 0 -or $mfa[0].State -ne 'Enforced') {
        Write-Host "Enforcing MFA for user $upn"
        Set-MsolUser -UserPrincipalName $upn -StrongAuthenticationRequirements @($requirement)
    }
    else {
        Write-Host "MFA is already enforced for user $upn"
    }
}

使用 Get-MsolRoleGet-MsolRoleMember

的替代代码
# first, get the credentials for a user that is allowed to do this
$cred = Get-Credential
Import-Module MSOnline
Connect-MsolService –Credential $cred

# set up a StrongAuthenticationRequirement object with the state you want the users in
$requirement = New-Object -TypeName Microsoft.Online.Administration.StrongAuthenticationRequirement
$requirement.RememberDevicesNotIssuedBefore = (Get-Date)
$requirement.RelyingParty = "*"
$requirement.State        = "Enforced"

# get a list of administrator roles (possibly only one role object is returned)
$roles = Get-MsolRole -RoleName "Company Administrators"
foreach ($role in $roles) {
    # get the list of members for this role and loop through
    Get-MsolRoleMember -RoleObjectId $role.ObjectId | ForEach-Object {
        $mfa = Get-MsolUser -ObjectId $_.ObjectId | Select-Object -ExpandProperty StrongAuthenticationRequirements
        if ($mfa.Count -eq 0 -or $mfa[0].State -ne 'Enforced') {
            Write-Host "Enforcing MFA for user $($_.DisplayName)"
            Set-MsolUser -ObjectId $_.ObjectId -StrongAuthenticationRequirements @($requirement)
        }
        else {
            Write-Host "MFA is already enforced for user $($_.DisplayName)"
        }
    }
}


更新

如果您真正需要的只是 "Company Administrators" 组成员及其 MFA ststus 的报告,代码可以简单得多:

# first, get the credentials for a user that is allowed to do this
$cred = Get-Credential
Import-Module MSOnline
Connect-MsolService –Credential $cred

# get a list of administrator roles (possibly only one role object is returned)
$roles = Get-MsolRole -RoleName "Company Administrators"
$result = foreach ($role in $roles) {
    # get the list of members for this role and loop through
    Get-MsolRoleMember -RoleObjectId $role.ObjectId | ForEach-Object {
        $mfa = Get-MsolUser -ObjectId $_.ObjectId | Select-Object -ExpandProperty StrongAuthenticationRequirements
        if ($mfa.Count -eq 0) { $status = 'Disabled' } else { $status = $mfa[0].State }
        # output an object to be collected in variable $result
        [PsCustomObject]@{
            'UserName'     = $_.DisplayName
            'EmailAddress' = $_.EmailAddress
            'MFA_Status'   = $status
        }
    }
}

# display on screen
$result | Format-Table -AutoSize

#output to a CSV file
$result | Export-Csv -Path 'X:\CompanyAdministrators.csv' -NoTypeInformation -Force