当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何导入条件访问策略

作者:精品下载站 日期:2024-12-14 06:31:10 浏览:14 分类:玩电脑

如何导入条件访问策略


我们希望将条件访问策略导入到 Microsoft Entra 租户中。现在,我们可以使用 Microsoft Entra 管理中心恢复条件访问策略。但这是单个条件访问策略上传。如果您想要导入多个条件访问策略怎么办? PowerShell 是最简单、最快的方法。在本文中,你将了解如何使用 Microsoft Graph PowerShell 将条件访问策略从 JSON 文件导入到 Microsoft Entra 中。

在你开始之前

您需要在 JSON 文件中包含条件访问策略,因为脚本将遍历 JSON 文件并将它们导入到 Microsoft Entra 租户中。

如果您已经有了 JSON 文件,那么就可以开始了。如果没有,请阅读有关如何导出条件访问策略的更多信息。

安装 Microsoft Graph PowerShell

在我们继续下一步并将所有条件访问策略恢复到 Microsoft Entra 租户之前,我们需要安装 Microsoft Graph PowerShell。

以管理员身份启动 Windows PowerShell 并运行以下命令。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

重要提示:始终安装 Microsoft Graph PowerShellMicrosoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

现在我们已经安装了 Microsoft Graph PowerShell SDK 模块,我们可以进入下一步。

准备 Import-CAPolicies PowerShell 脚本

(C:)驱动器上创建两个文件夹:

  • Temp
  • Scripts

下载 Import-CAPolicies.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。该脚本会将 C:\temp 文件夹中存在的 JSON 文件导入到 Microsoft Entra 租户中。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Import-CAPolicies.ps1 并将其放置在 C:\scripts 文件夹中。

<#
    .SYNOPSIS
    Import-CAPolicies.ps1

    .DESCRIPTION
    Import Conditional Access policies from JSON files for restore purposes.

    .LINK
    www.a-d.site/import-conditional-access-policies/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 11/19/2023 - Initial version
#>

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Policy.Read.All", "Policy.ReadWrite.ConditionalAccess", "Application.Read.All"

# Define the path to the directory containing your JSON files
$jsonFilesDirectory = "C:\temp\"

# Get all JSON files in the directory
$jsonFiles = Get-ChildItem -Path $jsonFilesDirectory -Filter *.json

# Check if there are no JSON files
if ($jsonFiles.Count -eq 0) {
    Write-Host "No JSON files found in the directory to import." -ForegroundColor Yellow
}
else {
    # Loop through each JSON file
    foreach ($jsonFile in $jsonFiles) {
        try {
            # Read the content of the JSON file and convert it to a PowerShell object
            $policyJson = Get-Content -Path $jsonFile.FullName | ConvertFrom-Json

            # Create a custom object
            $policyObject = [PSCustomObject]@{
                displayName     = $policyJson.displayName
                conditions      = $policyJson.conditions
                grantControls   = $policyJson.grantControls
                sessionControls = $policyJson.sessionControls
                state           = $policyJson.state
            }

            # Convert the custom object to JSON with a depth of 10
            $policyJsonString = $policyObject | ConvertTo-Json -Depth 10

            # Create the Conditional Access policy using the Microsoft Graph API
            $null = New-MgIdentityConditionalAccessPolicy -Body $policyJsonString
        
            # Print a success message
            Write-Host "Policy created successfully: $($policyJson.displayName) " -ForegroundColor Green
        }
        catch {
            # Print an error message if an exception occurs
            Write-Host "An error occurred while creating the policy: $_" -ForegroundColor Red
        }
    }
}

这就是它的样子。

[玩转系统] 如何导入条件访问策略

准备条件访问 JSON 文件

确保所有 CA 策略 JSON 文件都存在于 C:\temp 中。

[玩转系统] 如何导入条件访问策略

使用 PowerShell 删除所有条件访问策略

我们的 Microsoft Entra 租户中有许多可用的 CA 策略,我们希望删除它们。如果您想保留已存在的策略,或者已经为空,则可以跳过此步骤。

让我们使用 Remove-CAPolicies.ps1 PowerShell 脚本删除所有 CA 策略。

<#
    .SYNOPSIS
    Remove-CAPolicies.ps1

    .DESCRIPTION
    Remove all Conditional Access policies from Microsoft Entra.

    .LINK
    www.a-d.site/import-conditional-access-policies/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.a-d.site
    LinkedIn:   linkedin.com/in/a-d

    .CHANGELOG
    V1.00, 11/19/2023 - Initial version
#>

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Policy.Read.All", "Policy.ReadWrite.ConditionalAccess", "Application.Read.All"

# Get all policies
$policies = Get-MgIdentityConditionalAccessPolicy -All

# Check if there are any policiesX
if ($policies.Count -eq 0) {
    Write-Host "There are no Conditional Access policies to delete." -ForegroundColor Yellow
}
else {
    # Loop through each policy and delete it
    foreach ($policy in $policies) {
        Remove-MgIdentityConditionalAccessPolicy -ConditionalAccessPolicyId $policy.Id
        Write-Host "Policy deleted successfully: $($policy.DisplayName)" -ForegroundColor Green
    }
}

现在 Microsoft Entra 租户中不再有条件访问策略,让我们通过导入 JSON 文件来添加 CA 策略。

运行导入条件访问策略 PowerShell 脚本

运行 Import-CAPolicies.ps1 PowerShell 脚本以获取 C:\temp 文件夹中的所有条件访问 JSON 策略并将它们导入到 Microsoft Entra 租户。

C:\scripts\Import-CAPolicies.ps1

注意:如果您在 Microsoft Entra 中已有 CA 策略,它不会覆盖它们或检查该策略是否存在。它将添加具有相同名称的策略,您可以在查看创建日期时识别它们。创建日期。您可以删除或禁用已存在的策略。

输出显示:

Policy created successfully: Block legacy authentication
Policy created successfully: Require multifactor authentication for all users

验证条件访问策略

检查是否已使用 PowerShell 成功导入条件访问策略。运行 Get-MgIdentityConditionalAccessPolicy cmdlet。

Get-MgIdentityConditionalAccessPolicy -All

这就是我们示例中的样子。

Id                                   CreatedDateTime       Description DisplayName                                      ModifiedDateTime State
--                                   ---------------       ----------- -----------                                      ---------------- -----
a800fccb-1912-45b6-ad7b-634e60a33c6c 11/19/2023 5:14:01 PM             Block legacy authentication                                       disabled
6bfb32e1-92cf-40fb-8fce-0143bcb39584 11/19/2023 5:14:02 PM             Require multifactor authentication for all users                  enabled

验证条件访问策略是否已成功导入的另一种方法是使用 Microsoft Entra 管理中心:

  1. 登录 Microsoft Entra 管理中心

  2. 展开身份 > 保护

  3. 点击条件访问

  4. 选择查看所有政策

在我们的示例中,有两个 CA 策略。其中一项策略已启用,另一项策略已禁用。

[玩转系统] 如何导入条件访问策略

一切看起来都很棒!

这是否有助于您将条件访问策略从 JSON 文件恢复到 Microsoft Entra 租户?

了解更多:在 Microsoft Entra 和 PowerShell 中获取 MFA 状态 »

结论

您学习了如何使用 PowerShell 导入条件访问策略。如果您有多个 CA 策略,则使用 PowerShell 导入 CA 策略会更快。首先,连接到 Microsoft Graph PowerShell 并运行 Import-CAPolicies PowerShell 脚本。之后,所有条件访问策略及其配置都可在 Microsoft Entra 中使用。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 将 Azure AD 用户导出为 CSV。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯