当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell 获取 Office 365 组成员?

作者:精品下载站 日期:2024-12-15 00:02:06 浏览:17 分类:玩电脑

如何使用 PowerShell 获取 Office 365 组成员?


要求:使用 PowerShell 获取 Office 365 组的成员。

如何获取 Office 365 组成员?

Office 365 组提供了一种与组织中其他人协作的便捷方式。如果您需要获取所有组成员的列表,PowerShell 可以轻松实现。本指南将向您展示如何使用 PowerShell 获取 Office 365 组中的用户列表。让我们开始吧!

若要从 Microsoft 365 管理中心获取 Office 365 组中的成员列表,请执行以下操作:

  1. 登录 Microsoft 365 管理中心站点:https://admin.microsoft.com
  2. 展开“团队和组”,然后单击左侧导航中的“活跃团队和组”。
  3. 搜索并单击您希望从中获取成员的 Office 365 组。
  4. 在会员选项卡上,点击“会员”。这列出了该组的所有成员。

    [玩转系统] 如何使用 PowerShell 获取 Office 365 组成员?

PowerShell 在 Office 365 中获取组成员:

以下是使用 Get-UnifiedGroup 和 Get-UnifiedGroupLinks cmdlet 的 Office 365 PowerShell 获取组成员。


#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False

#Get all Members of Office 365 Group
Get-UnifiedGroup -Identity "[email protected]" | Get-UnifiedGroupLinks -LinkType Member
 
#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False

您可以将所有 Office 365 组成员导出为 CSV:


#Get all Members of Office 365 Group and export to CSV
Get-UnifiedGroup -Identity "[email protected]" | Get-UnifiedGroupLinks -LinkType Member `
      | Select DisplayName,PrimarySmtpAddress | Export-CSV "C:\Temp\GroupMembers.csv" -NoTypeInformation

使用 PowerShell 将所有 Office 365 组中的成员导出到 CSV

是否曾经需要获取 Microsoft 365 组中所有成员的列表并将其导出到 CSV 文件?当然,我们可以使用 PowerShell 快速检索组中所有成员的列表。以下是用于导出所有 Office 365 组的所有成员的 PowerShell:


$CSVPath = "C:\Temp\AllGroupMembers.csv"

#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False

#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}

#Get All Office 365 Groups
$O365Groups=Get-UnifiedGroup
ForEach ($Group in $O365Groups) 
{ 
    Write-Host "Group Name:" $Group.DisplayName -ForegroundColor Green
    Get-UnifiedGroupLinks -Identity $Group.Id -LinkType Members | Select DisplayName,PrimarySmtpAddress

    #Get Group Members and export to CSV
    Get-UnifiedGroupLinks -Identity $Group.Id -LinkType Members | Select-Object @{Name="Group Name";Expression={$Group.DisplayName}},`
         @{Name="User Name";Expression={$_.DisplayName}}, PrimarySmtpAddress | Export-CSV $CSVPath -NoTypeInformation -Append
}
 
#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False

用于获取 Office 365 组成员的 PnP PowerShell

以下是如何使用 Get-PnPMicrosoft365GroupMembers cmdlet 获取 Office 365 组成员。


#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$GroupEmail = "[email protected]"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive

    #Get the Office 365 Group from Email
    $Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail

    If($Group -ne $Null)
    {
        #Get Office 365 group members
        $Group | Get-PnPMicrosoft365GroupMembers
    }
    Else
    {
        Write-host "Could not Find Group!" -f Yellow
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

将所有组的所有成员导出到 CSV

从 Microsoft 365 环境导出所有组的成员怎么样?


#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$CSVPath = "C:\Temp\GroupsMembersData.csv"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive

    #Get all Office 365 Groups
    $Groups = Get-PnPMicrosoft365Group
    
    $GroupsData = @()
    #Loop through each group
    ForEach($Group in $Groups)
    {
        Write-host "Processing Group:"$Group.DisplayName
        #Get Members of the group
        $GroupMembers = (Get-PnPMicrosoft365GroupMembers -Identity $Group | Select -ExpandProperty UserPrincipalName) -join ";"

        #Get Group details
        $GroupsData += New-Object PSObject -property $([ordered]@{ 
            GroupName  = $Group.DisplayName
            Id = $Group.ID
            Visibility = $Group.Visibility
            Mail = $Group.Mail
            GroupMembers= $GroupMembers
        })
    }
    $GroupsData
    #Export Groups information to CSV
    $GroupsData | Export-Csv -Path $CSVPath -NoTypeInformation
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

使用 Azure AD PowerShell 模块获取 Office 365 组成员

您还可以使用 Azure AD PowerShell 列出组用户:


#Get Credentials to connect
$Cred = Get-Credential

#Connect to AzureAD
Connect-AzureAD -Credential $Cred

#Get the Group
$Group = Get-AzureADGroup -Filter "DisplayName eq 'HR Team'"

#Get Group Members
Get-AzureADGroupMember -ObjectId $Group.ObjectId

将 Azure AD 组成员导出到 CSV 怎么样?

PowerShell 获取 Azure AD 组成员并导出到 CSV


#Connect to AzureAD
Connect-AzureAD | Out-Null

#Get the Group
$Group = Get-AzureADGroup -Filter "DisplayName eq 'Sales Managers'"

#Get Group Members
Get-AzureADGroupMember -ObjectId $Group.ObjectId | Select DisplayName, UserPrincipalName, Mail | Export-Csv -path "C:\Temp\GroupMembers.csv" -Notypeinformation

结论:

本指南向您展示了使用 Microsoft 管理中心和 SharePoint Online PowerShell 模块检索 Office 365 组成员列表的两种方法。这两种方法都易于使用,并且允许您快速检索特定组的成员列表,包括他们的显示名称和电子邮件地址。您还可以将数据导出到 CSV 文件。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯