GroupID 列表中的 PowerShell 团队所有者和团队名称

PowerShell Team Owners and Team names from a List of GroupIDs

大家下午好,

希望有人可以帮助解决我在执行以下 PowerShell 查询时遇到的广泛问题。

简而言之,以下代码背后的思想是:

**1)**来自 MS Teams GroupID 的预定义列表(总共 260 多个),全部保存在 'List.txt' 文件

**2)**运行 通过每个 MS 团队组 ID 并提取 团队名称 团队所有者.

**3)**将数据导出为 .csv 格式:

Team Name: Team Owners:
STH-TeamName Owner1@nhs.net, Owner2@nhs.net
STH-AnotherTeam Owner3@nhs.net, Owner4@nhs.net

该代码虽然从多个来源拼凑而成,但实际上似乎确实有效。但是,“Write-Host”步骤 25 似乎确实非常缓慢地返回 DisplayName。我是 PowerShell 查询的完全初学者,因此语法和一般效率目前对我不利。

Import-Module Microsoftteams

Connect-MicrosoftTeams

Write-Host 1 Opening Lists File...
$TeamList = Get-content C:\users\USERNAME\TeamOwnerReport\List.txt

Write-Host 2 Stepping Through Team GroupIDs...
$TeamIDs = Foreach ($Item in $TeamList)
{get-team | where-object {$_.GroupID -eq $Item}
}

Write-Host 3 Stepping AllTeams are TEAMIDs...
$AllTeams = $TeamIDs.GroupID 

Write-Host 4 Doing Array bit...
$TeamReport = @()

Foreach ($Team in $AllTeams)
{
Write-Host 5 Doing teamName bit...
$TeamName = (Get-Team | Where-object {$_.GroupID -eq $Team}).DisplayName

Write-Host 6 Doing teamOwner bit...
$TeamOwner = (Get-TeamUser -GroupId $Team | Where-object {$_.Role -eq 'Owner'}).User

Write-Host 7 Doing teamReport bit...
$TeamReport = $TeamReport + [PSCustomObject]@{TeamName = $TeamName;TeamOwners = $TeamOwner -join ', ';}
}

$Path = 'C:\users\USERNAME\TeamOwnerReport'

$TeamReport | Export-csv ("$Path\TeamsReport_" + (get-date -format "dd-MM-yy") +".csv") -NoTypeInformation

我们将不胜感激 - 仍在学习 PowerShell 和 Stack Overflow 的诀窍。

您正在调用 Get-Team,如果没有参数,将 return 所有团队。这意味着每次迭代 您都会获得完整的团队列表 。您可以通过将查询更改为仅一个参数来改进它。

Write-Host 2 Stepping Through Team GroupIDs...
$TeamIDs = Foreach ($Item in $TeamList)
{get-team -GroupId $Item
}

同样的事情可以在第 5 步进行改进

Foreach ($Team in $AllTeams)
{
Write-Host 5 Doing teamName bit...
$TeamName = (Get-Team -GroupId $Team).DisplayName
...

但这些更改只是为了让您的步骤保持不变。整个脚本肯定可以改进,因为每个团队只需要调用 Get-Team 一次。

查看错误 Get-Team:找不到与参数名称 'GroupId'. 匹配的参数,您可能使用的是旧版本的 MicrosoftTeamsPowerShell模块,但你可以试试这个:

Import-Module Microsoftteams

Connect-MicrosoftTeams

# read the array of team group id's, remove empty or whitespace only lines
$TeamList   = Get-Content -Path 'C:\users\USERNAME\TeamOwnerReport\List.txt' | Where-Object { $_ -match '\S' }
$TeamReport = Get-Team | Where-object {$TeamList -contains $_.GroupId} | ForEach-Object {
    $owners= Get-TeamUser -GroupId $_.GroupId -Role Owner
    [PsCustomObject]@{
        TeamName   = $_.DisplayName
        TeamOwners = ($owners.User -join ', ')
    }
}


# save the report as CSV file
$outputPath = 'C:\users\USERNAME\TeamOwnerReport_{0:dd-MM-yyyy}.csv' -f (Get-Date)
$TeamReport | Export-Csv -Path $outputPath -NoTypeInformation