Azure Active Directory -通过powershell脚本导出用户列表和嵌套组

j8ag8udp  于 2023-06-06  发布在  Shell
关注(0)|答案(1)|浏览(165)

我在Azure Active Directory中有一个组,它具有嵌套组。我想出口的用户列表,连同他们的B组运行powershell脚本.理想情况下,我想输出显示用户的姓名,电子邮件和组。我试着写一个脚本,但我只能列出组或只有名字,但不是所有的东西在一起。
目前我有下面的代码,但我不知道为什么我一直得到错误:Get-AzureADGroupMember:无法将参数绑定到参数“ObjectId”,因为它为Null。

$groups=Get-AzureADGroup -SearchString 'my-group-name'
$resultsarray =@()
ForEach ($group in $groups)
{
$members = Get-AzureADGroupMember -ObjectId $group.ObjectId

 #Recursive call if the object is a group
        if ($members.ObjectType -eq 'Group')
        {
                  $resultarray += Get-AzureADGroupMembers -ObjectId $member.ObjectId
        }
}

#Add to output if object is a user
        if ($members.ObjectType -eq 'User')
        {
                   ForEach ($member in $members){
       $UserObject = new-object PSObject
       $UserObject | add-member  -membertype NoteProperty -name "Group Name" -Value $group.DisplayName
       $UserObject | add-member  -membertype NoteProperty -name "Member Name" -Value $member.DisplayName
       $UserObject | add-member  -membertype NoteProperty -name "ObjType" -Value $member.ObjectType
       $UserObject | add-member  -membertype NoteProperty -name "UserType" -Value $member.UserType
       $UserObject | add-member  -membertype NoteProperty -name "UserPrinicpalName" -Value $member.UserPrincipalName
       $resultsarray += $UserObject
    }
          }
    $resultsarray | Export-Csv -Encoding UTF8  -Delimiter ";" -Path 'C:\Users\Desktop\aadgroupmembers.csv'
thtygnil

thtygnil1#

我创建了一个Azure AD组并添加了成员,如下所示:

并在嵌套组**rukgroup**中添加如下成员:

要获取用户列表和嵌套组,请使用以下PowerShell脚本:

$group = Get-AzureADGroup -SearchString "testnestedgrp"

$resultsarray = @()

$members = Get-AzureADGroupMember -ObjectId $group.ObjectId -All $true

foreach ($member in $members) {
    if ($member.ObjectType -eq "User") {
        $user = Get-AzureADUser -ObjectId $member.ObjectId
        $userObject = New-Object PSObject
        $userObject | Add-Member -MemberType NoteProperty -Name "GroupName" -Value $group.DisplayName
        $userObject | Add-Member -MemberType NoteProperty -Name "MemberName" -Value $user.DisplayName
        $userObject | Add-Member -MemberType NoteProperty -Name "UPN" -Value $user.UserPrincipalName
        $userObject | Add-Member -MemberType NoteProperty -Name "NestedGroupName" -Value $null
        $resultsarray += $userObject
    }
    else {
        if ($member.ObjectType -eq "Group") {
            $nestedGroup = Get-AzureADGroup -ObjectId $member.ObjectId
            $nestedGroupObject = New-Object PSObject
            $nestedGroupObject | Add-Member -MemberType NoteProperty -Name "GroupName" -Value $group.DisplayName
            $nestedGroupObject | Add-Member -MemberType NoteProperty -Name "NestedGroupName" -Value $nestedGroup.DisplayName
            $nestedGroupObject | Add-Member -MemberType NoteProperty -Name "MemberName" -Value $null
            $nestedGroupObject | Add-Member -MemberType NoteProperty -Name "UPN" -Value $null
            $resultsarray += $nestedGroupObject

          
            $nestedMembers = Get-AzureADGroupMember -ObjectId $nestedGroup.ObjectId -All $true
            foreach ($nestedMember in $nestedMembers) {
                if ($nestedMember.ObjectType -eq "User") {
                    $nestedUser = Get-AzureADUser -ObjectId $nestedMember.ObjectId
                    $nestedUserObject = New-Object PSObject
                    $nestedUserObject | Add-Member -MemberType NoteProperty -Name "GroupName" -Value $group.DisplayName
                    $nestedUserObject | Add-Member -MemberType NoteProperty -Name "NestedGroupName" -Value $nestedGroup.DisplayName
                    $nestedUserObject | Add-Member -MemberType NoteProperty -Name "MemberName" -Value $nestedUser.DisplayName
                    $nestedUserObject | Add-Member -MemberType NoteProperty -Name "UPN" -Value $nestedUser.UserPrincipalName
                    $resultsarray += $nestedUserObject
                }
                else {
                    Write-Host "Object not found: $($nestedMember.ObjectType)"
                }
            }
        }
        else {
            Write-Host "Object not found: $($member.ObjectType)"
        }
    }
}

$resultsarray | Export-Csv -Path "C:\aadgroupmembers.csv" -NoTypeInformation

CSV文件成功导出,嵌套组名成员如下:

相关问题