使用 PowerShell 导出条件访问命名位置
Microsoft Entra 条件访问策略使用命名位置,它们非常棒!您可以在策略中排除或包含指定位置。但是,您想要一份命名位置报告并仔细查看它。在本文中,您将了解如何使用 PowerShell 导出条件访问命名位置。
在 Microsoft Entra 管理中心查找命名位置
要查找 Microsoft Entra ID 中的所有命名位置,请按照下列步骤操作:
- 登录到微软 Entra 管理中心
- 导航至保护 > 条件访问 > 命名位置
单击“命名位置”列表来查看设置了哪些 IP 地址或选择了哪些国家/地区需要花费时间并且不易查看。因此,最好使用 PowerShell 创建命名位置报告。
安装 Microsoft Graph PowerShell
在我们进一步继续并从 Microsoft Entra 租户获取所有命名位置之前,我们需要安装 Microsoft Graph PowerShell。
以管理员身份启动 Windows PowerShell 并运行以下命令。
Install-Module Microsoft.Graph -Force
重要的:在运行 cmdlet 或脚本之前,请务必更新到最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。
现在我们已经安装了 Microsoft Graph PowerShell SDK 模块,我们可以进入下一步。
连接到 Microsoft Graph PowerShell
您需要使用正确的权限连接到 Microsoft Graph PowerShell。
Connect-MgGraph -Scopes "Policy.Read.All"
您是否希望在没有用户交互的情况下进行连接,因为您希望脚本自动运行?使用基于证书的身份验证或客户端密钥进行设置。请阅读连接到 Microsoft Graph PowerShell 一文来了解更多信息。
要使用 PowerShell 获取条件访问中的命名位置,请使用获取 MgIdentityConditionalAccessNamedLocationcmdlet。
Get-MgIdentityConditionalAccessNamedLocation -All
输出显示命名位置。但是,它没有显示我们需要的重要属性。
Id CreatedDateTime DisplayName ModifiedDateTime
-- --------------- ----------- ----------------
0573aebb-3e9f-43c0-ba79-10b433d44dda 13/01/2025 09:05:51 Head Office 13/01/2025 09:05:51
0f3bbcc0-4920-42cd-8760-8bdc03980d5f 13/01/2025 09:06:47 Branch Office 13/01/2025 09:46:29
06af1569-60fd-4c54-85b0-9f2d59fc2803 13/01/2025 09:49:38 Trusted Location 13/01/2025 09:49:38
1837e1bf-ee5e-4cec-9a57-775ce18722c4 13/01/2025 09:55:59 NL 13/01/2025 10:35:28
1f9d9e0a-5195-4822-ba6b-d3b07ed54088 13/01/2025 10:35:56 USA + France 14/01/2025 09:20:41
我们缺少许多属性,因此我们将创建一个脚本来导出包含所有基本信息的精美报告。
使用 PowerShell 导出条件访问命名位置
Export-NamedLocations.ps1 PowerShell 脚本将获取 Microsoft Entra 租户中的所有条件访问命名位置及其关联策略。之后,它将报告导出到 CSV 文件。您可以使用 Microsoft Excel 或支持 CSV 文件扩展名的任何其他应用程序打开 CSV 文件。
对于每个指定位置,它都会收集以下信息:
- 显示名称
- 位置类型
- 值得信赖
- IP范围
- 国家
- 国家查找方法
- 包括未知国家和地区
- 创建日期
- 修改日期时间
- 政策
要使用 PowerShell 导出 Microsoft Entra ID 中的所有命名位置,请按照以下步骤操作:
步骤 1. 准备 Export-NamedLocations PowerShell 脚本
上创建两个文件夹(中:)驾驶:
- 温度
- 脚本
下载 Export-NamedLocations.ps1 PowerShell 脚本并将其放置在C:脚本文件夹。该脚本会将 JSON 文件导出到C:温度文件夹。
确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。
另一种选择是将以下代码复制并粘贴到记事本中。给它起个名字导出命名位置.ps1并将其放置在C:脚本文件夹。
<#
.SYNOPSIS
Export-NamedLocations.ps1
.DESCRIPTION
Export Conditional Access Named Locations policies to CSV file.
.LINK
www.alitajran.com/export-conditional-access-named-locations-powershell/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
X: x.com/alitajran
.CHANGELOG
V1.00, 01/13/2025 - Initial version
#>
param (
[string]$CSVPath = "C:tempNamedLocations.csv"
)
# Connect to Microsoft Graph with specific scopes
Connect-MgGraph -Scopes "Policy.Read.All" -NoWelcome
# Retrieve all Conditional Access Named Locations
$locations = Get-MgIdentityConditionalAccessNamedLocation -All | Select-Object Id, DisplayName, AdditionalProperties, CreatedDateTime, ModifiedDateTime
# Check if any Named Locations are found
if (-not $locations) {
Write-Host "No Named Locations found in Microsoft Entra ID." -ForegroundColor Cyan
exit
}
# Retrieve all Conditional Access Policies
$CAPolicy = Get-MgIdentityConditionalAccessPolicy -All | Select-Object Id, DisplayName, Conditions
# Initialize a new list to store report data
$Report = [System.Collections.Generic.List[Object]]::new()
# Get all specific cultures and store them in $cultures
$cultures = [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures)
# Create a dictionary for country code to full country name mapping
$countryNames = @{}
foreach ($culture in $cultures) {
$region = [System.Globalization.RegionInfo]::new($culture.Name)
if (-not $countryNames.ContainsKey($region.TwoLetterISORegionName)) {
$countryNames[$region.TwoLetterISORegionName] = $region.EnglishName
}
}
# Process the Named Locations
foreach ($location in $locations) {
# Determine the type of location (IP Ranges or Countries)
$locationType = if ($location.AdditionalProperties.ipRanges) { "IP Ranges" } else { "Countries" }
# Prepare list to hold country names
$countries = [System.Collections.Generic.List[string]]::new()
foreach ($countryCode in $location.AdditionalProperties.countriesAndRegions) {
if ($countryNames.ContainsKey($countryCode)) {
$countries.Add($countryNames[$countryCode])
}
else {
$countries.Add($countryCode)
}
}
# Translate country lookup method to a more descriptive string
$countryLookupMethod = switch ($location.AdditionalProperties.countryLookupMethod) {
"authenticatorAppGps" { "Determine location by GPS coordinates" }
"clientIpAddress" { "Determine location by IP address (IPv4 and IPv6)" }
default { $location.AdditionalProperties.countryLookupMethod }
}
# Check which policies include or exclude this Named Location
$policies = [System.Collections.Generic.List[string]]::new()
foreach ($Policy in $CAPolicy) {
$InclLocation = $Policy.Conditions.Locations.IncludeLocations
$ExclLocation = $Policy.Conditions.Locations.ExcludeLocations
if ($InclLocation -contains $location.Id -or $ExclLocation -contains $location.Id) {
$policies.Add($Policy.DisplayName)
}
}
# Create a custom object for each Named Location with all relevant details including policy names
$ReportLine = [PSCustomObject][Ordered]@{
DisplayName = $location.DisplayName
LocationType = $locationType
IsTrusted = $location.AdditionalProperties.isTrusted
IPranges = ($location.AdditionalProperties.ipRanges.cidrAddress -join ',')
Countries = ($countries -join ',')
CountryLookupMethod = $countryLookupMethod
IncludeUnknownCountriesAndRegions = $location.AdditionalProperties.includeUnknownCountriesAndRegions
CreatedDate = $location.CreatedDateTime
ModifiedDateTime = $location.ModifiedDateTime
Policy = ($policies -join ',')
}
$Report.Add($ReportLine)
}
# Sort the report by the DisplayName
$SortedReport = $Report | Sort-Object DisplayName
# Display report in a grid view
$SortedReport | Out-GridView -Title "Conditional Access Named Locations"
# Export the sorted report to a CSV file
$SortedReport | Export-Csv -Path $CSVPath -NoTypeInformation -Encoding utf8
Write-Host "File exported to: $CSVPath" -ForegroundColor Cyan
这就是它的样子。
步骤 2. 运行 Export-NamedLocations PowerShell 脚本
运行导出命名位置.ps1用于获取所有条件访问命名位置并将其导出到临时文件夹中的 CSV 文件的 PowerShell 脚本。
有关的:如何使用 PowerShell 导出 MS Teams 聊天历史记录
C:scripts.Export-NamedLocations.ps1 -CSVPath "C:tempNamedLocations.csv"
报告输出将发送到单独窗口 (Out-GridView) 中的交互式表格。
步骤 3. 检查指定位置 CSV 文件报告
这导出命名位置.ps1PowerShell 脚本会将所有命名位置导出到 CSV 文件。在路径中找到CSV文件C:温度。
打开命名位置.csv包含您最喜欢的应用程序的 CSV 文件。例如,微软 Excel。
条件访问命名位置报告看起来非常好。
就是这样!
结论
您了解了如何使用 PowerShell 导出 Microsoft Entra ID 中的条件访问命名位置。首先,连接到 Microsoft Graph PowerShell 并运行 Export-NamedLocations.ps1 PowerShell 脚本。之后,所有命名位置属性都可以在 CSV 文件中使用。
您喜欢这篇文章吗?您可能还喜欢如何配置 Microsoft Entra 多重身份验证。不要忘记关注我们并分享这篇文章。
