|
| 1 | +function Invoke-ExecScheduleForwardingVacation { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + Exchange.Mailbox.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + $Headers = $Request.Headers |
| 13 | + |
| 14 | + try { |
| 15 | + $TenantFilter = $Request.Body.tenantFilter |
| 16 | + $Users = @($Request.Body.Users) |
| 17 | + $ForwardOption = $Request.Body.forwardOption |
| 18 | + $ForwardInternal = $Request.Body.ForwardInternal.value ?? $Request.Body.ForwardInternal |
| 19 | + $ForwardExternal = $Request.Body.ForwardExternal |
| 20 | + $KeepCopy = if (-not [string]::IsNullOrWhiteSpace($Request.Body.KeepCopy)) { [System.Convert]::ToBoolean($Request.Body.KeepCopy) } else { $false } |
| 21 | + $StartDate = $Request.Body.startDate |
| 22 | + $EndDate = $Request.Body.endDate |
| 23 | + |
| 24 | + $UserUPNs = @($Users | ForEach-Object { $_.addedFields.userPrincipalName ?? $_.value ?? $_ }) |
| 25 | + |
| 26 | + if ($UserUPNs.Count -eq 0) { |
| 27 | + throw 'At least one user is required.' |
| 28 | + } |
| 29 | + |
| 30 | + $UserDisplay = ($UserUPNs | Select-Object -First 3) -join ', ' |
| 31 | + if ($UserUPNs.Count -gt 3) { $UserDisplay += " (+$($UserUPNs.Count - 3) more)" } |
| 32 | + |
| 33 | + $SharedParams = [PSCustomObject]@{ |
| 34 | + TenantFilter = $TenantFilter |
| 35 | + Users = $UserUPNs |
| 36 | + ForwardOption = $ForwardOption |
| 37 | + KeepCopy = $KeepCopy |
| 38 | + APIName = $APIName |
| 39 | + } |
| 40 | + |
| 41 | + switch ($ForwardOption) { |
| 42 | + 'internalAddress' { |
| 43 | + if ([string]::IsNullOrWhiteSpace($ForwardInternal)) { |
| 44 | + throw 'Forwarding target is required for internal forwarding.' |
| 45 | + } |
| 46 | + $SharedParams.ForwardInternal = $ForwardInternal |
| 47 | + $TargetValue = $ForwardInternal |
| 48 | + } |
| 49 | + 'ExternalAddress' { |
| 50 | + if ([string]::IsNullOrWhiteSpace($ForwardExternal)) { |
| 51 | + throw 'Forwarding target is required for external forwarding.' |
| 52 | + } |
| 53 | + $SharedParams.ForwardExternal = $ForwardExternal |
| 54 | + $TargetValue = $ForwardExternal |
| 55 | + } |
| 56 | + default { |
| 57 | + throw "$ForwardOption is not a valid forwarding option." |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + Add-CIPPScheduledTask -Task ([PSCustomObject]@{ |
| 62 | + TenantFilter = $TenantFilter |
| 63 | + Name = "Add Forwarding Vacation Mode: $UserDisplay -> $TargetValue" |
| 64 | + Command = @{ value = 'Set-CIPPVacationForwarding'; label = 'Set-CIPPVacationForwarding' } |
| 65 | + Parameters = ($SharedParams | Select-Object *, @{ n = 'Action'; e = { 'Add' } }) |
| 66 | + ScheduledTime = [int64]$StartDate |
| 67 | + PostExecution = $Request.Body.postExecution |
| 68 | + Reference = $Request.Body.reference |
| 69 | + }) -hidden $false |
| 70 | + |
| 71 | + Add-CIPPScheduledTask -Task ([PSCustomObject]@{ |
| 72 | + TenantFilter = $TenantFilter |
| 73 | + Name = "Remove Forwarding Vacation Mode: $UserDisplay" |
| 74 | + Command = @{ value = 'Set-CIPPVacationForwarding'; label = 'Set-CIPPVacationForwarding' } |
| 75 | + Parameters = [PSCustomObject]@{ |
| 76 | + TenantFilter = $TenantFilter |
| 77 | + Users = $UserUPNs |
| 78 | + Action = 'Remove' |
| 79 | + APIName = $APIName |
| 80 | + } |
| 81 | + ScheduledTime = [int64]$EndDate |
| 82 | + PostExecution = $Request.Body.postExecution |
| 83 | + Reference = $Request.Body.reference |
| 84 | + }) -hidden $false |
| 85 | + |
| 86 | + $Result = "Successfully scheduled forwarding vacation mode for $UserDisplay." |
| 87 | + $StatusCode = [HttpStatusCode]::OK |
| 88 | + } catch { |
| 89 | + $ErrorMessage = Get-CippException -Exception $_ |
| 90 | + $Result = "Failed to schedule forwarding vacation mode: $($ErrorMessage.NormalizedError)" |
| 91 | + Write-LogMessage -headers $Headers -API $APIName -message $Result -Sev Error -tenant $TenantFilter -LogData $ErrorMessage |
| 92 | + $StatusCode = [HttpStatusCode]::InternalServerError |
| 93 | + } |
| 94 | + |
| 95 | + return ([HttpResponseContext]@{ |
| 96 | + StatusCode = $StatusCode |
| 97 | + Body = @{ Results = $Result } |
| 98 | + }) |
| 99 | +} |
0 commit comments