Add-FullAccessPermission.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
function Add-FullAccessPermission


{
  <#
      .SYNOPSIS
      Adds FullAccess to target user
      .EXAMPLE
      Add-FullAccessPermission -enduser steve -targetmailbox bob
      Adds SendAs permission on bob's mailbox for end user steve WITHOUT AutoMapping.
      .EXAMPLE
      Add-FullAccessPermission -enduser steve -targetmailbox bob -AutoMapping
      Adds SendAs permission on bob's mailbox for end user steve WITH AutoMapping
  #>

  param
  (
    [String]
    [Parameter(Mandatory)]
    $enduser,

    [Parameter(Mandatory)]
    [String]
    $targetmailbox,

    [Switch]
    $AutoMapping
  )

  Confirm-ExchangeOnlineConnection -ConnectIfDisconnected
    
  try 
  {
    if ($AutoMapping) {
      Write-Output 'AutoMapping Flag Set. Automapping = $true'
      Add-MailboxPermission `
        -Identity $targetmailbox `
        -User $enduser `
        -AutoMapping $true `
        -AccessRights FullAccess `
        -Verbose
      }

    if (!$AutoMapping) {
      Write-Output 'AutoMapping Flag NOT Set. Automapping = $false'
      Add-MailboxPermission `
        -Identity $targetmailbox `
        -User $enduser `
        -AutoMapping $false `
        -AccessRights FullAccess `
        -Verbose
      }  
  }
  catch 
  {
    Write-Output -InputObject 'Error in adding FullAccess permissions.'
    Return $_.Exception
  }

}