我已经尝试了一段时间,让我的 $Tenant.Name 变量作为文本插入,而不是作为稍后获取的变量。
目前我收到以下输出:
租户 1 - AADRoles | 租户 1 - 组 | 租户 2 - AADRoles | 租户 2 - 组 |
---|---|---|---|
租户2管理员 | 租户 2 管理员组 | 租户2管理员 | 租户 2 管理员组 |
两个租户都从租户 2 获取角色和组,我猜这是因为计算属性直到最后才计算,然后它将使用最后一个值。我这样做只是一个白痴吗?
我想要实现的是找出来自 MainTenantUsers 的用户在我们的其他租户中拥有哪些权限和角色。我以为我会通过动态创建选择对象属性来输出它,但它并没有像我希望的那样工作。
SubTenantUsers 和 MainTenantUsers 变量包含来自 的信息Get-AzureADUser
,Get-AzureADDirectoryRole
并Get-AzureADGroup
帮助确定为各个用户分配了哪些组和角色。
# Get a list of tenants the user has access to
$Tenants = Get-AzTenant | Where-Object {$_.TenantId -NotMatch $TenantId}
# Get user Roles in other tenants
foreach ($Tenant in $Tenants) {
$null = Connect-AzureAD -TenantId $Tenant.Id
$SubTenantUsers = Get-TenantUsersRolesAndGroups
foreach ($MainTenantUser in $MainTenantUsers) {
### Add tenant to list of tenants the user is a member of
if ($SubTenantUsers.UserPrincipalName -like ('{0}*' -f $MainTenantUser.UserPrincipalName.Replace("@", "_"))) {
$MainTenantUser.AccessibleTenants += $Tenant.Name
}
### Add the users roles and groups in the tenant
$null = Add-Member -InputObject $MainTenantUser -MemberType 'NoteProperty' -Name $Tenant.Name -Force -Value (
[PSCustomObject]@{
Roles = ($SubTenantUsers | Where-Object UserPrincipalName -like ('{0}*' -f $MainTenantUser.UserPrincipalName.Replace("@", "_")) | Select-Object Roles).Roles
Groups = ($SubTenantUsers | Where-Object UserPrincipalName -like ('{0}*' -f $MainTenantUser.UserPrincipalName.Replace("@", "_")) | Select-Object Groups).Groups
}
)
}
}
foreach ($Tenant in $Tenants) {
$properties += @{
Name = "$($Tenant.Name) - AADRoles"
Expression = {($_.($Tenant.Name).'Roles'.'DisplayName' | Sort-Object) -join ', '}
}
$properties += @{
Name = "$($Tenant.Name) - Groups"
Expression = {($_.($Tenant.Name).'Groups'.'DisplayName' | Sort-Object) -join ', '}
}
}
$MainTenantUsers | Sort-Object 'DisplayName' | Select-Object $properties |
Export-Csv -Path "$pathto\output.csv" -Encoding 'UTF8' -NoTypeInformation