@@ -10,6 +10,8 @@ function Push-ExecScheduledCommand {
1010 $Table = Get-CippTable - tablename ' ScheduledTasks'
1111 $task = $Item.TaskInfo
1212 $commandParameters = $Item.Parameters | ConvertTo-Json - Depth 10 | ConvertFrom-Json - AsHashtable
13+ $Tenant = $Item.Parameters.TenantFilter ?? $Item.TaskInfo.Tenant
14+ $TenantInfo = Get-Tenants - TenantFilter $Tenant
1315
1416 $Function = Get-Command - Name $Item.Command
1517 if ($null -eq $Function ) {
@@ -21,7 +23,7 @@ function Push-ExecScheduledCommand {
2123 Results = " $Results "
2224 TaskState = $State
2325 }
24- Write-LogMessage - API ' Scheduler_UserTasks' - tenant $tenant - message " Failed to execute task $ ( $task.Name ) : The command $ ( $Item.Command ) does not exist." - sev Error
26+ 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
2527 return
2628 }
2729
@@ -40,8 +42,7 @@ function Push-ExecScheduledCommand {
4042 Write-Host " Failed to remove parameters: $ ( $_.Exception.Message ) "
4143 }
4244
43- $tenant = $Item.Parameters.TenantFilter ?? $Item.Tenant
44- Write-Host " Started Task: $ ( $Item.Command ) for tenant: $tenant "
45+ Write-Host " Started Task: $ ( $Item.Command ) for tenant: $Tenant "
4546 try {
4647
4748 try {
@@ -85,27 +86,27 @@ function Push-ExecScheduledCommand {
8586 Results = " $errorMessage "
8687 TaskState = $State
8788 }
88- Write-LogMessage - API ' Scheduler_UserTasks' - tenant $tenant - message " Failed to execute task $ ( $task.Name ) : $errorMessage " - sev Error - LogData (Get-CippExceptionData - Exception $_.Exception )
89+ 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 )
8990 }
9091 Write-Host ' Sending task results to target. Updating the task state.'
9192
9293 if ($Results ) {
9394 $TableDesign = '<style>table.blueTable{border:1px solid #1C6EA4;background-color:#EEE;width:100%;text-align:left;border-collapse:collapse}table.blueTable td,table.blueTable th{border:1px solid #AAA;padding:3px 2px}table.blueTable tbody td{font-size:13px}table.blueTable tr:nth-child(even){background:#D0E4F5}table.blueTable thead{background:#1C6EA4;background:-moz-linear-gradient(top,#5592bb 0,#327cad 66%,#1C6EA4 100%);background:-webkit-linear-gradient(top,#5592bb 0,#327cad 66%,#1C6EA4 100%);background:linear-gradient(to bottom,#5592bb 0,#327cad 66%,#1C6EA4 100%);border-bottom:2px solid #444}table.blueTable thead th{font-size:15px;font-weight:700;color:#FFF;border-left:2px solid #D0E4F5}table.blueTable thead th:first-child{border-left:none}table.blueTable tfoot{font-size:14px;font-weight:700;color:#FFF;background:#D0E4F5;background:-moz-linear-gradient(top,#dcebf7 0,#d4e6f6 66%,#D0E4F5 100%);background:-webkit-linear-gradient(top,#dcebf7 0,#d4e6f6 66%,#D0E4F5 100%);background:linear-gradient(to bottom,#dcebf7 0,#d4e6f6 66%,#D0E4F5 100%);border-top:2px solid #444}table.blueTable tfoot td{font-size:14px}table.blueTable tfoot .links{text-align:right}table.blueTable tfoot .links a{display:inline-block;background:#1C6EA4;color:#FFF;padding:2px 8px;border-radius:5px}</style>'
9495 $FinalResults = if ($results -is [array ] -and $results [0 ] -is [string ]) { $Results | ConvertTo-Html - Fragment - Property @ { l = ' Text' ; e = { $_ } } } else { $Results | ConvertTo-Html - Fragment }
95- $HTML = $FinalResults -replace ' <table>' , " This alert is for tenant $tenant . <br /><br /> $TableDesign <table class=blueTable>" | Out-String
96- $title = " $TaskType - $tenant - $ ( $task.Name ) "
96+ $HTML = $FinalResults -replace ' <table>' , " This alert is for tenant $Tenant . <br /><br /> $TableDesign <table class=blueTable>" | Out-String
97+ $title = " $TaskType - $Tenant - $ ( $task.Name ) "
9798 Write-Host ' Scheduler: Sending the results to the target.'
9899 Write-Host " The content of results is: $Results "
99100 switch - wildcard ($task.PostExecution ) {
100- ' *psa*' { Send-CIPPAlert - Type ' psa' - Title $title - HTMLContent $HTML - TenantFilter $tenant }
101- ' *email*' { Send-CIPPAlert - Type ' email' - Title $title - HTMLContent $HTML - TenantFilter $tenant }
101+ ' *psa*' { Send-CIPPAlert - Type ' psa' - Title $title - HTMLContent $HTML - TenantFilter $Tenant }
102+ ' *email*' { Send-CIPPAlert - Type ' email' - Title $title - HTMLContent $HTML - TenantFilter $Tenant }
102103 ' *webhook*' {
103104 $Webhook = [PSCustomObject ]@ {
104- ' Tenant' = $tenant
105+ ' Tenant' = $Tenant
105106 ' TaskInfo' = $Item.TaskInfo
106107 ' Results' = $Results
107108 }
108- Send-CIPPAlert - Type ' webhook' - Title $title - TenantFilter $tenant - JSONContent $ ($Webhook | ConvertTo-Json - Depth 20 )
109+ Send-CIPPAlert - Type ' webhook' - Title $title - TenantFilter $Tenant - JSONContent $ ($Webhook | ConvertTo-Json - Depth 20 )
109110 }
110111 }
111112 }
@@ -149,6 +150,6 @@ function Push-ExecScheduledCommand {
149150 }
150151 }
151152 if ($TaskType -ne ' Alert' ) {
152- Write-LogMessage - API ' Scheduler_UserTasks' - tenant $tenant - message " Successfully executed task: $ ( $task.Name ) " - sev Info
153+ Write-LogMessage - API ' Scheduler_UserTasks' - tenant $Tenant - tenantid $TenantInfo .customerId - message " Successfully executed task: $ ( $task.Name ) " - sev Info
153154 }
154155}
0 commit comments