I am using the script below to populate multiple Teams from a .csv:
# Read team users from CSV file
$TeamUsers = Import-CSV "Import_Path.csv"
$i = 0
# Group the objects by their TeamDesc property, so we only query each group once
$TeamUsers | Group-Object TeamDesc | ForEach-Object {
#Get the GroupId of this Group
$groupId = (Get-Team -DisplayName $_.Name).GroupId
#If the Group couldn't be found, just skip below logic
if(-not $groupId) { return }
#If the Group could be found, create a hashtable for future splatting
$params = @{ GroupId = $groupId }
#now we can enumerate each object in each group of objects
foreach($user in $_.Group){
try {
# create a hashtable for splatting progress
$progress = @{
PercentComplete = $i / $TeamUsers.Count * 100
Activity = 'Adding Users to MS Teams!!'
Status = 'Working on Team: "{0}" and User: "{1}"' -f $_.Name, $user.UserPrincipalName
}
Write-Progress @progress
# add this user with this role to the hashtable
$params['User'] = $user.UserPrincipalName
$params['Role'] = $user.Role
Add-TeamUser @params
}
catch {
('Error occurred for {0} - {1}' -f $user.TeamName, $user.UserPrincipalName),
$_.ToString() | Write-Warning
}
}
}
I would like to include the ability to export a report of the actions taken during the run. Ideally, at the end of the script, I would be able to collect a file that describes which users have been added to which Teams. Currently, the script will display actions in the PowerShell interface with the Write-Progress
line, but I would like to be able to have a tangible report as well.
CodePudding user response:
Not sure if there is an easier way, but I would create a new object containing the relevant information, and then at the end of the process, export a new CSV.
So at the start, declare a new array:
$AllActions = @()
Then, after the "Add-TeamUser" line, add these in:
$ActionObj = New-Object PSObject
$ActionObj | Add-Member NoteProperty -Name GroupID -value $GroupID
$ActionObj | Add-Member NoteProperty -Name User -Value $user.UserPrincipalName
$ActionObj | Add-Member NoteProperty -Name Role -Value $User.Role
$AllActions = $ActionObj
Now, down at the end, add in:
$AllActions | Export-csv .\AllActionsTaken.csv
So the final script would be:
# Read team users from CSV file
$AllActions = @()
$TeamUsers = Import-CSV "Import_Path.csv"
$i = 0
# Group the objects by their TeamDesc property, so we only query each group once
$TeamUsers | Group-Object TeamDesc | ForEach-Object {
#Get the GroupId of this Group
$groupId = (Get-Team -DisplayName $_.Name).GroupId
#If the Group couldn't be found, just skip below logic
if(-not $groupId) { return }
#If the Group could be found, create a hashtable for future splatting
$params = @{ GroupId = $groupId }
#now we can enumerate each object in each group of objects
foreach($user in $_.Group){
try {
# create a hashtable for splatting progress
$progress = @{
PercentComplete = $i / $TeamUsers.Count * 100
Activity = 'Adding Users to MS Teams!!'
Status = 'Working on Team: "{0}" and User: "{1}"' -f $_.Name, $user.UserPrincipalName
}
Write-Progress @progress
# add this user with this role to the hashtable
$params['User'] = $user.UserPrincipalName
$params['Role'] = $user.Role
Add-TeamUser @params
$ActionObj = New-Object PSObject
$ActionObj | Add-Member NoteProperty -Name GroupID -value $GroupID
$ActionObj | Add-Member NoteProperty -Name User -Value $user.UserPrincipalName
$ActionObj | Add-Member NoteProperty -Name Role -Value $User.Role
$AllActions = $ActionObj
}
catch {
('Error occurred for {0} - {1}' -f $user.TeamName, $user.UserPrincipalName),
$_.ToString() | Write-Warning
}
}
}
$AllActions | Export-csv .\AllActioonsTaken.csv