-
Notifications
You must be signed in to change notification settings - Fork 0
/
letMeRemove.ps1
executable file
·71 lines (58 loc) · 2.26 KB
/
letMeRemove.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# TODO:
# Paramaters
# check if it works on windows
$moduleGet = Get-Module -ListAvailable -Name PowerShellGet
$moduleTeams = Get-Module -ListAvailable -Name MicrosoftTeams
if (!($moduleGet -and $moduleTeams)) {
Write-Host "Trying to install modules..."
Install-Module -Name PowerShellGet -Force -AllowClobber
Get-Module -ListAvailable -Name PowerShellGet
# Recheck if the modules are installed correctly
$moduleGet = Get-Module -ListAvailable -Name PowerShellGet
$moduleTeams = Get-Module -ListAvailable -Name MicrosoftTeams
if (-not ($moduleGet -and $moduleTeams)) {
Write-Host "Failed to install module PowerShellGet and/or MicrosoftTeams.`nTry do it yourself by:`nInstall-Module -Name PowerShellGet -Force -AllowClobber`nInstall-Module -Name MicrosoftTeams -Force -AllowClobber"
Exit
}
}
# Connect and get the email.
Write-Host "Log-in in your browser."
$acc = Connect-MicrosoftTeams | Select-Object -ExpandProperty "Account"
if (-not $acc) {
Write-Host "Impossible to connect to the Microsoft Account"
Exit
}
Write-Host "Logged in with:" $acc
$all_teams = Get-Team -User $acc
# List all the team enrolled
for ($i = 0; $i -lt $all_teams.Length; $i++) {
Write-Output "$(($i+1))) $($all_teams[$i].DisplayName)"
}
# choice of
$choiceMulti = Read-Host "Do you want to remove from one team or multiple teams?`n(0 one team, 1 multiple teams)"
while ($true) {
if ($choiceMulti -eq 0 -or $choiceMulti -eq 1) {
break;
}
else {
Write-Host "Input invalid."
$choiceMulti = Read-Host "Do you want to remove from one team or multiple teams?`n(0 one team, 1 multiple teams)"
}
}
if ($choiceMulti -eq 0) {
$choiceTeam = Read-Host "Select the team"
$groupId = $all_teams[($choiceTeam - 1)].GroupId
Remove-TeamUser -GroupId $groupId -User $acc
}
if ($choiceMulti -eq 1) {
$choiceTeams = Read-Host "Select the teams, separeted by ','"
$teams = $choiceTeams -split ','
# Write-Host $teams
for ($i = 0; $i -lt $teams.Count; $i++) {
$groupId = $all_teams[($teams[$i])-1].GroupId
Remove-TeamUser -GroupId $groupId -User $acc
Write-Host "Removed from:" $all_teams[($teams[$i])-1].DisplayName
}
}
Write-Host "Disconnecting from Teams..."
Disconnect-MicrosoftTeams