You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

从GroupID列表提取Microsoft Teams团队名称与所有者的PowerShell脚本效率优化求助

Optimizing Your PowerShell Teams Owner Report Script

Hey DanG, welcome to PowerShell! Let's fix that slow script you're dealing with—those repeated calls to Get-Team are killing your performance, and we can make this run way faster with a few key changes.

Problem Breakdown

Your current script calls Get-Team once per GroupID in your list (260+ times) plus another time per team in the loop. Each Get-Team call is a remote request to Microsoft 365, which adds up to tons of unnecessary latency. We can eliminate almost all of those remote calls by fetching data in bulk first.

Optimized Script

Here's a revised version that will drastically speed up your report generation:

Import-Module MicrosoftTeams
Connect-MicrosoftTeams

Write-Host "1. Opening GroupID list file..."
$TeamList = Get-Content C:\users\USERNAME\TeamOwnerReport\List.txt

# Fetch ALL teams in your organization ONCE (this is the biggest performance boost)
Write-Host "2. Fetching all Teams from Microsoft 365..."
$AllOrgTeams = Get-Team

# Filter the full Teams list to only include the GroupIDs in your text file
Write-Host "3. Filtering Teams to match your GroupID list..."
$TargetTeams = $AllOrgTeams | Where-Object { $TeamList -contains $_.GroupId }

Write-Host "4. Processing team owners and building report..."
# Build the report by iterating over our pre-filtered teams
$TeamReport = foreach ($Team in $TargetTeams) {
    Write-Host "Processing: $($Team.DisplayName)"
    # Get owners for the current team (only one remote call per team here, which is unavoidable)
    $TeamOwners = (Get-TeamUser -GroupId $Team.GroupId | Where-Object { $_.Role -eq 'Owner' }).User
    
    # Output the custom object directly; PowerShell will collect these into $TeamReport automatically
    [PSCustomObject]@{
        TeamName   = $Team.DisplayName
        TeamOwners = $TeamOwners -join ', '
    }
}

# Generate output file path with date
$OutputPath = 'C:\users\USERNAME\TeamOwnerReport'
$OutputFile = Join-Path -Path $OutputPath -ChildPath "TeamsReport_$(Get-Date -Format 'dd-MM-yy').csv"

# Export to CSV
$TeamReport | Export-Csv -Path $OutputFile -NoTypeInformation

Write-Host "Done! Report saved to: $OutputFile"

Key Improvements Explained

Let's go over why this is faster and cleaner:

  • Bulk Get-Team fetch: We call Get-Team just once to get all teams in your organization, then filter locally using your GroupID list. This cuts out 260+ remote requests right away.
  • Reuse Team object properties: The $TargetTeams variable already has the DisplayName property, so we don't need to call Get-Team again in the loop to retrieve it.
  • Efficient array building: Instead of using $TeamReport += ... (which creates a new array every time, slowing things down), we use a foreach loop that outputs objects directly. PowerShell automatically collects these into the $TeamReport array efficiently.
  • Reliable path construction: Join-Path handles path separators correctly, so you don't have to worry about missing or extra slashes in your output path.

Bonus Tips

  • If your organization has thousands of teams, you might want to add a filter to the initial Get-Team call (e.g., -MailNickName *STH* if your teams follow a naming pattern) to reduce the data fetched.
  • Consider adding error handling (like try/catch blocks) for cases where a GroupID in your list doesn't exist or you don't have permissions to access a team.

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

火山引擎 最新活动