Skip to content

Commit 685144a

Browse files
authored
Merge pull request #368 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 9b2ed2e + 2433e92 commit 685144a

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Push-ExecScheduledCommand.ps1

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ function Push-ExecScheduledCommand {
6666
}
6767

6868
Write-Host 'ran the command. Processing results'
69+
Write-Host "Results: $($results | ConvertTo-Json -Depth 10)"
6970
if ($item.command -like 'Get-CIPPAlert*') {
71+
Write-Host 'This is an alert task. Processing results as alerts.'
7072
$results = @($results)
7173
$TaskType = 'Alert'
7274
} else {
75+
Write-Host 'This is a scheduled task. Processing results as scheduled task.'
7376
$TaskType = 'Scheduled Task'
7477
if ($results -is [String]) {
7578
$results = @{ Results = $results }
@@ -81,15 +84,16 @@ function Push-ExecScheduledCommand {
8184
@{ Results = $Message }
8285
}
8386
}
84-
87+
Write-Host "Results after processing: $($results | ConvertTo-Json -Depth 10)"
88+
write0host 'Moving onto storing results'
8589
if ($results -is [string]) {
8690
$StoredResults = $results
8791
} else {
8892
$results = $results | Select-Object * -ExcludeProperty RowKey, PartitionKey
8993
$StoredResults = $results | ConvertTo-Json -Compress -Depth 20 | Out-String
9094
}
9195
}
92-
96+
Write-Host "Results: $($results | ConvertTo-Json -Depth 10)"
9397
if ($StoredResults.Length -gt 64000 -or $task.Tenant -eq 'AllTenants' -or $task.TenantGroup) {
9498
$TaskResultsTable = Get-CippTable -tablename 'ScheduledTaskResults'
9599
$TaskResults = @{
@@ -101,13 +105,34 @@ function Push-ExecScheduledCommand {
101105
$StoredResults = @{ Results = 'Completed, details are available in the More Info pane' } | ConvertTo-Json -Compress
102106
}
103107
} catch {
108+
Write-Host "Failed to run task: $($_.Exception.Message)"
104109
$errorMessage = $_.Exception.Message
110+
#if recurrence is just a number, add it in days.
111+
if ($task.Recurrence -match '^\d+$') {
112+
$task.Recurrence = $task.Recurrence + 'd'
113+
}
114+
$secondsToAdd = switch -Regex ($task.Recurrence) {
115+
'(\d+)m$' { [int64]$matches[1] * 60 }
116+
'(\d+)h$' { [int64]$matches[1] * 3600 }
117+
'(\d+)d$' { [int64]$matches[1] * 86400 }
118+
default { 0 }
119+
}
120+
121+
if ($secondsToAdd -gt 0) {
122+
$unixtimeNow = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds
123+
if ([int64]$task.ScheduledTime -lt ($unixtimeNow - $secondsToAdd)) {
124+
$task.ScheduledTime = $unixtimeNow
125+
}
126+
}
127+
128+
$nextRunUnixTime = [int64]$task.ScheduledTime + [int64]$secondsToAdd
105129
if ($task.Recurrence -ne 0) { $State = 'Failed - Planned' } else { $State = 'Failed' }
106130
Update-AzDataTableEntity -Force @Table -Entity @{
107-
PartitionKey = $task.PartitionKey
108-
RowKey = $task.RowKey
109-
Results = "$errorMessage"
110-
TaskState = $State
131+
PartitionKey = $task.PartitionKey
132+
RowKey = $task.RowKey
133+
Results = "$errorMessage"
134+
ScheduledTime = "$nextRunUnixTime"
135+
TaskState = $State
111136
}
112137
Write-LogMessage -API 'Scheduler_UserTasks' -tenant $Tenant -tenantid $TenantInfo.customerId -message "Failed to execute task $($task.Name): $errorMessage" -sev Error -LogData (Get-CippExceptionData -Exception $_.Exception)
113138
}

0 commit comments

Comments
 (0)