问题:如何使用PowerShell Microsoft Graph检索一组AzureAD组的成员

kpbwa7wx  于 2023-04-21  发布在  Shell
关注(0)|答案(1)|浏览(130)

我在从AzureAD获取一系列组的所有成员时遇到问题。谁可以帮助我?提前感谢您使用下面的脚本,我可以检索1个组的成员,并且工作正常。

Connect-MgGraph
$GROUP_NAME = 'SUG-APP-MAN-SPBI_BICC-P'
$group = Get-MgGroup -Filter "DisplayName eq '$GROUP_NAME'"
$members = Get-MgGroupMember -GroupId $group.Id
$users = @()
foreach ($member in $members)
{
$user = Get-MgUser -UserId $member.Id
$users += New-Object PSObject -Property @{Group = $group.DsplayName;
Name = $user.DisplayName;
USERPRINCIPALNAME = $user.Mail}
}
$users | Format-Table

我试图检索一个组范围内的所有成员。问题是我如何定义变量$GROUP_NAME

Connect-MgGraph
$GROUP_LIST = Get-MgGroup
foreach ($Group in $GROUP_LIST)
{
$GROUP_NAME = $GROUP_LIST
$group = Get-MgGroup -Filter "DisplayName eq '$GROUP_NAME'"
$members = Get-MgGroupMember -GroupId $group.Id
$users = @()
foreach ($member in $members)
{
$user = Get-MgUser -UserId $member.Id
$users += New-Object PSObject -Property @{Group = $group.DsplayName;
Name = $user.DisplayName;
USERPRINCIPALNAME = $user.Mail}
}
}
$users | Format-Table

错误:

zi8p0yeb

zi8p0yeb1#

这对我很有效

Connect-MgGraph -Scopes "User.Read.All", "GroupMember.Read.All", "Group.Read.All", "Directory.Read.All"
$GROUP_LIST = Get-MgGroup |select Id, DisplayName, Description, GroupTypes
$Group=""
$users = @()
foreach ($Group in $GROUP_LIST) {

  #$group = Get-MgGroup -Filter "DisplayName eq '$Group.DisplayName'"
  $members = Get-MgGroupMember -GroupId $group.Id

  Write-Host $Group.DisplayName
  foreach ($member in $members) {
    $user = Get-MgUser -UserId $member.Id
    $users += New-Object PSObject -Property @{Group = $group.DisplayName;
      Name                                          = $user.DisplayName;
      USERPRINCIPALNAME                             = $user.Mail
    }
  }  
}

$users | Format-Table

相关问题