Skip to content

Commit

Permalink
Moved to Set-CIPPForwarding Function
Browse files Browse the repository at this point in the history
Moved ExecEmailForwarding to use Set-CIPPForwarding
Removed the need for ExecDisableEmailForward
  • Loading branch information
BNWEIN committed Jun 27, 2024
1 parent 531d24f commit 46671ff
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 44 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Function Invoke-ExecEmailForward {
$ForwardingSMTPAddress = $request.body.ForwardExternal
$DisableForwarding = $request.body.disableForwarding
$APIName = $TriggerMetadata.FunctionName
[bool]$KeepCopy = if ($request.body.keepCopy -eq "true") { $true } else { $false }

if ($ForwardingAddress) {
try {
New-ExoRequest -tenantid $TenantFilter -cmdlet 'Set-mailbox' -cmdParams @{Identity = $Username; ForwardingAddress = $ForwardingAddress ; DeliverToMailboxAndForward = [bool]$request.body.keepCopy } -Anchor $username
Set-CIPPForwarding -userid $username -tenantFilter $TenantFilter -APIName $APINAME -ExecutingUser $request.headers.'x-ms-client-principal' -Forward $ForwardingAddress -keepCopy $KeepCopy
if (-not $request.body.KeepCopy) {
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Set Forwarding for $($username) to $($ForwardingAddress) and not keeping a copy" -Sev 'Info' -tenant $TenantFilter
$results = "Forwarding all email for $($username) to $($ForwardingAddress) and not keeping a copy"
Expand All @@ -36,7 +37,7 @@ Function Invoke-ExecEmailForward {

elseif ($ForwardingSMTPAddress) {
try {
New-ExoRequest -tenantid $TenantFilter -cmdlet 'Set-mailbox' -cmdParams @{Identity = $Username; ForwardingSMTPAddress = $ForwardingSMTPAddress ; DeliverToMailboxAndForward = [bool]$request.body.keepCopy } -Anchor $username
Set-CIPPForwarding -userid $username -tenantFilter $TenantFilter -APIName $APINAME -ExecutingUser $request.headers.'x-ms-client-principal' -forwardingSMTPAddress $ForwardingSMTPAddress -keepCopy $KeepCopy
if (-not $request.body.KeepCopy) {
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Set forwarding for $($username) to $($ForwardingSMTPAddress) and not keeping a copy" -Sev 'Info' -tenant $TenantFilter
$results = "Forwarding all email for $($username) to $($ForwardingSMTPAddress) and not keeping a copy"
Expand All @@ -54,7 +55,7 @@ Function Invoke-ExecEmailForward {

elseif ($DisableForwarding -eq 'True') {
try {
New-ExoRequest -tenantid $TenantFilter -cmdlet 'Set-Mailbox' -cmdParams @{Identity = $Username; ForwardingAddress = $null; ForwardingSMTPAddress = $null; DeliverToMailboxAndForward = $false }
Set-CIPPForwarding -userid $username -username $username -tenantFilter $Tenantfilter -ExecutingUser $ExecutingUser -APIName $APIName -Disable $true
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Disabled Email forwarding for $($username)" -Sev 'Info' -tenant $TenantFilter
$results = "Disabled Email Forwarding for $($username)"
} catch {
Expand Down
6 changes: 5 additions & 1 deletion Modules/CIPPCore/Public/Invoke-CIPPOffboardingJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ function Invoke-CIPPOffboardingJob {
Set-CIPPOutOfOffice -tenantFilter $tenantFilter -userid $username -InternalMessage $Options.OOO -ExternalMessage $Options.OOO -ExecutingUser $ExecutingUser -APIName $APIName -state 'Enabled'
}
{ $_.'forward' -ne '' } {
Set-CIPPForwarding -userid $userid -username $username -tenantFilter $Tenantfilter -Forward $Options.forward -KeepCopy [bool]$Options.keepCopy -ExecutingUser $ExecutingUser -APIName $APIName
if (!$options.keepcopy) {
Set-CIPPForwarding -userid $userid -username $username -tenantFilter $Tenantfilter -Forward $Options.forward -ExecutingUser $ExecutingUser -APIName $APIName
} else {
Set-CIPPForwarding -userid $userid -username $username -tenantFilter $Tenantfilter -Forward $Options.forward -KeepCopy $Options.keepCopy -ExecutingUser $ExecutingUser -APIName $APIName
}
}
{ $_.'RemoveLicenses' -eq 'true' } {
Remove-CIPPLicense -userid $userid -username $Username -tenantFilter $Tenantfilter -ExecutingUser $ExecutingUser -APIName $APIName
Expand Down
18 changes: 12 additions & 6 deletions Modules/CIPPCore/Public/Set-CIPPForwarding.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ function Set-CIPPForwarding {
[bool]$Disable
)


try {
if (!$username) { $username = $userid }
if ($PSCmdlet.ShouldProcess($username, 'Set forwarding')) {
$null = New-ExoRequest -tenantid $tenantFilter -cmdlet 'Set-mailbox' -cmdParams @{Identity = $userid; ForwardingSMTPAddress = $forwardingSMTPAddress; ForwardingAddress = $Forward ; DeliverToMailboxAndForward = [bool]$KeepCopy } -Anchor $username
}
if (!$Disable) {
$Message = "Forwarding all email for $username to $Forward"
} else {
$Message = "Disabled forwarding for $username"
if ($Disable -eq $true) {
Write-Output "Disabling forwarding for $username"
$null = New-ExoRequest -tenantid $tenantFilter -cmdlet 'Set-mailbox' -cmdParams @{Identity = $userid; ForwardingSMTPAddress = $null; ForwardingAddress = $null ; DeliverToMailboxAndForward = $false } -Anchor $username
$Message = "Disabled forwarding for $username"
} elseif ($Forward) {
$null = New-ExoRequest -tenantid $tenantFilter -cmdlet 'Set-mailbox' -cmdParams @{Identity = $userid; ForwardingSMTPAddress = $null; ForwardingAddress = $Forward ; DeliverToMailboxAndForward = $KeepCopy } -Anchor $username
$Message = "Forwarding all email for $username to Internal Address $Forward and keeping a copy set to $KeepCopy"
} elseif ($forwardingSMTPAddress) {
$null = New-ExoRequest -tenantid $tenantFilter -cmdlet 'Set-mailbox' -cmdParams @{Identity = $userid; ForwardingSMTPAddress = $forwardingSMTPAddress; ForwardingAddress = $null ; DeliverToMailboxAndForward = $KeepCopy } -Anchor $username
$Message = "Forwarding all email for $username to External Address $ForwardingSMTPAddress and keeping a copy set to $KeepCopy"
}
}
Write-LogMessage -user $ExecutingUser -API $APIName -message $Message -Sev 'Info' -tenant $TenantFilter
return $Message
Expand Down

0 comments on commit 46671ff

Please sign in to comment.