You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写PowerShell脚本整合Connect-ExchangeOnline、Get-UnifiedGroup、Get-Team及Get-TeamUser命令并导出数据至CSV文件

当然可行!我来帮你一步步实现这些需求,下面是具体的PowerShell脚本示例和关键说明:

前置准备:安装必要的PowerShell模块

在运行脚本前,请确保你已经安装了以下两个模块(如果没装,打开PowerShell作为管理员执行下面的命令):

# 安装Exchange Online管理模块
Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber

# 安装Microsoft Teams模块
Install-Module -Name MicrosoftTeams -Force -AllowClobber

一、整合Exchange Online连接与Unified Groups数据导出

这个脚本会先连接到Exchange Online,然后获取所有Unified Group的核心信息并导出到CSV:

# 设置输出路径(请替换成你自己的文件夹路径)
$outputPath = "C:\O365_Data_Exports"

# 确保输出文件夹存在
if (-not (Test-Path $outputPath)) {
    New-Item -ItemType Directory -Path $outputPath | Out-Null
}

# 连接到Exchange Online(替换成你的管理员邮箱)
Connect-ExchangeOnline -UserPrincipalName your_admin@domain.com -ShowProgress $true

# 获取Unified Groups数据并导出
$unifiedGroups = Get-UnifiedGroup | Select-Object DisplayName, WhenChanged, GroupId, PrimarySmtpAddress, Description
$unifiedGroups | Export-Csv -Path "$outputPath\UnifiedGroups.csv" -NoTypeInformation -Encoding UTF8

Write-Host "Unified Groups数据已导出到 $outputPath\UnifiedGroups.csv"

二、同时导出Unified Groups、Teams及Team用户数据

这个脚本会一次性完成双平台连接,然后分别导出三类数据到独立的CSV文件,方便后续分析:

# 设置输出路径(请替换成你自己的文件夹路径)
$outputPath = "C:\O365_Data_Exports"

# 确保输出文件夹存在
if (-not (Test-Path $outputPath)) {
    New-Item -ItemType Directory -Path $outputPath | Out-Null
}

# --------------------------
# 1. 连接Exchange Online并导出Unified Groups
# --------------------------
Connect-ExchangeOnline -UserPrincipalName your_admin@domain.com -ShowProgress $true
$unifiedGroups = Get-UnifiedGroup | Select-Object DisplayName, WhenChanged, GroupId, PrimarySmtpAddress, Description
$unifiedGroups | Export-Csv -Path "$outputPath\UnifiedGroups.csv" -NoTypeInformation -Encoding UTF8
Write-Host "Unified Groups数据导出完成"

# --------------------------
# 2. 连接Microsoft Teams并导出Teams列表
# --------------------------
Connect-MicrosoftTeams -UserPrincipalName your_admin@domain.com
$teams = Get-Team | Select-Object GroupId, DisplayName, Description, Visibility, CreatedDateTime
$teams | Export-Csv -Path "$outputPath\Teams.csv" -NoTypeInformation -Encoding UTF8
Write-Host "Teams列表数据导出完成"

# --------------------------
# 3. 导出所有Team的用户数据(自动关联Team名称)
# --------------------------
$allTeamUsers = @()
foreach ($team in $teams) {
    Write-Host "正在获取团队 $($team.DisplayName) 的用户..."
    $teamUsers = Get-TeamUser -GroupId $team.GroupId
    foreach ($user in $teamUsers) {
        $allTeamUsers += [PSCustomObject]@{
            TeamName = $team.DisplayName
            TeamGroupId = $team.GroupId
            UserUPN = $user.UserPrincipalName
            UserName = $user.Name
            UserRole = $user.Role
        }
    }
}
$allTeamUsers | Export-Csv -Path "$outputPath\TeamUsers.csv" -NoTypeInformation -Encoding UTF8
Write-Host "Team用户数据导出完成"

# 断开连接(可选,避免会话残留)
Disconnect-ExchangeOnline -Confirm:$false
Disconnect-MicrosoftTeams -Confirm:$false

三、单独导出Teams及Team用户数据

如果只需要Teams相关数据,用这个简化版脚本即可:

# 设置输出路径(请替换成你自己的文件夹路径)
$outputPath = "C:\O365_Data_Exports"

# 确保输出文件夹存在
if (-not (Test-Path $outputPath)) {
    New-Item -ItemType Directory -Path $outputPath | Out-Null
}

# 连接到Microsoft Teams
Connect-MicrosoftTeams -UserPrincipalName your_admin@domain.com

# 导出Teams列表
$teams = Get-Team | Select-Object GroupId, DisplayName, Description, Visibility, CreatedDateTime
$teams | Export-Csv -Path "$outputPath\Teams.csv" -NoTypeInformation -Encoding UTF8
Write-Host "Teams列表导出完成"

# 导出所有Team的用户数据
$allTeamUsers = @()
foreach ($team in $teams) {
    Write-Host "处理团队: $($team.DisplayName)"
    $users = Get-TeamUser -GroupId $team.GroupId
    foreach ($user in $users) {
        $allTeamUsers += [PSCustomObject]@{
            TeamDisplayName = $team.DisplayName
            TeamGroupId = $team.GroupId
            UserPrincipalName = $user.UserPrincipalName
            UserName = $user.Name
            Role = $user.Role
        }
    }
}
$allTeamUsers | Export-Csv -Path "$outputPath\TeamUsers.csv" -NoTypeInformation -Encoding UTF8
Write-Host "Team用户数据导出完成"

# 断开连接
Disconnect-MicrosoftTeams -Confirm:$false

关键注意事项:

  • 请确保你使用的账号拥有Exchange Online管理员和Teams管理员权限,否则可能无法获取全部数据。
  • 第一次运行模块时,可能需要确认信任模块来源(输入Y即可)。
  • 导出的CSV用UTF8编码,避免中文乱码问题。
  • 如果连接Exchange Online时遇到现代验证问题,可以尝试添加 -UseRPSSession 参数到 Connect-ExchangeOnline 命令(仅适用于旧版模块)。

内容的提问来源于stack exchange,提问作者Toto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 06:13:12