尝试使用Powershell以CSV格式将所有团队及其所有者,成员和访客

问题描述

我正在编写一个脚本,该脚本允许我按其ID提取所有团队组并列出ID,名称,所有者,成员和来宾。

代码在一定程度上有效,我获得了所有必要的信息,但似乎将其限制为2个所有者,4个成员且没有来宾...

当我运行代码并将其添加到PSObject并简单地对所有数据进行写主机时,但是我无法正确地将其附加到CSV。

下面的代码,它可能是PSObject的限制,或者我做错了什么/缺少了一些东西(希望在第二部分;))

    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"
}

解决方法

要解决此问题,我需要使用-join添加其他用户:)

工作代码:

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"
}