Skip to content

Commit 8909eb1

Browse files
committed
Add ScheduledTaskId tracking and filtering to logs
Introduces script-level ScheduledTaskId variable in Push-ExecScheduledCommand and ensures its cleanup. Updates Write-LogMessage to include ScheduledTaskId in log entries. Enhances Invoke-ListLogs to support filtering by ScheduledTaskId, improving traceability of scheduled tasks in logs. Also adds cleanup for StandardInfo variable in Push-CIPPStandard.
1 parent 38f8f40 commit 8909eb1

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ function Push-ExecScheduledCommand {
77
$item = $Item | ConvertTo-Json -Depth 100 | ConvertFrom-Json
88
Write-Information "We are going to be running a scheduled task: $($Item.TaskInfo | ConvertTo-Json -Depth 10)"
99

10+
$script:ScheduledTaskId = $Item.TaskInfo.RowKey
11+
1012
$Table = Get-CippTable -tablename 'ScheduledTasks'
1113
$task = $Item.TaskInfo
1214
$commandParameters = $Item.Parameters | ConvertTo-Json -Depth 10 | ConvertFrom-Json -AsHashtable
@@ -21,10 +23,12 @@ function Push-ExecScheduledCommand {
2123
$CurrentTask = Get-AzDataTableEntity @Table -Filter "PartitionKey eq '$($task.PartitionKey)' and RowKey eq '$($task.RowKey)'"
2224
if (!$CurrentTask) {
2325
Write-Information "The task $($task.Name) for tenant $($task.Tenant) does not exist in the ScheduledTasks table. Exiting."
26+
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
2427
return
2528
}
2629
if ($CurrentTask.TaskState -eq 'Completed') {
2730
Write-Information "The task $($task.Name) for tenant $($task.Tenant) is already completed. Skipping execution."
31+
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
2832
return
2933
}
3034

@@ -69,6 +73,7 @@ function Push-ExecScheduledCommand {
6973
TaskState = 'Planned'
7074
ScheduledTime = [string]$nextRunUnixTime
7175
}
76+
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
7277
return
7378
}
7479
}
@@ -94,6 +99,7 @@ function Push-ExecScheduledCommand {
9499
}
95100

96101
Write-LogMessage -API 'Scheduler_UserTasks' -tenant $Tenant -tenantid $TenantInfo.customerId -message "Failed to execute task $($task.Name): The command $($Item.Command) does not exist." -sev Error
102+
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
97103
return
98104
}
99105

@@ -330,4 +336,5 @@ function Push-ExecScheduledCommand {
330336
if ($TaskType -ne 'Alert') {
331337
Write-LogMessage -API 'Scheduler_UserTasks' -tenant $Tenant -tenantid $TenantInfo.customerId -message "Successfully executed task: $($task.Name)" -sev Info
332338
}
339+
Remove-Variable -Name ScheduledTaskId -Scope Script -ErrorAction SilentlyContinue
333340
}

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Standards/Push-CIPPStandard.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@ function Push-CIPPStandard {
5656
Write-Warning "Error running standard $($Standard) for tenant $($Tenant) - $($_.Exception.Message)"
5757
Write-Information $_.InvocationInfo.PositionMessage
5858
throw $_.Exception.Message
59+
} finally {
60+
Remove-Variable -Name StandardInfo -Scope Script -ErrorAction SilentlyContinue
5961
}
6062
}

Modules/CIPPCore/Public/Entrypoints/Invoke-ListLogs.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ function Invoke-ListLogs {
8686
$TenantFilter = $Request.Query.Tenant
8787
$ApiFilter = $Request.Query.API
8888
$StandardFilter = $Request.Query.StandardTemplateId
89+
$ScheduledTaskFilter = $Request.Query.ScheduledTaskId
8990

9091
$StartDate = $Request.Query.StartDate ?? $Request.Query.DateFilter
9192
$EndDate = $Request.Query.EndDate ?? $Request.Query.DateFilter
@@ -115,9 +116,10 @@ function Invoke-ListLogs {
115116
$Rows = Get-AzDataTableEntity @Table -Filter $Filter | Where-Object {
116117
$_.Severity -in $LogLevel -and
117118
$_.Username -like $username -and
118-
($TenantFilter -eq $null -or $TenantFilter -eq 'AllTenants' -or $_.Tenant -like "*$TenantFilter*" -or $_.TenantID -eq $TenantFilter) -and
119-
($ApiFilter -eq $null -or $_.API -match "$ApiFilter") -and
120-
($StandardFilter -eq $null -or $_.StandardTemplateId -eq $StandardFilter)
119+
([string]::IsNullOrEmpty($TenantFilter) -or $TenantFilter -eq 'AllTenants' -or $_.Tenant -like "*$TenantFilter*" -or $_.TenantID -eq $TenantFilter) -and
120+
([string]::IsNullOrEmpty($ApiFilter) -or $_.API -match "$ApiFilter") -and
121+
([string]::IsNullOrEmpty($StandardFilter) -or $_.StandardTemplateId -eq $StandardFilter) -and
122+
([string]::IsNullOrEmpty($ScheduledTaskFilter) -or $_.ScheduledTaskId -eq $ScheduledTaskFilter)
121123
}
122124

123125
if ($AllowedTenants -notcontains 'AllTenants') {

Modules/CIPPCore/Public/GraphHelper/Write-LogMessage.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ function Write-LogMessage {
7676
$TableRow.ConditionalAccessTemplateId = [string]$script:StandardInfo.ConditionalAccessTemplateId
7777
}
7878
}
79+
if ($script:ScheduledTaskId) {
80+
$TableRow.ScheduledTaskId = [string]$script:ScheduledTaskId
81+
}
7982

8083
$Table.Entity = $TableRow
8184
Add-CIPPAzDataTableEntity @Table | Out-Null

0 commit comments

Comments
 (0)