Skip to content

Commit 5e80d13

Browse files
committed
Add time settings management for function app
Extended Invoke-ExecBackendURLs.ps1 to include Hosted, OS, SKU, Timezone, and business hours properties. Added Invoke-ExecTimeSettings.ps1 to handle updating timezone and business hours settings for the function app, including validation and Azure app settings update logic.
1 parent da06e49 commit 5e80d13

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecBackendURLs.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ function Invoke-ExecBackendURLs {
3232
RGName = $RGName
3333
FunctionName = $env:WEBSITE_SITE_NAME
3434
SWAName = $SWAName
35+
Hosted = $env:CIPP_HOSTED -eq 'true' ?? $false
36+
OS = $IsLinux ? 'Linux' : 'Windows'
37+
SKU = $env:WEBSITE_SKU
38+
Timezone = $env:WEBSITE_TIME_ZONE ?? 'UTC'
39+
BusinessHoursStart = $env:CIPP_BUSINESS_HOURS_START ?? '09:00'
40+
BusinessHoursEnd = $env:CIPP_BUSINESS_HOURS_END ?? '17:00'
3541
}
3642

3743

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
function Invoke-ExecTimeSettings {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
CIPP.SuperAdmin.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
try {
12+
$Subscription = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
13+
$Owner = $env:WEBSITE_OWNER_NAME
14+
15+
if ($env:WEBSITE_SKU -ne 'FlexConsumption' -and $Owner -match '^(?<SubscriptionId>[^+]+)\+(?<RGName>[^-]+(?:-[^-]+)*?)(?:-[^-]+webspace(?:-Linux)?)?$') {
16+
$RGName = $Matches.RGName
17+
} else {
18+
$RGName = $env:WEBSITE_RESOURCE_GROUP
19+
}
20+
21+
$FunctionName = $env:WEBSITE_SITE_NAME
22+
$Timezone = $Request.Body.Timezone.value ?? $Request.Body.Timezone
23+
$BusinessHoursStart = $Request.Body.BusinessHoursStart.value ?? $Request.Body.BusinessHoursStart
24+
25+
# Validate timezone format
26+
if (-not $Timezone) {
27+
throw 'Timezone is required'
28+
}
29+
30+
if (!$IsLinux) {
31+
# Get Timezone standard name for Windows
32+
$Timezone = Get-TimeZone -Id $Timezone | Select-Object -ExpandProperty StandardName
33+
}
34+
35+
# Calculate business hours end time (10 hours after start)
36+
$BusinessHoursEnd = $null
37+
if ($env:WEBSITE_SKU -eq 'FlexConsumption') {
38+
if (-not $BusinessHoursStart) {
39+
throw 'Business hours start time is required for Flex Consumption plans'
40+
}
41+
42+
# Validate time format (HH:mm)
43+
if ($BusinessHoursStart -notmatch '^\d{2}:\d{2}$') {
44+
throw 'Business hours start time must be in HH:mm format'
45+
}
46+
47+
# Calculate end time (start + 10 hours)
48+
$StartTime = [DateTime]::ParseExact($BusinessHoursStart, 'HH:mm', $null)
49+
$EndTime = $StartTime.AddHours(10)
50+
$BusinessHoursEnd = $EndTime.ToString('HH:mm')
51+
}
52+
53+
Write-Information "Updating function app time settings: Timezone=$Timezone, BusinessHoursStart=$BusinessHoursStart, BusinessHoursEnd=$BusinessHoursEnd"
54+
55+
# Get current app settings
56+
$Token = Get-AzAccessToken -ResourceUrl 'https://management.azure.com'
57+
$Headers = @{
58+
Authorization = "Bearer $($Token.Token)"
59+
'Content-Type' = 'application/json'
60+
}
61+
62+
$AppSettingsUrl = "https://management.azure.com/subscriptions/$Subscription/resourceGroups/$RGName/providers/Microsoft.Web/sites/$FunctionName/config/appsettings/list?api-version=2022-03-01"
63+
64+
$CurrentSettings = Invoke-RestMethod -Uri $AppSettingsUrl -Method POST -Headers $Headers
65+
66+
# Update settings
67+
$CurrentSettings.properties['WEBSITE_TIME_ZONE'] = $Timezone
68+
69+
if ($env:WEBSITE_SKU -eq 'FlexConsumption') {
70+
$CurrentSettings.properties['CIPP_BUSINESS_HOURS_START'] = $BusinessHoursStart
71+
$CurrentSettings.properties['CIPP_BUSINESS_HOURS_END'] = $BusinessHoursEnd
72+
}
73+
74+
# Save settings
75+
$UpdateUrl = "https://management.azure.com/subscriptions/$Subscription/resourceGroups/$RGName/providers/Microsoft.Web/sites/$FunctionName/config/appsettings?api-version=2022-03-01"
76+
77+
$UpdateResponse = Invoke-RestMethod -Uri $UpdateUrl -Method PUT -Headers $Headers -Body ($CurrentSettings | ConvertTo-Json -Depth 10)
78+
79+
Write-LogMessage -API 'ExecTimeSettings' -headers $Request.Headers -message "Updated time settings: Timezone=$Timezone, BusinessHours=$BusinessHoursStart-$BusinessHoursEnd" -Sev 'Info'
80+
81+
$Results = @{
82+
Results = 'Time settings updated successfully. Please note that timezone changes may require a function app restart to take effect.'
83+
Timezone = $Timezone
84+
SKU = $env:WEBSITE_SKU
85+
}
86+
87+
if ($env:WEBSITE_SKU -eq 'FlexConsumption') {
88+
$Results.BusinessHoursStart = $BusinessHoursStart
89+
$Results.BusinessHoursEnd = $BusinessHoursEnd
90+
}
91+
92+
return ([HttpResponseContext]@{
93+
StatusCode = [httpstatusCode]::OK
94+
Body = $Results
95+
})
96+
97+
} catch {
98+
$ErrorMessage = Get-CippException -Exception $_
99+
Write-LogMessage -API 'ExecTimeSettings' -headers $Request.Headers -message "Failed to update time settings: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
100+
101+
return ([HttpResponseContext]@{
102+
StatusCode = [httpstatusCode]::BadRequest
103+
Body = @{
104+
Results = "Failed to update time settings: $($ErrorMessage.NormalizedError)"
105+
}
106+
})
107+
}
108+
}

0 commit comments

Comments
 (0)