TeamsAddMembersFromCSVFile.psm1
# TeamsAddMembersFromCSVFile.psm1 # version: 03/10/2020 # author: Walter van Heuven # # PowerShell module with useful functions # # Requires MicrosoftTeams # to install enter: # Install-Module -Name MicrosoftTeams -RequiredVersion 1.1.3-preview -AllowPrerelease -force -AllowClobber # then enter Connect-MicrosoftTeams in the PowerShell and follow the instructions. # #Requires -Modules @{ ModuleName="MicrosoftTeams"; RequiredVersion="1.1.3" } Import-Module -Name MicrosoftTeams -RequiredVersion "1.1.3" # Add-ToTeamFromCSVFile # # Add people to a team or channel within a team # # run script example: Add-ToTeamFromCSVFile students.csv # # Input file requires 2 columns, separated by commas to adds students to a Team. # First line of CSV file should be a header indicating the column names: email, team # # Input file requires 3 columns, separated by commas to adds students to a Team Channel. # First line of CSV file should be a header indicating the column names: email, team, channel # # Column role is optional, default role is Member function Add-ToTeamFromCSVFile { param ( [String] $fileToProcess ) $startTime = Get-Date -DisplayHint Date # Check if input file is provided if ($fileToProcess -eq "") { Write-Error "Input CSV file missing!" return } # Check if file exists and has required columns if (Test-Path -LiteralPath $fileToProcess -PathType Leaf) { Write-Output "Processing CSV file: $fileToProcess" $inputCsvFile = Import-Csv -Path $fileToProcess # check CSV file $colCount = ($inputCsvFile | get-member -type NoteProperty).Count if ($colCount -lt 2) { Write-Error "Error in file: '$fileToProcess', 2 columns needed, found: $colCount." Write-Error "Required columns in the input file: email, team" Write-Error "Additional column needed when adding students to channel: channel" Write-Error "Optional column: role, if missing, role is Member." return } else { # CSV file has at least 2 columns, now check if these have the required names if ($null -eq $inputCsvFile.email -or $null -eq $inputCsvFile.team) { Write-Error "Error in file: '$fileToProcess'." Write-Error "Typo in column name or column name missing." Write-Error "Required columns in the input file: email, team" return } } # $n = 1 $addedTeamMembers = 0 $addedChannelMembers = 0 $currentTeam = "" $currentTeamMembers = @() $currentChannel = "" $currentChannelMembers = @() $changedTeam = 0 $theEmail = "" $theTeam = "" $theChannel = "" $theRole = "" # foreach ($line in $inputCsvFile) { $theEmail = $line.email $theTeam = $line.team # check if Role column is available, if not set Role to Member if ($null -eq $line.role) { $theRole = "Member" } else { if ($line.role -eq "") { $theRole = "Member" } else { $theRole = $line.role } } if ($null -eq $line.channel) { $theChannel = "" } else { $theChannel = $line.channel } # Team if ($currentTeam -ne $theTeam) { try { $grpid = (Get-Team -DisplayName $theTeam).GroupId $currentTeamMembers = (Get-TeamUser -GroupId $grpid).User } catch { Write-Error "Unable to connect to MicrosoftTeams (AzureCloud)." Write-Error "Run first these cmdlets in the PowerShell:" Write-Error "Import-Module -Name MicrosoftTeams -RequiredVersion '1.1.3'" Write-Error "Connect-MicrosoftTeams" return } $currentTeam = $theTeam $changedTeam = 1 } else { $changedTeam = 0 } if ($currentTeamMembers -Contains $theEmail) { # do not warn when adding people to a channel within a team if ($theChannel -eq "") { Write-Warning "Line $n, $theEmail already member of Team: $theTeam" } } else { try { Add-TeamUser -GroupId $grpid -User $theEmail -Role $theRole Write-Output "Line $n, added: $theEmail, role: $theRole to team: $theTeam" } catch { Write-Error "Unable to connect to MicrosoftTeams (AzureCloud)." return } $addedTeamMembers += 1 $currentTeamMembers += $theEmail } # Channel if ($theChannel -ne "") { if (($currentChannel -ne $theChannel) -or ($changedTeam -eq 1)) { try { $currentChannelMembers = (Get-TeamChannelUser -GroupId $grpid -DisplayName $theChannel).User } catch { Write-Error "Unable to connect to MicrosoftTeams (AzureCloud)." return } $currentChannel = $theChannel } if ($currentChannelMembers -Contains $theEmail) { Write-Warning "Line $n, $theEmail already member of channel: $theChannel" } else { try { Add-TeamChannelUser -GroupId $grpid -DisplayName $theChannel -User $theEmail -Role $theRole Write-Output "Line $n, added: $theEmail, role: $theRole to channel: $theChannel in team: $theTeam" } catch { Write-Error "Unable to connect to MicrosoftTeams (AzureCloud)." return } $addedChannelMembers += 1 $currentChannelMembers += $theEmail } } $n += 1 } $endTime = Get-Date -DisplayHint Date Write-Output "Finished processing file: '$fileToProcess'" Write-Output "" Write-Output "Start time: $startTime" Write-Output "End time: $endTime" Write-Output "Total number of people added to Teams: $addedTeamMembers" Write-Output "Total number of people added to Channels: $addedChannelMembers" } else { Write-Error "Input CSV file: '$fileToProcess' does not exist." } } Export-ModuleMember -Function Add-ToTeamFromCSVFile |