Skip to content

Instantly share code, notes, and snippets.

@DoctorD90
Last active February 14, 2021 20:56
Show Gist options
  • Save DoctorD90/8006c2d59e6ab1b6d089f6d5a4386778 to your computer and use it in GitHub Desktop.
Save DoctorD90/8006c2d59e6ab1b6d089f6d5a4386778 to your computer and use it in GitHub Desktop.
PowerShell script with GUI to manage users in Teams Groups in bulk mode
#Set-ExecutionPolicy unrestricted
#Install-Module -Name MicrosoftTeams
Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='MTeams Group Bulk Manager'
#$main_form.Width = 300
#$main_form.Height = 400
$main_form.AutoSize = $true
$main_form.AutoScale = $true
$main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
## Update the status
function UpdatePB([Int32]$pbval,[String]$text) {
# if ([String]::IsNullOrEmpty($pbval)) {
# if ($progressBarStatus.Value -eq 0) {
# $pbval = 50
# } else {
# $pbval = 0
# }
# }
$progressBarStatus.Value = $pbval
#if ([String]::IsNullOrEmpty($text)) {
if ($text -eq 0) {
if ($pbval -eq 0) {
$text = "Ready"
} else {
$text = "In Progress"
}
}
$LabelStatus.Text = "STATUS: "+$text
}
## Login
function LoginAsCUser() {
$ButtonLogin.Enabled = $false
UpdatePB 50 0
$UPN = whoami /upn
$res = Connect-MicrosoftTeams -AccountId $UPN
if ([string]::IsNullOrEmpty($res)) {
$null = [System.Windows.Forms.MessageBox]::Show("Login Error", "Error", 0,[System.Windows.Forms.MessageBoxIcon]::Error)
$ButtonLogin.Enabled = $true
} else {
$acc = $res.Account.id
$msg = "Successfully loged in as:`n"+$acc
$null = [System.Windows.Forms.MessageBox]::Show($msg, "OK", 0,[System.Windows.Forms.MessageBoxIcon]::Information)
$textBoxGUUID.Enabled = $true
$ButtonGUUID1.Enabled = $true
$ButtonRUN1.Enabled = $true
$textBoxRetrieve.Enabled = $true
$ButtonRetrieve1.Enabled = $true
$LabelLogged.Text = "Logged as: "+$acc
}
UpdatePB 0 0
}
## Create an example file list
function CreateExample() {
UpdatePB 50 0
$filecsvname = Save-File "Select CSV Users List Example"
if (!$filecsvname -eq 0) {
Add-Content -Path $filecsvname -Encoding utf8 -Value "GUUID;MAIL;ROLE;ACTION"
CSVRulez
}
UpdatePB 0 0
}
## Rulez of file list
function CSVRulez() {
$msg = "COLUMNS EXPLANATION:`nGUUID:`tUUID of the group (gatherable from button)`nMAIL:`tThe mail of the user`nROLE:`tAllowed type: Owner, Member`nACTION:`tAllowed type: ADD, REMOVE"
$null = [System.Windows.Forms.MessageBox]::Show($msg, "OK", 0,[System.Windows.Forms.MessageBoxIcon]::Information)
}
## Get UUID of Group
function GetGUUID() {
UpdatePB 50 0
if ($textBoxGUUID.Text.Length -eq 0) {
$guuid = $null
} else {
$guuid = (Get-Team -DisplayName $textBoxGUUID.Text).GroupId
}
if ([string]::IsNullOrEmpty($guuid)) {
$null = [System.Windows.Forms.MessageBox]::Show("There is an error with the Displayed Group Name", "Error", 0,[System.Windows.Forms.MessageBoxIcon]::Error)
$LabelGUUID3.Text = "- None -"
$ButtonGUUID2.Enabled = $false
} else {
$null = [System.Windows.Forms.MessageBox]::Show($msg, "OK", 0,[System.Windows.Forms.MessageBoxIcon]::Information)
$LabelGUUID3.Text = $guuid
$ButtonGUUID2.Enabled = $true
}
UpdatePB 0 0
}
## Save As
function Save-File([String]$titl) {
Add-Type -AssemblyName System.Windows.Forms
$SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$SaveFileDialog.Title = $titl
$SaveFileDialog.CreatePrompt = $true
$SaveFileDialog.OverwritePrompt = $true
$SaveFileDialog.AddExtension = $true
$SaveFileDialog.CheckFileExists = $false
$SaveFileDialog.CheckPathExists = $true
$SaveFileDialog.Filter = 'CSV UTF-8 (Comma delimited) (*.csv)|*.csv'
$SaveFileDialog.DefaultExt = ".csv"
$SaveFileDialog.FileName = "MTeams_UserList"
$SaveFileDialog.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$null = $SaveFileDialog.ShowDialog()
if ($SaveFileDialog.FileNames.Count -eq 0) {
#if ($SaveFileDialog.ShowDialog() -eq "Cancel") {
$null = [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,[System.Windows.Forms.MessageBoxIcon]::Exclamation)
$result = 0
} else {
$result = $SaveFileDialog.FileName
}
$SaveFileDialog.Dispose()
return $result
}
## Load File
function Select-File() {
Add-Type -AssemblyName System.Windows.Forms
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Title = "Select CSV Users List"
$OpenFileDialog.InitialDirectory = [Environment]::GetFolderPath('Desktop')
$OpenFileDialog.CheckFileExists = $true
$OpenFileDialog.Multiselect = $false
$OpenFileDialog.CheckPathExists = $true
$OpenFileDialog.Filter = 'CSV UTF-8 (Comma delimited) (*.csv)|*.csv'
$OpenFileDialog.ValidateNames = $true
$null = $OpenFileDialog.ShowDialog()
if ($OpenFileDialog.FileNames.Count -eq 0) {
#if ($OpenFileDialog.ShowDialog() -eq "Cancel") {
$null = [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,[System.Windows.Forms.MessageBoxIcon]::Exclamation)
$result = 0
} else {
$result = $OpenFileDialog.FileName
}
$OpenFileDialog.Dispose()
return $result
}
function LoadCSVUserList() {
UpdatePB 50 0
$csvfile = Select-File
if ($csvfile -eq 0) {
$LabelRUN1.Text = "- None -"
$ButtonRUN2.Enabled = $false
} else {
$LabelRUN1.Text = $csvfile
$ButtonRUN2.Enabled = $true
}
UpdatePB 0 0
}
function RetrieveUserList() {
UpdatePB 50 0
$textBoxRetrieve.Enabled = $false
$ButtonRetrieve1.Enabled = $false
$filecsvname = Save-File "Save the Channel CSV Users List"
if (!$filecsvname -eq 0) {
$TeamsUsers=Get-TeamUser -GroupId $textBoxRetrieve.Text -ErrorVariable mterror
if ([string]::IsNullOrEmpty($mterror)) {
#Add-Content -Path $filecsvname -Encoding utf8 -Value "GUUID;MAIL;ROLE;ACTION"
$TeamsUsers|Select-Object @{Name='GUUID';Expression={$textBoxRetrieve.Text}},@{Name='MAIL';Expression={$_.User}},@{Name='ROLE';Expression={$_.Role}},@{Name='ACTION';Expression={'ADD'}}|Export-Csv -Path $filecsvname -Delimiter ";" -Encoding UTF8 -NoTypeInformation
$null = [System.Windows.Forms.MessageBox]::Show("User List Retrieved", "OK", 0,[System.Windows.Forms.MessageBoxIcon]::Information)
} else {
$null = [System.Windows.Forms.MessageBox]::Show($mterror.Message, "Error", 0,[System.Windows.Forms.MessageBoxIcon]::Error)
}
}
$textBoxRetrieve.Enabled = $true
$ButtonRetrieve1.Enabled = $true
UpdatePB 0 0
}
function RUNEXE() {
UpdatePB 0 "Initializing"
$ButtonRUN2.Enabled = $false
$myCSVlist=$LabelRUN1.Text
$mypath=Split-Path -Path $myCSVlist -Parent
$myname=(Get-Item $myCSVlist).BaseName
# Configure the path to the log file
$logtxtpath=Join-Path -Path $mypath -ChildPath $myname"_log.txt"
$logcsvpath=Join-Path -Path $mypath -ChildPath $myname"_log.csv"
if (!(Test-Path $logcsvpath)) {Add-Content -Path "$logcsvpath" -Encoding UTF8 -Value "DATE;GUUID;MAIL;ROLE;RESULT"}
$mycsv=Import-Csv -Path $myCSVlist -Delimiter ";" -Encoding UTF8
# Count list length
$totallines=$mycsv.Count
$padzero=([string]$totallines).Length
# Get, Print and Log general info
$mydate=Get-Date -Format "yyyy/MM/dd HH:mm K"
$startlog="##################################################`nDATE:`t$mydate`nCSV:`t$myCSVlist`nLOGS:`t$logcsvpath`nLOGS:`t$logtxtpath"
Write-Host "$startlog"
Add-Content -Path "$logtxtpath" -Encoding UTF8 -Value "$startlog"
# Index of current position
$i=0
# Number of succeeded
$succi=0
# Number of errors
$erri=0
$mycsv | foreach{
# Teams Group
$uguuid=$_.GUUID
# Mail
$umail=$_.MAIL
# Role
$urole=$_.ROLE
# ACTION
$uaction=$_.ACTION
# Set up index template
$i=$i+1
$counter=([string]$i).PadLeft($padzero,'0')
$mytext="$counter/$totallines"
Write-Host "`n$mytext - User: $umail"
$ii = [math]::floor(($i/$totallines)*100)
UpdatePB $ii $mytext
if ($uaction -eq "ADD") {
# Add User to the team
Add-TeamUser -GroupId $uguuid -Role $urole -user $umail -ErrorVariable adderror
$aact="ADDED"
} else {
# Delete User
Remove-TeamUser -GroupId $uguuid -user $umail -ErrorVariable adderror
$aact="DELETED"
$urole="removed"
}
# Check brutally if there is any error
if ([string]::IsNullOrEmpty($adderror)) {
# No Error
$succi=$succi+1
Add-Content -Path "$logcsvpath" -Encoding UTF8 -Value "$mydate;$uguuid;$umail;$urole;$aact"
Add-Content -Path "$logtxtpath" -Encoding UTF8 -Value "`n$mytext - User: $umail - $aact"
} else {
# Error
$erri=$erri+1
Add-Content -Path "$logcsvpath" -Encoding UTF8 -Value "$mydate;$uguuid;$umail;$urole;ERROR"
Add-Content -Path "$logtxtpath" -Encoding UTF8 -Value "`n$mytext - User: $umail - ERROR"
$adderror|Out-File -FilePath "$logtxtpath" -Encoding UTF8 -Append
$aact="ERROR"
}
Write-Host $aact
}
$finalres="Total Accounts:`t`t$totallines`nSucceeded:`t`t$(([string]$succi).PadLeft($padzero,' '))`nErrors:`t`t`t$(([string]$erri).PadLeft($padzero,' '))"
Add-Content -Path "$logtxtpath" -Encoding UTF8 -Value "`n`n$finalres`n"
Write-Host "`n`n$finalres`n"
$null = [System.Windows.Forms.MessageBox]::Show($finalres, "OK", 0,[System.Windows.Forms.MessageBoxIcon]::Information)
$LabelRUN1.Text = "- None -"
UpdatePB 0 0
}
######
## STATUS
$progressBarStatus = New-Object System.Windows.Forms.ProgressBar -Property @{
Location = New-Object System.Drawing.Point(0,0)
Size = New-Object System.Drawing.Size(300,20)
#Name = "progressBarStatus"
Value = 0
Style="Continuous"
Anchor='Top,Left,Right'
AutoSize = $true
}
$main_form.Controls.Add($progressBarStatus)
$LabelStatus = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,30)
Text = "STATUS: Ready"
AutoSize = $true
}
$main_form.Controls.Add($LabelStatus)
$Sep1 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,50)
Text = ""
AutoSize = $false
Height = 2
Width = 300
BorderStyle = "Fixed3D"
}
$main_form.Controls.Add($Sep1)
## List Example
$ButtonExample1 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,60)
Text = "File Example"
AutoSize = $true
}
$ButtonExample1.Add_Click({CreateExample})
$main_form.Controls.Add($ButtonExample1)
$ButtonExample2 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(100,60)
Text = "File Help"
AutoSize = $true
}
$ButtonExample2.Add_Click({CSVRulez})
$main_form.Controls.Add($ButtonExample2)
$Sep2 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,90)
Text = ""
AutoSize = $false
Height = 2
Width = 300
BorderStyle = "Fixed3D"
}
$main_form.Controls.Add($Sep2)
## LOGIN
$ButtonLogin = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,90)
Text = "Login as current User"
AutoSize = $true
}
$ButtonLogin.Add_Click({LoginAsCUser})
$main_form.Controls.Add($ButtonLogin)
$LabelLogged = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,120)
Text = "Logged as: - None -"
AutoSize = $true
}
$main_form.Controls.Add($LabelLogged)
$Sep3 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,140)
Text = ""
AutoSize = $false
Height = 2
Width = 300
BorderStyle = "Fixed3D"
}
$main_form.Controls.Add($Sep3)
## GET UUID
$LabelGUUID1 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,150)
Text = "Display Group Name:"
AutoSize = $true
}
$main_form.Controls.Add($LabelGUUID1)
$textBoxGUUID = New-Object System.Windows.Forms.TextBox -Property @{
Location = New-Object System.Drawing.Point(120,150)
Size = New-Object System.Drawing.Size(210,30)
Multiline = $false
AcceptsReturn = $false
ScrollBars = "None"
Text = ""
TextAlign = "Left"
AutoSize = $true
#MaxLength = 36
Enabled = $false
}
$main_form.Controls.Add($textBoxGUUID)
$ButtonGUUID1 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,180)
Text = "Get GUUID"
AutoSize = $true
Enabled = $false
}
$ButtonGUUID1.Add_Click({GetGUUID})
$main_form.Controls.Add($ButtonGUUID1)
$LabelGUUID2 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,210)
Text = "GUUID: "
AutoSize = $true
}
$main_form.Controls.Add($LabelGUUID2)
$LabelGUUID3 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(50,210)
Text = "- None -"
AutoSize = $true
}
$main_form.Controls.Add($LabelGUUID3)
$ButtonGUUID2 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,240)
Text = "Copy GUUID"
AutoSize = $true
Enabled = $false
}
$ButtonGUUID2.Add_Click({Set-Clipboard -Value $LabelGUUID3.Text})
$main_form.Controls.Add($ButtonGUUID2)
$Sep4 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,270)
Text = ""
AutoSize = $false
Height = 2
Width = 300
BorderStyle = "Fixed3D"
}
$main_form.Controls.Add($Sep4)
## Retrieve Info
$LabelRetrieve1 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,280)
Text = "GUUID:"
AutoSize = $true
}
$main_form.Controls.Add($LabelRetrieve1)
$textBoxRetrieve = New-Object System.Windows.Forms.TextBox -Property @{
Location = New-Object System.Drawing.Point(50,280)
Size = New-Object System.Drawing.Size(210,30)
Multiline = $false
AcceptsReturn = $false
ScrollBars = "None"
Text = ""
TextAlign = "Left"
AutoSize = $true
MaxLength = 36
Enabled = $false
}
$main_form.Controls.Add($textBoxRetrieve)
$ButtonRetrieve1 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,310)
Text = "Retrieve Member List"
AutoSize = $true
Enabled = $false
}
$ButtonRetrieve1.Add_Click({RetrieveUserList})
$main_form.Controls.Add($ButtonRetrieve1)
$Sep5 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,340)
Text = ""
AutoSize = $false
Height = 2
Width = 300
BorderStyle = "Fixed3D"
}
$main_form.Controls.Add($Sep5)
## Load List
$ButtonRUN1 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,350)
Text = "CSV User List"
AutoSize = $true
Enabled = $false
}
$ButtonRUN1.Add_Click({LoadCSVUserList})
$main_form.Controls.Add($ButtonRUN1)
$LabelRUN1 = New-Object System.Windows.Forms.Label -Property @{
Location = New-Object System.Drawing.Point(0,380)
Text = "- None -"
AutoSize = $true
}
$main_form.Controls.Add($LabelRUN1)
$ButtonRUN2 = New-Object System.Windows.Forms.Button -Property @{
Location = New-Object System.Drawing.Point(0,410)
Text = "RUN"
AutoSize = $true
Enabled = $false
}
$ButtonRUN2.Add_Click({RUNEXE})
$main_form.Controls.Add($ButtonRUN2)
$main_form.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment