-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExchangeToolboxV1.ps1
470 lines (454 loc) · 21.3 KB
/
ExchangeToolboxV1.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
######################################################################################
# Exchange Toolbox V1.0 - Released 26/07/2022 #
# #
# Script Created by ReproDev: https://github.com/reprodev/Exchange-ToolboxV1/ #
# Released Under MIT Licence #
# Check out other projects : https://github.com/reprodev/ #
# Why not buy me a coffee? : https://ko-fi.com/reprodev #
# #
######################################################################################
# Credential Function Loaded before Global Variable
function Update-Creds
{
# This code block will remove the User Credential saved for this session if you've logged in with the wrong credentials
Clear-Host
Write-Host "Re-enter your credentials..."
$global:UserCredential = Get-Credential
#[console]::beep(800,1000)
Write-Host "You're logged back in. Returning to the Previous Menu..."
Pause
}
# ------------------------ LIST OF GLOBAL VARIABLES -----------------------------
# Get-Credential cmdlet and asks for the user to log in with their credentials and then saves them as a secure.securitystring new object
# until the app is closed
$global:UserCredential = Get-Credential
# --------------------------- LIST OF FUNCTIONS -------------------------------
# ------- FUNCTIONS FOR MAILBOX ADMIN --------------
# This function adds a single user to a Mailbox and gives Full Access as the Default permission
function Add-Box-Single
{Clear-Host
[string]$UserNameMailAdd = Read-Host "Enter the display name, email address or AD Username of the USER that needs access to a Mailbox. By Default, they will all be given Full Access to the Mailbox"
[string]$MailBoxAdd = Read-Host "Enter the display name, email address or AD Username of the MAILBOX they need access to"
#$PermissionMailAdd = Read-Host "What kind of permissions does the user need e.g. FullAccess, SendAs"
Show-Session-Global
Add-MailboxPermission -Identity $MailBoxAdd -User $UserNameMailAdd -AccessRights "FullAccess" -InheritanceType All
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function removes fully removes a single user from a mailbox
function Remove-Box-Single
{Clear-Host
[string]$UserNameMailRem = Read-Host "Enter the display name, email address or AD Username of the USER that you want to remove from a Mailbox"
[string]$MailBoxRem = Read-Host "Enter the display name, email address or AD Username of the MAILBOX"
Show-Session-Global
Remove-MailboxPermission -Identity $MailBoxRem -User $UserNameMailRem -AccessRights "FullAccess" -InheritanceType All
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function lists all users who have access to a mailbox
function ShareBoxList
{Clear-Host
[string]$MailboxShCheck = Read-Host "Enter the display name, email address or AD Username of the MAILBOX to list the users who have access and their permission"
Show-Session-Global
Get-MailboxPermission -Identity $MailboxShCheck | Select-Object User,Identity,AccessRights | Format-List
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function lists all the mailboxes a single user has delegate access to and the type of permission formatted in a list
function UserBoxList
{Clear-Host
Write-Host "Please Note: THIS FUNCTION WILL ONLY WORK WITH THEIR EMAIL ADDRESS. Once you have entered the details. This searches all of Exchange and could take up to an hour to complete. A sound will play when the script has finished so please wait."
[string]$MailboxUserCheck = Read-Host "Enter the EMAIL ADDRESS of the USER to list all the mailboxes they have delegate access to"
Show-Session-Global
# Get-Mailbox | Get-MailboxPermission -Identity $MailboxUserCheck | Format-List - Can be used to format this as a list instead of below but will cut off long Display Names and take just as long
# Please note this one will take a long time and may look like it's stalled and show errors of duplicate email address the below [console]beep sound will play when the script has finished so you can do something else while you wait
#Get-Mailbox -RecipientTypeDetails UserMailbox,SharedMailbox -ResultSize Unlimited | Get-MailboxPermission -User $MailboxUserCheck | Format-List
Get-Mailbox -RecipientTypeDetails UserMailbox,SharedMailbox -ResultSize Unlimited | Get-MailboxPermission -User $MailboxUserCheck | Select-Object User,Identity,AccessRights | Format-List
#$global:Sounds
[console]::beep(800,1000)
Pause
Get-PSSession | Remove-PSSession
}
# This function adds multiple mailboxes from a single user in a loop until the user types "N".
# Type in the User once and then the you will be prompted for each Mailbox without having to retype the user
function Add-Box-Multi
{Clear-Host
Show-Session-Global
[string]$UserNameAddMailMulti = Read-Host "Enter the display name, email address or AD Username of the USER you want to add to multiple Mailboxes e.g. Staff Mover"
[string]$UserNameAddMailLoop = 'Do you want to add multiple Mailboxes for this user. By default, they will all be given Full Access to the Mailbox [Y/N]'
do
{
$response = Read-Host -Prompt $UserNameAddMailLoop
if ($response -eq 'y'){[String]$UserNameAddMailMultiBox = Read-Host "Enter the display name, email address or AD Username of the MAILBOX you want to add this user to, e.g. New Team Shared Inbox"
{
}
Add-MailboxPermission -Identity $UserNameAddMailMultiBox -User $UserNameAddMailMulti -AccessRights "FullAccess" -InheritanceType All #The FullAccess can be changed to ReadPermission but you'll usually be adding just as FullAccess which is why this is the default
#$global:Sounds
}
} until ($response -eq 'n')
Pause
Get-PSSession | Remove-PSSession
}
# This function removes multiple mailboxes from a single user in a loop until the user types "N".
# Type in the User once and then the you will be prompted for each Mailbox without having to retype the user
function Remove-Box-Multi
{Clear-Host
Show-Session-Global
[string]$UserNameRemMailMulti = Read-Host "Enter the display name, email address or AD Username of the USER you want to remove from Mailboxes"
[string]$UserNameRemMailLoop = 'Do you want to remove multiple Mailboxes from this user [Y/N]'
do
{
$response = Read-Host -Prompt $UserNameRemMailLoop
if ($response -eq 'y'){$UserNameRemMailMultiBox = Read-Host "Enter the display name, email address or AD Username of the MAILBOX you want to remove from this user, e.g. Previous Team Shared Mailboxes"
{
}
Remove-MailboxPermission -Identity $UserNameRemMailMultiBox -User $UserNameRemMailMulti -AccessRights "FullAccess" -InheritanceType All
#$global:Sounds
}
} until ($response -eq 'n')
Pause
Get-PSSession | Remove-PSSession
}
# ------------ FUNCTIONS FOR CALENDAR ADMINISTRATION --------------
# This function lists all the users that have delegate access to a calendar and the type of permission formatted in a list
function UserCalList
{Clear-Host
[string]$CalendarCheck = Read-Host "Enter the display name, email address or AD Username of the CALENDAR to check delegate permissions for"
Show-Session-Global
Get-MailboxFolderPermission -Identity $CalendarCheck":\calendar"
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function lets you add a single user to a calendar and the type of permissions they need
function Add-Cal-Single
{Clear-Host
[string]$CalendarAdd = Read-Host "Enter the display name, email address or AD Username of the CALENDAR"
[string]$UserNameAdd = Read-Host "Enter the display name, email address or AD Username of the USER that needs access to this calendar"
[string]$PermissionAdd = Read-Host "What kind of permissions does the user need e.g. Owner, Editor, PublishingEditor"
Show-Session-Global
Add-MailboxFolderPermission -Identity $CalendarAdd":\calendar" -User $UserNameAdd -AccessRights $PermissionAdd
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function lets you remove a single user to a calendar and the type of permissions they need
function Remove-Cal-Single
{Clear-Host
[string]$CalendarRem = Read-Host "Enter the display name, email address or AD Username of the CALENDAR"
[string]$UserNameRemove = Read-Host "Enter the display name, email address or AD Username of the USER to remove from this calendar"
Show-Session-Global
Remove-MailboxFolderPermission -Identity $CalendarRem":\calendar" -User $UserNameRemove
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function lets you change a single user's access to a calendar and the type of permissions they have
function Set-Cal-Single
{Clear-Host
[string]$CalendarSet = Read-Host "Enter the display name, email address or AD Username of the CALENDAR"
[string]$UserNameSet = Read-Host "Enter the display name, email address or AD Username of the USER changing access to this calendar"
[string]$PermissionSet = Read-Host "What kind of permissions does the USER need now e.g. Owner, Editor, PublishingEditor"
Show-Session-Global
Set-MailboxFolderPermission -Identity $CalendarSet":\calendar" -User $UserNameSet -AccessRights $PermissionSet
#$global:Sounds
Pause
Get-PSSession | Remove-PSSession
}
# This function removes multiple calendars from a single user in a loop until the user types "N".
# Type in the User once and then the you will be prompted for each Calendar without having to retype the user
function Remove-Cal-Multi
{Clear-Host
Show-Session-Global
[string]$UserNameRemMulti = Read-Host "Enter the display name, email address or AD Username of the USER to remove from calendars e.g. Staff Mover"
[string]$UserNameRemLoop = 'Do you want to remove multiple calendars from this user [Y/N]'
do
{
$response = Read-Host -Prompt $UserNameRemLoop
if ($response -eq 'y'){[String]$UserNameRemMultiCals = Read-Host "Enter the display name, email address or AD Username of the calendar you want to remove e.g. Team Calendar"
{
}
Remove-MailboxFolderPermission -Identity $UserNameRemMultiCals":\calendar" -User $UserNameRemMulti
#$global:Sounds
}
} until ($response -eq 'n')
Pause
Remove-PSSession $Session}
# ---------- Functions for Session Management --------------
# This function starts a session in Exchange Online to carry out the function for carrying out Powershell actions
function Show-Session-Global
{ Clear-Host
# Writes a message on screen to confirm that the login script to create a new session has started
Write-Host "Running the requested script..." -ForegroundColor DarkBlue -BackgroundColor Gray
# This sets the variable Session in the global scope and is the command that you would run before commands usually when you do something with Exchange in Powershell
$global:Session =
New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $global:UserCredential -Authentication "Basic" -AllowRedirection;
# The below line then imports that session using the Variable we have just created to allow us to send the output of this function into the option we need
# This is why the Menu Options contain these functions so this login part can be changed just here if this changes in future
Import-PSSession -Session $global:Session
}
# ---------- Functions for Menus and Submenus ----------
function Show-MainMenu-Toolbox
{
# This is the create function to display the one level flat Menu and the Opt variables are called later for the the Write-Hosts.
# Just change the text in the Double Speech marks and it will change in the menu.
# You can add as many options as you want here make sure you wrap the new string in 2 round brackets above and below.
# Leave Title as it is in the String Variable here and also below as you will call this later in the Do for User Input later on as -Title.
# Each of the code blocks have variables that should relate to the function of that section for easier readibility and to make any
# changes to this script easier in the future.
param (
[string]$MainTitle = "My Menu"
)
(
[string]$MainOpt1 = "Exchange Mailbox Administration"
)
(
[string]$MainOpt2 = "Exchange Calendar Administration"
)
(
[string]$MainOpt3 = "Show All Admin Programs"
)
(
[string]$MainOpt4 = "Update Your Saved Credentials"
)
Clear-Host
Write-Host "================== $MainTitle ==================" -ForegroundColor DarkBlue -BackgroundColor Gray
Write-Host ""
Write-Host " 1: $MainOpt1"
Write-Host " 2: $MainOpt2"
Write-Host " 3: $MainOpt3"
Write-Host " 4: $MainOpt4"
#Write-Host " 5: $MainOpt5"
#Write-Host " S: Turn On/Off Sounds"
Write-Host ""
Write-Host "=======Type 'Q' to Quit The Application========"
}
function Show-SubMenu-Calendar
{
# This is the create function to display the calendar Sub Menu and the CalOpt variables are called later for the the Write-Hosts.
# The Title has also been changed in the Write-Host and Parameter to CalTitle so that this menu has it's own Title
param (
[string]$CalTitle = "My Menu"
)
(
[string]$CalOpt1 = "List all Users that have Delegate Permissions on a Calendar"
)
(
[string]$CalOpt2 = "Add a User to a Calendar"
)
(
[string]$CalOpt3 = "Remove a User from a Calendar"
)
(
[string]$CalOpt4 = "Change a User's Access Permissions to a Calendar"
)
(
[string]$CalOpt5 = "Remove an Individual User From Multiple Calendars"
)
(
[string]$CalOpt6 = "Update Your Saved Credentials"
)
Clear-Host
Write-Host "================== $CalTitle ==================" -ForegroundColor DarkBlue -BackgroundColor Gray
Write-Host ""
Write-Host " 1: $CalOpt1"
Write-Host " 2: $CalOpt2"
Write-Host " 3: $CalOpt3"
Write-Host " 4: $CalOpt4"
Write-Host " 5: $CalOpt5"
Write-Host " 6: $CalOpt6"
#Write-Host " S: Turn On/Off Sounds"
Write-Host ""
}
function Show-SubMenu-Mailbox
{
# This is the create function to display the calendar Sub Menu and the MailOpt variables are called later for the the Write-Hosts.
# The Title has also been changed in the Write-Host and Parameter to MailTitle so that this menu has it's own Title
param (
[string]$MailTitle = "My Menu"
)
(
[string]$MailOpt1 = "Give a User Delegate Access to a Mailbox"
)
(
[string]$MailOpt2 = "Remove a User's Delegate Access to a Mailbox"
)
(
[string]$MailOpt3 = "List all Users that have Delegate access to a Mailbox"
)
(
[string]$MailOpt4 = "List all Mailboxes a User has Delegate Access To"
)
(
[string]$MailOpt5 = "Add an Individual User as Delegate to Multiple Mailboxes"
)
(
[string]$MailOpt6 = "Remove an Individual User as a Delegate From Multiple Mailboxes"
)
(
[string]$MailOpt7 = "Update Your Saved Credentials"
)
Clear-Host
Write-Host "================== $MailTitle ==================" -ForegroundColor DarkBlue -BackgroundColor Gray
Write-Host ""
Write-Host " 1: $MailOpt1"
Write-Host " 2: $MailOpt2"
Write-Host " 3: $MailOpt3"
Write-Host " 4: $MailOpt4"
Write-Host " 5: $MailOpt5"
Write-Host " 6: $MailOpt6"
Write-Host " 7: $MailOpt7"
#Write-Host " S: Turn On/Off Sounds"
Write-Host ""
}
function Show-SubMenu-Full
{
# This is the create function to display all the scripts in a Sub Menu and the FullOpt variables are called later for the the Write-Hosts.
# The Title has also been changed in the Write-Host and Parameter to FullTitle so that this menu has it's own Title
param (
[string]$FullTitle = "My Menu"
)
(
[string]$FullOpt1 = "Give a User Delegate Access to a Mailbox"
)
(
[string]$FullOpt2 = "Remove a User's Delegate Access to a Mailbox"
)
(
[string]$FullOpt3 = "List all Users that have Delegate access to a Mailbox"
)
(
[string]$FullOpt4 = "List all Mailboxes a User has Delegate Access To"
)
(
[string]$FullOpt5 = "Add an Individual User as Delegate to Multiple Mailboxes"
)
(
[string]$FullOpt6 = "Remove an Individual User as a Delegate From Multiple Mailboxes"
)
(
[string]$FullOpt7 = "List all Users that have Delegate Permissions on a Calendar"
)
(
[string]$FullOpt8 = "Add a User to a Calendar"
)
(
[string]$FullOpt9 = "Remove a User from a Calendar"
)
(
[string]$FullOpt10 = "Change a User's Access Permissions to a Calendar"
)
(
[string]$FullOpt11 = "Remove an Individual User From Multiple Calendars"
)
(
[string]$FullOpt12 = "Update Your Saved Credentials"
)
Clear-Host
Write-Host "================== $FullTitle ==================" -ForegroundColor DarkBlue -BackgroundColor Gray
Write-Host ""
Write-Host " 1: $FullOpt1"
Write-Host " 2: $FullOpt2"
Write-Host " 3: $FullOpt3"
Write-Host " 4: $FullOpt4"
Write-Host " 5: $FullOpt5"
Write-Host " 6: $FullOpt6"
Write-Host " 7: $FullOpt7"
Write-Host " 8: $FullOpt8"
Write-Host " 9: $FullOpt9"
Write-Host "10: $FullOpt10"
Write-Host "11: $FullOpt11"
Write-Host "12: $FullOpt12"
#Write-Host " S: Turn On/Off Sounds"
Write-Host ""
}
# --------------------------------- END OF FUNCTION LIST -----------------------------------------------------
#
# Do Loop for Menu containing user switches and a nested looping system
# This also calls the Menus with a changed positional paremeter switch so you can change these next to e.g. "-MainTitle" and "-MailTitle"
# Use a new variable for each menu or it'll loop indefinitely or just have no menu e.g. $MainTile and $MailTitle
# You can add as many options as you want to each layer and then branch out from there
# Functions have been used so they can be called instead of code blocks in the User Switches for easier readibility and editing of the options later
do
{
Clear-Host
Show-MainMenu-Toolbox -MainTitle "Exchange Toolbox v1.0"
$UserMain = Read-Host "Please enter your selection from the list"
#$global:Sounds = [console]::beep(800,1000)
switch ($UserMain)
{
"1" {
do
{
Clear-Host
Show-SubMenu-Mailbox -MailTitle "Microsoft Exchange Mailbox Administration"
$UserSubMail = Read-Host -Prompt 'Enter 1 - 7 or B for Back'
Switch ($UserSubMail)
{
'1' {Add-Box-Single}
'2' {Remove-Box-Single}
'3' {ShareBoxList}
'4' {UserBoxList}
'5' {Add-Box-Multi}
'6' {Remove-Box-Multi}
'7' {Update-Creds}
}
#pause
}
until ($UserSubMail -eq "b")
} "2" {
Clear-Host
do
{
Clear-Host
Show-SubMenu-Calendar -CalTitle "Microsoft Exchange Calendar Administration"
$UserSubCal = Read-Host -Prompt 'Enter 1 - 6 or B for Back'
Switch ($UserSubCal)
{
'1' {UserCalList}
'2' {Add-Cal-Single}
'3' {Remove-Cal-Single}
'4' {Set-Cal-Single}
'5' {Remove-Cal-Multi}
'6' {Update-Creds}
}
#pause
}
until ($UserSubCal -eq "b")
} "3" {
Clear-Host
do
{
Clear-Host
Show-SubMenu-Full -FullTitle "Full List Of Applications"
$UserSubFull = Read-Host -Prompt 'Enter 1 - 12 or B for Back'
Switch ($UserSubFull)
{
'1' {Add-Box-Single}
'2' {Remove-Box-Single}
'3' {ShareBoxList}
'4' {UserBoxList}
'5' {Add-Box-Multi}
'6' {Remove-Box-Multi}
'7' {UserCalList}
'8' {Add-Cal-Single}
'9' {UserBoxList}
'10' {Set-Cal-Single}
'11' {Remove-Cal-Multi}
'12' {Update-Creds}
}
#pause
}
until ($UserSubFull -eq "b")
} "4" {Clear-Host
Update-Creds
}"q" {
return
}
}
#pause
}
until ($UserMain -eq "q")