@@ -9,97 +9,36 @@ function Invoke-ExecTimeSettings {
99 param ($Request , $TriggerMetadata )
1010
1111 try {
12- $Subscription = Get-CIPPAzFunctionAppSubId
13- if ($env: WEBSITE_RESOURCE_GROUP ) {
14- $RGName = $env: WEBSITE_RESOURCE_GROUP
15- } else {
16- $Owner = $env: WEBSITE_OWNER_NAME
17- if ($env: WEBSITE_SKU -ne ' FlexConsumption' -and $Owner -match ' ^(?<SubscriptionId>[^+]+)\+(?<RGName>[^-]+(?:-[^-]+)*?)(?:-[^-]+webspace(?:-Linux)?)?$' ) {
18- $RGName = $Matches.RGName
19- } else {
20- Write-Information " Could not determine resource group from environment variables. Owner: $Owner "
21- $RGName = $null
22- }
23- }
24-
25- $FunctionName = $env: WEBSITE_SITE_NAME
2612 $Timezone = $Request.Body.Timezone.value ?? $Request.Body.Timezone
27- $BusinessHoursStart = $Request.Body.BusinessHoursStart.value ?? $Request.Body.BusinessHoursStart
2813
29- # Validate timezone format
3014 if (-not $Timezone ) {
3115 throw ' Timezone is required'
3216 }
3317
34- if (! $IsLinux ) {
35- # Get Timezone standard name for Windows
36- $Timezone = Get-TimeZone - Id $Timezone | Select-Object - ExpandProperty StandardName
37- }
38-
39- # Calculate business hours end time (10 hours after start)
40- $BusinessHoursEnd = $null
41- if ($env: WEBSITE_SKU -eq ' FlexConsumption' ) {
42- if (-not $BusinessHoursStart ) {
43- throw ' Business hours start time is required for Flex Consumption plans'
44- }
45-
46- # Validate time format (HH:mm)
47- if ($BusinessHoursStart -notmatch ' ^\d{2}:\d{2}$' ) {
48- throw ' Business hours start time must be in HH:mm format'
49- }
50-
51- # Calculate end time (start + 10 hours)
52- $StartTime = [DateTime ]::ParseExact($BusinessHoursStart , ' HH:mm' , $null )
53- $EndTime = $StartTime.AddHours (10 )
54- $BusinessHoursEnd = $EndTime.ToString (' HH:mm' )
55- }
56-
57- Write-Information " Updating function app time settings: Timezone=$Timezone , BusinessHoursStart=$BusinessHoursStart , BusinessHoursEnd=$BusinessHoursEnd "
58-
59- # Build app settings hashtable
60- # Linux Consumption (Dynamic SKU) does not support WEBSITE_TIME_ZONE
61- $IsLinuxConsumption = $IsLinux -and $env: WEBSITE_SKU -ne ' FlexConsumption'
62-
63- if ($IsLinuxConsumption ) {
64- Write-Information ' Skipping WEBSITE_TIME_ZONE — not supported on Linux Consumption plans'
65- $Results = @ {
66- Results = ' Timezone setting is not supported on Linux Consumption function apps. No changes were made.'
67- SKU = $env: WEBSITE_SKU
68- }
69- return ([HttpResponseContext ]@ {
70- StatusCode = [httpstatusCode ]::OK
71- Body = $Results
72- })
73- }
74-
75- $AppSettings = @ {
76- ' WEBSITE_TIME_ZONE' = $Timezone
18+ # Validate the IANA timezone ID is recognised by .NET
19+ try {
20+ $null = [TimeZoneInfo ]::FindSystemTimeZoneById($Timezone )
21+ } catch {
22+ throw " Invalid timezone: '$Timezone ' is not a recognised IANA timezone ID"
7723 }
7824
79- if ($env: WEBSITE_SKU -eq ' FlexConsumption' ) {
80- $AppSettings [' CIPP_BUSINESS_HOURS_START' ] = $BusinessHoursStart
81- $AppSettings [' CIPP_BUSINESS_HOURS_END' ] = $BusinessHoursEnd
25+ $Config = @ {
26+ PartitionKey = ' TimeSettings'
27+ RowKey = ' TimeSettings'
28+ Timezone = $Timezone
8229 }
8330
84- # Update app settings using ARM REST via managed identity
85- Update-CIPPAzFunctionAppSetting - Name $FunctionName - ResourceGroupName $RGName - AppSetting $AppSettings | Out-Null
31+ $ConfigTable = Get-CIPPTable - tablename Config
32+ Add-CIPPAzDataTableEntity @ConfigTable - Entity $Config - Force | Out-Null
8633
87- Write-LogMessage - API ' ExecTimeSettings' - headers $Request.Headers - message " Updated time settings: Timezone=$Timezone , BusinessHours=$BusinessHoursStart -$BusinessHoursEnd " - Sev ' Info'
88-
89- $Results = @ {
90- Results = ' Time settings updated successfully. Please note that timezone changes may require a function app restart to take effect.'
91- Timezone = $Timezone
92- SKU = $env: WEBSITE_SKU
93- }
94-
95- if ($env: WEBSITE_SKU -eq ' FlexConsumption' ) {
96- $Results.BusinessHoursStart = $BusinessHoursStart
97- $Results.BusinessHoursEnd = $BusinessHoursEnd
98- }
34+ Write-LogMessage - API ' ExecTimeSettings' - headers $Request.Headers - message " Updated time settings: Timezone=$Timezone " - Sev ' Info'
9935
10036 return ([HttpResponseContext ]@ {
10137 StatusCode = [httpstatusCode ]::OK
102- Body = $Results
38+ Body = @ {
39+ Results = ' Time settings updated successfully.'
40+ Timezone = $Timezone
41+ }
10342 })
10443
10544 } catch {
0 commit comments