Microsoft 365 解决方案:如何通过Powershell获取所有Private Channel及其成员列表

时间:2022-12-27 19:57:36

Blog链接:​https://blog.51cto.com/13969817

如上一篇Blog所分享,Private Channel是私有频道,只有Teams中属于Private Channel的所有者或成员的用户才能访问该Channel,其他任何人,包括Guest都无法访问,今天本文将给大家分享如何获取Private Channel以及成员并保存在csv 文件中。

我们可以使用Get-TeamChannelUser cmdlet检索PrivateChannel的成员和所有者,以下脚本获取所有Private Channel及其成员,并将结果导出到CSV文件。

具体脚本如下所示:

$Result = @() #Result array
$AllTeams= Get-Team
$TotalTeams = $AllTeams.Count
$i = 0
ForEach ($Team in $AllTeams)
{
$i++
Write-Progress -Activity "Fetching private channels from $($Team.Displayname)" -Status "$i out of $TotalTeams teams completed" -Id 1
Try
{
$PrivateChannels = Get-TeamChannel -GroupId $Team.GroupId -MembershipType Private
$TotalChannels = $PrivateChannels.Count
$ch=0
ForEach ($Channel in $PrivateChannels)
{
$ch++
Write-Progress -Activity "Fetching members from $($Channel.Displayname)" -Status "$ch out of $TotalChannels Channels completed" -ParentId 1
Try
{
$ChannelUsers = Get-TeamChannelUser -GroupId $Team.GroupId -DisplayName $Channel.DisplayName
ForEach ($ChannelUser in $ChannelUsers)
{
$Result += New-Object PSObject -property $([ordered]@{
TeamName = $Team.DisplayName
ChannelName = $Channel.DisplayName
UserName = $ChannelUser.Name
UserPrincipalName = $ChannelUser.User
Role = $ChannelUser.Role
GroupId = $Team.GroupId
ChannelId = $Channel.Id
UserId = $ChannelUser.UserId
})
}
}
Catch
{
Write-Host "Error occurred for $($Team.Displayname) - $($Channel.Displayname)" -f Yellow
Write-Host $_ -f Red
}
}

}
Catch
{
Write-Host "Error occurred for $($Team.Displayname)" -f Yellow
Write-Host $_ -f Red
}
}
$Result | Export-CSV "C:\Temp\AllPrivateChannelMembers.CSV" -NoTypeInformation -Encoding UTF8

谢谢大家阅读,若日后大家有其他需求,欢迎线下随时讨论。