PowerShell - 在自己的行中列出项目

PowerShell - List items on own line

大家下午好,

用户 'Theo' 向我展示了以下代码,该代码旨在 return MS Teams 所有者和成员及其各自的团队名称和组 ID。

    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 

这会生成一个可爱的 .csv table,如下所示。

TeamName GroupID TeamOwners TeamMembers
Team1 GUID1 Owner1@nhs.net; Owner2@nhs.net Mem1@nhs.net; Mem2@nhs.net
Team2 GUID2 Owner3@nhs.net; Owner4nhs.net Mem3@nhs.net

我希望代码生成如下所示的 .csv table

TeamName GroupID UserName Role
Team1 GUID1 Owner1@nhs.net Owner
Team1 GUID1 Owner2@nhs.net Owner

我试过从代码中删除 -join 命令,但请举手,我不熟悉 PowerShell 涉及的语法。

这里是原始的post代码:

欢迎大家指点。

我还没有对此进行测试,只是快速查看了您的代码,但基本上目前它只是循环一次,遍历团队本身的列表。您想要添加更多循环,遍历每个所有者,然后遍历每个成员。尝试替换:

    $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 '; ')
        }
    }

有了这个:

这个:

    $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'
            }
        }
    }