From e8c52cfc0a18b8f0f9253b10219d0f4d422ff22f Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Thu, 27 Feb 2020 17:26:22 +0000 Subject: [PATCH] Initial commit Signed-off-by: Luke Tainton --- Import-TeamsUsers.ps1 | 49 +++++++++++++++++++++++++++++++++++++++++++ README.md | 13 ++++++++++++ template.csv | 1 + 3 files changed, 63 insertions(+) create mode 100644 Import-TeamsUsers.ps1 create mode 100644 template.csv diff --git a/Import-TeamsUsers.ps1 b/Import-TeamsUsers.ps1 new file mode 100644 index 0000000..2770576 --- /dev/null +++ b/Import-TeamsUsers.ps1 @@ -0,0 +1,49 @@ +function Import-TeamsUsers { + Param( + [parameter(Mandatory=$true,HelpMessage="Specify Group ID")] + $GroupId, + [parameter(Mandatory=$true,HelpMessage="Specify CSV file")] + $File + ) + + # Import CSV file and required module + $Users = Import-CSV $File + $UserCount = $Users | Measure-Object | Select-Object -expand count + Import-Module -Name MicrosoftTeams + + # 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 + Write-Host -ForegroundColor Red "You need to authenticate to Microsoft Teams before continuing. Please run 'Connect-MicrosoftTeams' and try again." + Break + } Catch [System.Net.Http.HttpRequestException] { + # Team does not exist + Write-Host -ForegroundColor Red "Team with ID $GroupId does not exist!" + Break + } + + $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" + Add-TeamUser -GroupId $GroupId -Role $Role -User $User -ErrorAction SilentlyContinue + } + } Else { + Write-Host -ForegroundColor Red "Aborting." + } + +} + + +$GroupId = "" +$File = "" + +Import-TeamsUsers -GroupId $GroupId -File $File diff --git a/README.md b/README.md index 735c58d..ac79cc3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ # Import-TeamsUsers A Powershell script that imports users from a CSV into a Microsoft Teams team. + +# Setup +You'll need to do a few things before you can run the script: +1. Have a CSV file. This needs to be in the format `email,role`. You can copy the template if required. +2. Install the module. Run `Install-Module -Name MicrosoftTeams` in your Powershell terminal. +3. Import the module. Run `Import-Module -Name MicrosoftTeams` in your Powershell terminal. +4. Authenticate to the API. Run `Connect-MicrosoftTeams` in your Powershell terminal. + +# How to use +Open `Import-TeamsUsers.ps1` and modify the `$GroupId` and `$File` variables at the bottom, then execute the script from the command line. + +# Issues? +If you're having problems or have an idea for a new feature, please check [here](https://github.com/luketainton/Import-TeamsUsers/issues) to see if someone else is having the same problem, and open an issue if one doesn't already exist. If you can implement a fix or feature request, please file a pull request! diff --git a/template.csv b/template.csv new file mode 100644 index 0000000..646d4f2 --- /dev/null +++ b/template.csv @@ -0,0 +1 @@ +email,role \ No newline at end of file