我正在尝试编写一个脚本来检查成员列表是否属于天蓝色中的不同组,然后将列表导出到 Excel。我不确定 Azure 中是否有本地方法可以执行此操作,因为我是 Azure 新手,因此我一直在尝试通过 PowerShell 解决此问题。我只是在挣扎。
输出示例:
显示名称 | 第 1 组 | 第2组 | 第3组 |
---|---|---|---|
用户1 | 是的 | 是的 | 是的 |
用户2 | 是的 | 没有 | 是的 |
用户3 | 没有 | 是的 | 没有 |
用户4 | 是的 | 是的 | 是的 |
当然,您可以在 PowerShell 中执行此操作。
您也可以直接在 Excel 中使用 VBA 来完成此操作,但需要做更多的工作。 PowerShell 很合适,因为它拥有与 Azure / Entra 交互所需的所有 cmdlet。
您将需要 Az PowerShell 模块,因此请确保已安装该模块。
下面的示例将:
# Import Az module and connect to Azure
Import-Module Az
Connect-AzAccount
# Define lists of users and groups
$users = "User1", "User2", "User3"
$groups = "Group1", "Group2", "Group3"
# Create empty hashtable to store data
$data = @{}
# Build hashtable with default values (false)
foreach ($user in $users) {
$data[$user] = @{}
foreach ($group in $groups) {
$data[$user][$group] = $false
}
# Iterate each group and retrieve the members using the group name. Check if each user is present, for each group, and update the hashtable
foreach ($group in $groups) {
$groupMembers = Get-AzADGroupMember -GroupDisplayName $group
foreach ($user in $users) {
if ($groupMembers.DisplayName -contains $user) {
$data[$user][$group] = $true
}
else {
$data[$user][$group] = $false
}
}
# Convert hashtable to custom objects to get table header name in output
$objects = foreach ($user in $users) {
[PSCustomObject]$data[$user] | Select-Object @{Name="DisplayName"; Expression={$user}}, *
}
# Format the table
$objects | Format-Table -Property DisplayName, Group1, Group2, Group3
# We can also export this output to CSV file
$objects | Export-Csv C:\path\to\file.csv
您应该能够根据您的要求调整此脚本。