如何在 office 365 powershell 中列出用户和他们所属的组

how to list users and the groups they are part of in office 365 powershell

我需要一个脚本来导出 Office 365 中的用户和他们所属的组,而不是相反。谁能帮忙。我找到的所有答案都是导出通讯组及其成员。

我尝试使用下面的方法,但我不知道如何select 分组名称。

get-mailbox | ? {$_.PrimarySMTPAddress -like "*domain.com"} | Select DisplayName,Alias,PrimarySMTPAddress'

我也试过了

get-mailbox | ? {$_.PrimarySMTPAddress -like "*domain.com"} | Sort Name | % { $MbxDirData = $_ ; Get-MailboxStatistics $_ } | Select DisplayName, @{E={ $MbxDirData.Alias };L='Alias'}, @{E={ $MbxDirData.PrimarySMTPAddress };L='PrimarySMTPAddress'}, @{E={ $_.TotalItemSize.Value + $_.TotalDeletedItemSize.Value };L="TotalMailboxSize"}

感谢任何帮助。

我们可以使用 PowerShell cmdlet Get-UnifiedGroup and its group members by Get-UnifiedGroupLinks cmdlet 列出所有 office 365 组。

您可以使用以下 PowerShell 脚本,它将所有 Office 365 组成员导出到 csv。我们已经在我们的本地环境中测试了它,它运行良好。

$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-UnifiedGroupLinks -Identity $group.Name -LinkType Members -ResultSize Unlimited | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group.DisplayName
       Member = $_.Name
       EmailAddress = $_.PrimarySMTPAddress
       RecipientType= $_.RecipientType
}}} | Export-CSV "C:\Office365GroupMembers.csv" -NoTypeInformation -Encoding UTF8

这里是示例输出截图以供参考:

注: Get-UnifiedGroup cmdlet 仅在基于云的服务中可用。

有关更多信息,请参考 this blog post & also if you faces any issues while executing Get-unifiedGroup cmdlet you refer this .

这未经测试,但我认为您可以使用 cmdlet Get-User and then Get-Group 来检索用户所属的组,如下所示:

Get-Mailbox | Where-Object {$_.PrimarySMTPAddress -like "*domain.com"} | ForEach-Object {
    $user   = Get-User -Identity $_.DistinguishedName                      
    $groups = Get-Group | Where-Object {$_.Members -contains $User}
    $_ | Select-Object DisplayName, Alias, PrimarySMTPAddress,
                       @{Name = 'Groups' ; Expression = {$groups.Name -join '; '}}
} | Export-Csv -Path 'X:\O365UserGroups.csv' -NoTypeInformation

上面在 CSV 的一个字段中用分号连接组,但如果您希望输出每组一行,您可以这样做:

Get-Mailbox | Where-Object {$_.PrimarySMTPAddress -like "*domain.com"} | ForEach-Object {
    $user   = Get-User -Identity $_.DistinguishedName                      
    $groups = Get-Group | Where-Object {$_.Members -contains $User}
    # output a data row for each group in the collection
    foreach ($group in $groups) {
        $_ | Select-Object DisplayName, Alias, PrimarySMTPAddress,
                       @{Name = 'Groups' ; Expression = {$group.Name}}
    }
} | Export-Csv -Path 'X:\O365UserGroups.csv' -NoTypeInformation