Skip to content

Commit a85ec47

Browse files
feat: Add functions for scheduling forwarding vacation mode (KelvinTegelaar#1985)
Frontend PR: KelvinTegelaar/CIPP#5800
2 parents af3f817 + ba09156 commit a85ec47

3 files changed

Lines changed: 157 additions & 3 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function Set-CIPPVacationForwarding {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter(Mandatory)] [string]$TenantFilter,
5+
[Parameter(Mandatory)] [ValidateSet('Add', 'Remove')] [string]$Action,
6+
[object[]]$Users,
7+
[ValidateSet('internalAddress', 'ExternalAddress')] [string]$ForwardOption,
8+
[string]$ForwardInternal,
9+
[string]$ForwardExternal,
10+
[bool]$KeepCopy,
11+
[string]$APIName = 'Forwarding Vacation Mode',
12+
$Headers
13+
)
14+
15+
$Results = [System.Collections.Generic.List[string]]::new()
16+
$Users = @($Users)
17+
18+
foreach ($upn in $Users) {
19+
if ([string]::IsNullOrWhiteSpace($upn)) { continue }
20+
21+
try {
22+
if ($Action -eq 'Remove') {
23+
$result = Set-CIPPForwarding -UserID $upn -Username $upn -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName -Disable $true
24+
} else {
25+
switch ($ForwardOption) {
26+
'internalAddress' {
27+
if ([string]::IsNullOrWhiteSpace($ForwardInternal)) {
28+
throw 'ForwardInternal is required for internal forwarding.'
29+
}
30+
31+
$result = Set-CIPPForwarding -UserID $upn -Username $upn -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName -Forward $ForwardInternal -KeepCopy $KeepCopy
32+
}
33+
'ExternalAddress' {
34+
if ([string]::IsNullOrWhiteSpace($ForwardExternal)) {
35+
throw 'ForwardExternal is required for external forwarding.'
36+
}
37+
38+
$result = Set-CIPPForwarding -UserID $upn -Username $upn -TenantFilter $TenantFilter -Headers $Headers -APIName $APIName -ForwardingSMTPAddress $ForwardExternal -KeepCopy $KeepCopy
39+
}
40+
default {
41+
throw "Unsupported forward option: $ForwardOption"
42+
}
43+
}
44+
}
45+
46+
$Results.Add($result)
47+
} catch {
48+
$ErrorMessage = Get-CippException -Exception $_
49+
$Results.Add("Failed to set forwarding for ${upn}: $($ErrorMessage.NormalizedError)")
50+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Failed to set forwarding for ${upn}: $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage
51+
}
52+
}
53+
54+
return $Results
55+
}

Modules/CIPPCore/Public/Set-CIPPVacationOOO.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ function Set-CIPPVacationOOO {
7777
$result = Set-CIPPOutOfOffice @SplatParams
7878
$Results.Add($result)
7979
} catch {
80-
$err = (Get-CippException -Exception $_).NormalizedError
81-
$Results.Add("Failed to set OOO for ${upn}: $err")
82-
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Failed OOO for ${upn}: $err" -Sev Error
80+
$err = Get-CippException -Exception $_
81+
$Results.Add("Failed to set OOO for ${upn}: $($err.NormalizedError)")
82+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Failed OOO for ${upn}: $($err.NormalizedError)" -Sev Error -LogData $err
8383
}
8484
}
8585
return $Results

0 commit comments

Comments
 (0)