Good afternoon all,
Hoping someone can help with a broad problem I'm having with efficiency in the follow PowerShell query.
In brief, the idea behind the following code is:
**1)**From a predefined list of MS Teams GroupID's (260 in total), all held in 'List.txt' file
**2)**Run through each of the MS Teams GroupIDs and extract the Team Name and the Team Owners.
**3)**Export the data to a .csv in the format:
Team Name: | Team Owners: |
---|---|
STH-TeamName | [email protected], [email protected] |
STH-AnotherTeam | [email protected], [email protected] |
The code, whilst cobbled together from a number of sources does appear to be working in essence. However, "Write-Host" steps 2 and 5 appear to be returning the DisplayName very slowly indeed. I am a complete beginner with PowerShell queries so the syntax and general efficiencies are not in my favour at the moment.
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
Any support here would be greatly appreciated - still very much learning the ropes of PowerShell and of Stack Overflow.
CodePudding user response:
Looking at the error Get-Team : A parameter cannot be found that matches parameter name 'GroupId'., you may have an older version of the MicrosoftTeamsPowerShell module, but then you could try this instead:
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
CodePudding user response:
You're calling Get-Team
which, without parameters, will return all teams. This means you're getting the full Team list every iteration. You could improve that by changing the query to just a parameter.
Write-Host 2 Stepping Through Team GroupIDs...
$TeamIDs = Foreach ($Item in $TeamList)
{get-team -GroupId $Item
}
The same thing could be improved on step 5
Foreach ($Team in $AllTeams)
{
Write-Host 5 Doing teamName bit...
$TeamName = (Get-Team -GroupId $Team).DisplayName
...
But these changes are only to keep your steps the same. This whole script could definitely be improved because you should only need to call Get-Team
one time per team.