<#
.SYNOPSIS
Adds full mailbox permissions and send as permissions to users and mailboxes you specify in a CSV file. There is no option for one or the other, this script
will always do both and only do full access and send. I might upgrade this in future versions if I find some scripts out there that will do this better.
The CSV must contain two headers UPN and Mailbox.
One to One mapping user to mailbox. You will need to add another entry to add same user to another mailbox or multiple users to the same mailbox.
Example CSV
UPN,Mailbox
lance@me.com,FirstMailbox@me.com
lance@me.com,SecondMailbox@me.com
kulwinder@me.com,FirstMailbox@me.com
***YOU MUST INPUT THE PATH TO THE CSV FILE IN THE CMDLET!***
.NOTES
Name: Add-FullAndSendToMailboxFromCSV.ps1
Author: Lance Lingerfelt
Version: 1.0
Modify Date: 2024-07-15
Parameter Values:
$CSVPath is manditory and sets the path where your input file with your user and mailbox values CSV is located.
Log File Location:
Path = "C:\BaringsScripts\Logs\Add-FullAndSendToMailboxFromCSV/<MM>/<DD>/"
Name = "<RunTimeDate_nnn>.log"
.EXAMPLE
Run the Script with the Retention Policy as Default MRM Policy
.\Add-FullAndSendToMailboxFromCSV.ps1 -CSVPath 'C:\Temp\filename.csv'
#>
[CmdletBinding(SupportsShouldProcess = $true)]
Param(
[Parameter(Mandatory = $true)]
[string] $CSVPath
)
# ================================================
# DO NOT MODIFY BEGIN
# ================================================
$ErrorActionPreference = 'SilentlyContinue'
$Date = Get-Date -Format "MM/dd/yyyy"
# Set Logging Configuration
$Log = [PSCustomObject]@{
Path = "C:\BaringsScripts\Logs\Add-FullAndSendToMailboxFromCSV"
Name = "$($Date).log"
}
# ================================================
# DO NOT MODIFY END
# ================================================
# ================================================
# SCRIPT BEGIN
# ================================================
# Create New Logger Instance if Enabled
if ($PSCmdlet.ShouldProcess("Create New Logger Instance", $Log.Path)) {
# Import Logger Module
try {
if ( -not (Get-Module -Name PoShLog -ListAvailable) ) {
Install-Module -Name PoShLog -Scope CurrentUser -Force
}
else {
Import-Module -Name PoShLog -Force
}
}
catch {
Write-Host -Object "Unable to import logger module. Error: $($_.Exception.Message)"
exit 1
}
# Create New Logger Instance. Verbose logging level. Log to file and console. Start Logger.
New-Logger | `
Set-MinimumLevel -Value Verbose | `
Add-SinkFile -Path "$($Log.Path)\$($Log.Name)" -OutputTemplate `
'{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}' -RollingInterval Day | `
Add-SinkConsole | `
Start-Logger
# Log Start of Script
Write-VerboseLog "Start of Script."
Write-Host "Start of Script." -ForegroundColor DarkGreen
}
if ($PSCmdlet.ShouldProcess("Create New Exchange Online Instance", $Log.Path)) {
# Import ExchangeOnlineManagement Module
try {
if ( -not (Get-Module -Name ExchangeOnlineManagement -ListAvailable) ) {
Install-Module -Name ExchangeOnlineManagement -Scope CurrentUser -Force
}
else {
Import-Module -Name ExchangeOnlineManagement -Force
}
}
catch {
Write-Host -Object "Unable to import ExchangeOnlineManagement module. Error: $($_.Exception.Message)"
Write-VerboseLog "Unable to import ExchangeOnlineManagement module. Error: $($_.Exception.Message)"
exit 1
}
}
#Connect Exchange Online
Write-Host "Connecting To Exchange Online" -ForegroundColor Green
Write-VerboseLog "Connecting To Exchange Online"
Connect-ExchangeOnline -ShowBanner:$False
#Import the CSV file form the cmdlet input
Write-Host "Importing CSV Values" -ForegroundColor Green
Write-VerboseLog "Importing CSV Values"
$MailboxIds = Import-CSV -Path $CSVPath
#Add the Full Mailbox Access and Send-As Permissions based on the data in the CSV file.
Foreach ($MailboxId in $MailboxIds) {
# Check if the user specified in Group exists
$userExists = @()
$userExists += Get-User -Identity $MailboxId.UPN -ErrorAction SilentlyContinue -Resultsize Unlimited
$userExists += Get-Contact -Identity $MailboxId.UPN -ErrorAction SilentlyContinue -ResultSize Unlimited
# Check if the distribution group specified in GroupName exists
$mailboxExists = Get-Mailbox -Identity $MailboxID.Mailbox -ErrorAction SilentlyContinue
if ($userExists -and $mailboxExists) {
#Set the Warning variable
#$warnvar = "This is the warning variable"
# Both user and group exist, so proceed with updating the permissions
try {
Write-Host "Adding Full Mailbox Access Permission for User: [$($MailboxId.UPN)] to mailbox: [$($mailboxId.Mailbox)]" -ForegroundColor Green
Write-VerboseLog "Adding Full Mailbox Access Permission for User: [$($MailboxId.UPN)] to mailbox: [$($mailboxId.Mailbox)]"
Add-MailboxPermission -Identity $MailboxId.Mailbox -User $MailboxId.UPN -AccessRights FullAccess -AutoMapping:$True -ErrorAction SilentlyContinue -WarningVariable warnvar
if ($warnvar) {
Write-VerboseLog "You had a warning: $($warnvar) Recorded in the Log File in: $($Log.Path)"
}
else {
Write-VerboseLog "No Warnings Recorded"
}
}
catch {
Write-Host "Failed to add full access permission for $($MailboxId.UPN) Error: $($_.Exception.Message)" -ForegroundColor Red
Write-VerboseLog "Failed to add full access permission for $($MailboxId.UPN) Error: $($_.Exception.Message)"
}
try {
Write-Host "Adding Send-As Permission for User: [$($MailboxId.UPN)] to mailbox: [$($MailboxId.Mailbox)]" -ForegroundColor Cyan
Write-VerboseLog "Adding Send-As Permission for User: [$($MailboxId.UPN)] to mailbox: [$($MailboxId.Mailbox)]"
Add-RecipientPermission -Identity $MailboxId.Mailbox -AccessRights SendAs -Trustee $MailboxId.UPN -Confirm:$False -ErrorAction SilentlyContinue -WarningVariable warnvar | Out-Null
if ($warnvar) {
Write-VerboseLog "You had a warning: $($warnvar) Recorded in the Log File in: $($Log.Path)"
}
else {
Write-VerboseLog "No Warnings Recorded"
}
}
catch {
Write-Host "Failed to add Send-As permission for [$($MailboxId.UPN)] Error: $($_.Exception.Message)" -ForegroundColor Red
Write-VerboseLog "Failed to add Send-As permission for [$($MailboxId.UPN)] Error: $($_.Exception.Message)"
}
}
elseif (-not $mailboxExists) {
# Mailbox doesn't exist, display a message
Write-Host "The mailbox [$($MailboxId.Mailbox)] specified in Mailbox column doesn't exist. Skipping." -ForegroundColor DarkRed
Write-VerboseLog "The mailbox [$($MailboxId.Mailbox)] specified in Mailbox column doesn't exist. Skipping."
}
elseif (-not $userExists) {
# User doesn't exist, display a message
Write-Host "User [$(MailboxID.UPN)] specified in User column doesn't exist. Skipping." -ForegroundColor DarkRed
Write-VerboseLog "User [$(MailboxID.UPN)] specified in User column doesn't exist. Skipping."
}
}
#Create an array of mailbox identity values to use for cmdlets below.
$mbxlist = @()
foreach ($MailboxId in $MailboxIds){
$mbxlist += $mailboxId.Mailbox
}
#Get the final list of mailbox vaules that are unique
$finalmbxlist =@()
$finalmbxlist += $mbxlist | Get-Unique
#Show the mailbox permissions of each mailbox upon completion of adding the users to each mailbox
foreach ($finalmbx in $finalmbxlist) {
Write-Host "Showing Final Mailbox Permissions for Mailbox: [$($finalmbx)]" -ForegroundColor Green
Write-VerboseLog "Showing Final Mailbox Permissions for Mailbox: [$($finalmbx)]"
Get-MailboxPermission -Identity $finalmbx | Format-Table -AutoSize -Wrap
#Show the Send-As permissions for each mailbox upon completion of adding the users to each mailbox
Write-Host "Showing Final Send-As Permissions for Mailbox: [$($finalmbx)]" -ForegroundColor Cyan
Write-VerboseLog "Showing Final Send-As Permissions for Mailbox: [$($finalmbx)]"
Get-RecipientPermission -Identity $finalmbx | Format-Table -AutoSize -Wrap
}
# Disconnect from Microsoft 365 PowerShell sessions
Write-VerboseLog "Disconnecting Exchange Online module"
Write-Host "Disconnecting Exchange Online module" -ForegroundColor Red
Disconnect-ExchangeOnline -Confirm:$False
Write-VerboseLog "End of Script"
Write-Host "End of Script" -ForegroundColor DarkRed
# ================================================
# SCRIPT END
# ================================================