From c54995f9b19452ca41a83a6851fffcff7f4d3fec Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Sun, 22 Mar 2020 15:54:54 +0000 Subject: [PATCH] Show user their teams instead of passing ID as parameter Signed-off-by: Luke Tainton --- Import-TeamsUsers.psm1 | 59 ++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/Import-TeamsUsers.psm1 b/Import-TeamsUsers.psm1 index c6c35b5..bad58ba 100644 --- a/Import-TeamsUsers.psm1 +++ b/Import-TeamsUsers.psm1 @@ -6,33 +6,29 @@ Function Import-TeamsUsers { .DESCRIPTION Import-TeamsUsers is a Powershell function that will enrol users from a CSV file into a given Microsoft Teams group. - It has two required parameters (switches): -GroupId and -File. + It has two required parameters (switches): -Email and -File. - .PARAMETER GroupId - Specifies the unique identified for the destination Team. This can be retrieved from Powershell by executing the Get-Team cmdlet. + .PARAMETER Email + Your Office 365 email address. This is used to list your teams. .PARAMETER File The path to the CSV file that contains your users. Can either be an absolute path or relative path. .EXAMPLE - Import-TeamsUsers -GroupId "00000000-0000-0000-0000-000000000000" -File "users.csv" + Import-TeamsUsers -Email "user@domain.com" -File "users.csv" #> Param( - [parameter(Mandatory=$true, position=0, ParameterSetName='Params', HelpMessage="Specify Group ID")] - [string]$GroupId, + [parameter(Mandatory=$true, position=0, ParameterSetName='Params', HelpMessage="Specify your Office 365 email address")] + [string]$Email, [parameter(Mandatory=$true, position=1, ParameterSetName='Params', HelpMessage="Specify CSV file")] [string]$File ) Begin { + $ErrorActionPreference = 'Stop' ##### IMPORT CSV FILE ##### - If ($File) { - $Users = Import-CSV $File - } Else { - Write-Host -ForegroundColor Red "CSV file not specified or does not exist." - Exit - } + $Users = Import-CSV $File ##### CHECK MODULE IS INSTALLED AND IMPORTED ##### if (Get-Module -ListAvailable -Name MicrosoftTeams) { @@ -42,35 +38,29 @@ Function Import-TeamsUsers { Write-Host -ForegroundColor Red "Module MicrosoftTeams doesn't exist. Please run 'Install-Module -Name MicrosoftTeams' and retry." Exit } - - ##### CHECK TEAM EXISTS ##### - Try { - $Team = Get-Team -GroupId $GroupId - If ($Team) { - $TeamName = $Team.DisplayName - Write-Host -ForegroundColor Green "Team $TeamName exists!" - } - } Catch [System.UnauthorizedAccessException] { - # User is not authenticated or does not have access - Write-Host -ForegroundColor Red "You do not have access to manage this team." - Exit - } Catch [System.Net.Http.HttpRequestException] { - # Team does not exist - Write-Host -ForegroundColor Red "Team with ID $GroupId does not exist." - Exit - } - } + Process { - $UsersAdded = 0; + ##### GET USER'S TEAMS ##### + Get-Team -User $Email | Select-Object -Property GroupId, DisplayName | Format-Table -AutoSize + $GroupId = Read-Host -Prompt "Paste the GroupId of the desired group" + + ##### ENROL USERS ##### + $global:UsersAdded = 0; $UserCount = $Users | Measure-Object | Select-Object -expand count $Consent = Read-Host -Prompt "You are about to add $UserCount users. Are you sure? [y/N]" If ($Consent -eq "y" -Or $Consent -eq "Y") { $Users | ForEach-Object { $User = $_.email $Role = $_.role - Write-Host "Adding user $User with role $Role" - If (Add-TeamUser -GroupId $GroupId -Role $Role -User $User -ErrorAction SilentlyContinue) {$global:UsersAdded++} + Try { + Add-TeamUser -GroupId $GroupId -Role $Role -User $User + Write-Host "Added user $User with role $Role" + $global:UsersAdded++ + } Catch [Microsoft.TeamsCmdlets.PowerShell.Custom.ErrorHandling.ApiException] { + Write-Host -ForegroundColor Red "Error adding user $User with role $Role" + } + } } Else { Write-Host -ForegroundColor Red "Aborting." @@ -79,6 +69,7 @@ Function Import-TeamsUsers { } End { - Write-Host -ForegroundColor Green "$UsersAdded added successfully." + Write-Host -ForegroundColor Green "$global:UsersAdded users added successfully." + Disconnect-MicrosoftTeams } }