0

I'm working on a script that allows me to fetch all of our Teams groups by their IDs and list the Id, Name, Owner(s), Member(s) and Guest(s).

The code works to a certain point, I get all the needed information, but it seems to be limiting it to 2 owners, 4 members and no guests...

When I run the code with adding it to a PSObject and simply do a write-host all the data is there, but I can't append it correctly to a CSV.

Code below, its either a limitation of the PSObject or I am doing something wrong/ missing something (hoping on the 2nd part ;) )

    try
{
    $host.Runspace.ThreadOptions = "ReuseThread"

    # Get the credentials  
    Connect-AzureAD
 
    # Connect to Microsoft Teams  
    Connect-MicrosoftTeams
 
    # Get all the teams from tenant
    [array]$teamColl = $null 
    [array]$ownerColl = $null
    [array]$memberColl = $null
    [array]$guestColl = $null

    $teamColl=Get-Team

    $date = Get-Date -Format "yyyy-MM-dd"
    $OutputFile01 = "C:\temp\GetTeamsOwnersAndMembers-$date.csv"

    # Clean file
    Remove-Item $OutputFile01 -ErrorAction SilentlyContinue

    $objectCollection=@()

    $ownerCount = 0
    $memberCount = 0
    $guestCount = 0

    # Loop through the teams  
    foreach($team in $teamColl)  
    {
        $object = New-Object PSObject 

        # Get the Teams basic information
        $object | Add-Member -type NoteProperty -Name ID -Value $team.GroupId
        $object | Add-Member -type NoteProperty -Name TeamsName -Value $team.DisplayName
        #$object | Add-Member -type NoteProperty -Name Description -Value $team.Description

        # Get the Teams owners
        $ownerColl = Get-TeamUser -GroupId $team.GroupId -Role Owner
        $memberColl = Get-TeamUser -GroupId $team.GroupId -Role Member
        $guestColl = Get-TeamUser -GroupId $team.GroupId -Role Guest

        #Write-Host "$ownerColl"
        #Write-Host "$memberColl"
        #Write-Host "$guestColl"

        # Loop through the owners 
        foreach($owner in $ownerColl)
        {
            $ownerCount++ 
            $object | Add-Member -type NoteProperty -Name Owner_$ownerCount -Value $owner.User     
        }

        # Loop through the members
        foreach($member in $memberColl)
        {
            $memberCount++
            $object | Add-Member -type NoteProperty -Name Member_$memberCount -Value $member.User
        }

        # Loop through the guests
        foreach($guest in $guestColl)
        {
            $guestCount++
            $object | Add-Member -type NoteProperty -Name Guest_$guestCount -Value $guest.User
        }

        # Reset counters
        $ownerCount = 0
        $memberCount = 0
        $guestCount = 0

        $objectCollection += $object
    }

    $objectCollection | Export-Csv $OutputFile01 -NoTypeInformation
}

catch [System.Exception] 
{ 
    Write-Host -ForegroundColor Red $_.Exception.ToString()    
}

finally
{
    Write-Host "Done"
}
  • Does this answer your question: [Not all properties displayed](https://stackoverflow.com/a/44429084/1701026)? – iRon Oct 27 '20 at 08:09
  • 2
    As a side note: try to [avoid using the increase assignment operator (+=) to create a collection](https://stackoverflow.com/a/60708579/1701026) as it is exponential expensive. – iRon Oct 27 '20 at 09:16

1 Answers1

0

Was able to solve it, I needed to use the -join to add the additional users :)

Working code:

try
{
    $host.Runspace.ThreadOptions = "ReuseThread"

    # Get the credentials  
    Connect-AzureAD
 
    # Connect to Microsoft Teams  
    Connect-MicrosoftTeams
 
    # Get all the teams from tenant
    [array]$teamColl = $null 
    [array]$ownerColl = $null
    [array]$memberColl = $null
    [array]$guestColl = $null

    $teamColl=Get-Team

    $date = Get-Date -Format "yyyy-MM-dd"
    $OutputFile01 = "C:\temp\GetTeamsOwnersAndMembers-$date.csv"

    # Clean file
    Remove-Item $OutputFile01 -ErrorAction SilentlyContinue

    $GroupsCSV=@()

    Write-Host -ForegroundColor Green "Processing Groups"

    # Loop through the teams  
    foreach($team in $teamColl)  
    {
        $ownerCount = 0
        $memberCount = 0
        $guestCount = 0

        Write-Host -ForegroundColor Yellow -NoNewline "."
        $ownerColl = Get-TeamUser -GroupId $team.GroupId -Role Owner
        $ownerCollection=@()
        # Loop through the owners
        foreach($owner in $ownerColl)
        {
            $ownerCount++
            $ownerCollection += $owner.User
        }

        $memberColl = Get-TeamUser -GroupId $team.GroupId -Role Member
        $memberCollection=@()
        # Loop through the members
        foreach($member in $memberColl)
        {
            $memberCount++
            $memberCollection += $member.User
        }

        $guestColl = Get-TeamUser -GroupId $team.GroupId -Role Guest
        $guestCollection=@()
        # Loop through the guests
        foreach($guest in $guestColl)
        {
            $guestCount++
            $guestCollection += $guest.User
        }

        # Create CSV file line
        $GroupsRow = [pscustomobject]@{
            GroupId = $team.GroupId
            Name = $team.DisplayName
            OwnerCount = $ownerCount
            MemberCount = $memberCount
            GuestCount = $guestCount
            Owners = $ownerCollection -join " | "
            Members = $memberCollection -join " | "
            Guests = $guestCollection -join " | "
        }

        # Add to export array
        $GroupsCSV+=$GroupsRow
    }

    # Export to CSV
    Write-Host -ForegroundColor Green "`nCreating and exporting CSV file"
    $GroupsCSV | Export-Csv -NoTypeInformation -Path $OutputFile01
}

catch [System.Exception] 
{ 
    Write-Host -ForegroundColor Red $_.Exception.ToString()    
}

finally
{
    Write-Host "Done"
}