How can I export info used to gather Teams users?

Viewed 13

I want to look up which users populate which teams and export the info to a csv. I am able to get the user info, but I want to preserve info for which Team they occupy. So far, I am only able to export the last entry from my source csv to my exported data.

#Import Teams list from importFile.csv
$Teams = Import-CSV "importFile.csv"

#Capture Team names for later export
$Teams | Select-Object 'TeamDesc' | ForEach-Object {$teamName = $_.TeamDesc}

#Iterate through list
ForEach($Team in $Teams)
{
#Get Team GroupID from TeamDesc column  
$GroupId = (Get-Team -DisplayName $Team.'TeamDesc').GroupId 

#Get Team user info, select values, and export to destinationFile.csv    
Get-TeamUser -GroupId $GroupId -Role 'Member' | Select-Object 'User','Name',$teamName |  Export-CSV -NoTypeInformation "exportFile.csv" -Append
}

Is there a way to apply the "TeamDesc" info to each of the users it gathers from the respective Teams?

1 Answers

Without having experiences with Teams management or access to a tenant to test and assuming that I've got what you meant you can use a [PSCustomObject] to combine the results of two or more queries in one output object.

$Teams = Import-CSV -Path '.\importFile.csv'
$Result = 
ForEach ($Team in $Teams) {
    $GroupId = (Get-Team -DisplayName $Team.'TeamDesc').GroupId 
    $TeamUserList = Get-TeamUser -GroupId $GroupId -Role 'Member' 
    foreach ($TeamUser in $TeamUserList) {
        [PSCustomObject]@{
            TeamName = $Team.TeamDesc
            GroupId  = $GroupId
            User     = $TeamUser.User
            Name     = $TeamUser.Name
        }
    }
}

$Result | 
    Export-CSV -Path '.\exportFile.csv' -NoTypeInformation  
Related