-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddDelegatedPermissions-v2.ps1
62 lines (57 loc) · 1.95 KB
/
AddDelegatedPermissions-v2.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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$File,
[Parameter(Mandatory = $true)]
[string]
$ErrorFile
)
BEGIN {
$ErrorActionPreference = "Stop"
$CSV = Import-Csv $File
function AddGrantSendOnBehalfTo($Mailbox, $GrantTo) {
Set-Mailbox $Mailbox -GrantSendOnBehalfTo @{Add = "$GrantTo" } -ErrorAction Stop | Out-Null
}
function AddSendAs($Mailbox, $GrantTo) {
Add-RecipientPermission $Mailbox -Trustee $GrantTo -AccessRights SendAs -Confirm:$false -ErrorAction Stop | Out-Null
}
function AddFullAs($Mailbox, $GrantTo) {
Add-MailboxPermission $Mailbox -User $GrantTo -AccessRights FullAccess -ErrorAction Stop | Out-Null
}
}
PROCESS {
$Total = $CSV | Measure-Object | Select-Object -ExpandProperty Count
$i = 0
$ErrorList = @()
foreach ($Line in $CSV) {
Write-Progress -Activity "Adding delegated permissions..." -Status "Total: [$i / $Total] | Mailbox: $($Line.SharedMailbox)" -PercentComplete (($i / $Total) * 100)
$i++
$NewEmail = $line.SharedMailbox
$NewUserGrantedPermission = $Line.NewMember
try {
AddSendAs -Mailbox $NewEmail -GrantTo $NewUserGrantedPermission
}
Catch {
$err = $_
Write-Host "Failed to add SendAs on $NewEmail to $NewUserGrantedPermission. Error: $err" -ForegroundColor Red
$ErrorList += $Line
continue
}
try {
AddFullAs -Mailbox $NewEmail -GrantTo $NewUserGrantedPermission
}
Catch {
$err = $_
Write-Host "Failed to add FullAccess on $NewEmail to $NewUserGrantedPermission. Error: $err" -ForegroundColor Red
$ErrorList += $Line
continue
}
}
}
END {
if ($ErrorList) {
$ErrorList | Export-Csv -NoTypeInformation -LiteralPath $ErrorFile
Write-Host "Error file saved to: '$ErrorFile'" -ForegroundColor Red
}
}