# Created By Alberto Igbinedion
#
# Addding Send As Permission Allows user to be able to send on behalf 
# Adding Full Permission Allow users to own act as owners of the mailbox

# Supply CSV Path Else Enter for Single User
Write-Host "Please Enter" -NoNewline
Write-Host " CSV Path For Multiple Users: " -ForegroundColor Yellow -NoNewline
Write-Host " OR Press Enter To Continue: " -ForegroundColor Green -NoNewline
$CSVFile = Read-Host

Write-Host "Enter MailBox To Add Permission: " -ForegroundColor Yellow -NoNewline
$MailBoxToAddPermission = Read-Host

# If CSV Path Is Supplied Import CSV

if($CSVFile)
{
    $UserList = get-content $CSVFile
}

else 
{
    Write-Host "Enter User Who Wants Access: " -ForegroundColor Yellow -NoNewline
    $UserList = @(Read-Host)
}

# Select Full Or Send As Option

Write-Host "Please Enter" -NoNewline
Write-Host " 1 " -ForegroundColor Green -NoNewline
Write-Host "For FullAccess OR" -NoNewline
Write-Host " 2 " -ForegroundColor Green -NoNewline
Write-Host "For Send As Right: " -NoNewline
$Option = Read-Host


# Looping through The Selected Options And then Adding Members

While ($Option -ge 3)
{
Write-Host "This is not a valid Entry, please enter either 1 or 2"

$Option = Read-Host "Please Enter The Correct Option ? 1=Full Permission, 2=Send As Permission"
}

If ($Option -eq 1)
{ 

foreach($User in $usersList) {
    Add-MailboxPermission -Identity $MailBoxToAddPermission -User $user -AccessRights Full -InheritanceType all
}

}elseif ($Option -eq 2) 
 { 
 
 foreach($User in $UserList) {

 Add-ADPermission -Identity $MailBoxToAddPermission -User $User -AccessRights 'ExtendedRight' -ExtendedRights 'send as'

}


}


#NewLine To Create Space

"`n"

# Check Who Are Members

Write-Host "Below Are Current List of Permissions For $MailBoxToAddPermission  " -ForegroundColor Green

# Check Permission has been applied to mailbox
Get-MailboxPermission -Identity $MailBoxToAddPermission | fl user, AccessRights

# Set GrantSendOnbehalf for a user
# Set-DistributionGroup -Identity Outlook_Feedback@ons.gov.uk -GrantSendOnBehalfTo igbina

# Remove MAilbox Permissions
# Remove-MailboxPermission -Identity "MAilBoxToRemove" -User "$users" -accessright full
  
  
  
  # Setup MailEnabled Security Group "Outlook_feedback"
  # Create Shared Mailbox "Outlook.feedback"
  # Add Users to Security Groups
  # Grant Security Group 'Full' Permission and 'Send as' Permission
  # Then do the Below......
        
        # Adding SendOnBehalf Permission to users
# $a = Get-DistributionGroup "Outlook_feedback"
# $b = Get-DistributionGroupMember outlook_feedback
# $a.GrantSendOnBehalfTo += $b.DistinguishedName
# Set-DistributionGroup "outlook_feedback" -GrantSendOnBehalfTo $a.GrantSendOnBehalfTo
# Get-DistributionGroup "Outlook_feedback" | fl name,grant*

        # Removing
# Get-DistributionGroup "Outlook_feedback" | fl name,grant*
# $a = Get-DistributionGroup "Outlook_feedback"
# $a.GrantSendOnBehalfTo | fl distinguishedname
# $a.GrantSendOnBehalfTo -= "DN"

        # removes all
# Set-DistributionGroup "Outlook_feedback" -GrantSendOnBehalfTo $null 

