Good afternoon all,
I was very kindly shown the following code by the user 'Theo', the code is designed to return MS Teams Owners and members with their respective team names and Group IDs.
Import-Module Microsoftteams
Connect-MicrosoftTeams
#=========================================================================
$GUIDList = 'C:\users\Your_User_Name_Here\TeamOwnerReport\ListGUID.txt'
#=========================================================================
$TeamList = Get-Content -Path "$GUIDList" | Where-Object { $_ -match '\S' }
$TeamReport = Get-Team | Where-object {$TeamList -contains $_.GroupId} | ForEach-Object {
$owners= Get-TeamUser -GroupId $_.GroupId -Role Owner
$members= Get-TeamUser -GroupId $_.GroupID -Role Member
[PsCustomObject]@{
TeamName = $_.DisplayName
GUID = $_.GroupID
TeamOwners = ($owners.User -join '; ')
TeamMembers = ($members.User -join '; ')
}
}
#=========================================================================
$FileName = 'MSTeamOwnMem'
$path = 'C:\Users\Your_User_Name_Here\TeamOwnerReport'
#=========================================================================
$OutputPath = "$path\$FileName-{0:yyyy-MM-dd-HHmm}.csv" -f (Get-Date)
$TeamReport | Export-Csv -Path $OutputPath -NoTypeInformation
Disconnect-MicrosoftTeams
This produces a lovely .csv table as below.
TeamName | GroupID | TeamOwners | TeamMembers |
---|---|---|---|
Team1 | GUID1 | [email protected]; [email protected] | [email protected]; [email protected] |
Team2 | GUID2 | [email protected]; Owner4nhs.net | [email protected] |
I would like the code to instead produce a .csv table that looks as follows
TeamName | GroupID | UserName | Role |
---|---|---|---|
Team1 | GUID1 | [email protected] | Owner |
Team1 | GUID1 | [email protected] | Owner |
I have tried removing the -join
command from the code, but holding my hands up here, I'm unfamiliar with the syntax involved with PowerShell.
Here is the original post with code: PowerShell Team Owners and Team names from a List of GroupIDs
Any pointers would be most welcome.
CodePudding user response:
I haven't tested this, it's just with a quick look at your code, but basically at the moment it's just looping one time, over the list of teams themselves. You want to add more loops, to go through each of the owners and then each of the members. Try replace:
$TeamReport = Get-Team | Where-object {$TeamList -contains $_.GroupId} | ForEach-Object {
$owners= Get-TeamUser -GroupId $_.GroupId -Role Owner
$members= Get-TeamUser -GroupId $_.GroupID -Role Member
[PsCustomObject]@{
TeamName = $_.DisplayName
GUID = $_.GroupID
TeamOwners = ($owners.User -join '; ')
TeamMembers = ($members.User -join '; ')
}
}
with this:
this:
$TeamReport = @()
$teams = Get-Team | Where-object {$TeamList -contains $_.GroupId}
foreach ($team in $teams) {
$owners = Get-TeamUser -GroupId $team.GroupId -Role Owner
$members = Get-TeamUser -GroupId $team.GroupID -Role Member
foreach ($owner in $owners) {
$TeamReport = [PsCustomObject]@{
TeamName = $team.DisplayName
GUID = $team.GroupID
UserName = $owner.User
Role = 'Owner'
}
}
foreach ($member in $members) {
$TeamReport = [PsCustomObject]@{
TeamName = $team.DisplayName
GUID = $team.GroupID
UserName = $member.User
Role = 'Member'
}
}
}