Skip to main content

CM Scheduled Maintenance Tasks Info

Because of this reddit post, Maintenance Task does not finish, I got inspired to look closer at maintenance tasks.  There is already a view for seeing what your tasks are set to, and how long they have taken to run the last time they ran.

There is also a very helpful instructions here, How site maintenance tasks can make your life much easier, including some powershell, for asking any of the tasks to "run now", instead of waiting for a scheduled time.

I've included the powershell code for 'run now', just in case (as sometimes happens for me) a linked post disappears.

Why this might be interesting to know... if a task takes "too long", or, say, Backups run because a task triggers at midnight, backup triggers at 1am, and the midnight-task gets cancelled and never finishes, you might want to adjust some of the timings, or 'DeleteOlderThan', because your site might need adjusting.

select 
SQLTaskStatus.TaskName,
SQLTaskStatus.BeginTime,
SQLTaskStatus.LatestBeginTime,
SQLTaskStatus.IsEnabled,
SQLTaskStatus.CompletionStatus,
SQLTaskStatus.LastStartTime,
SQLTaskStatus.LastCompletionTime,
DATEDIFF(SECOND, SQLTaskStatus.LastStartTime, SQLTaskStatus.LastCompletionTime ) as 'RunTime in seconds',
 CASE WHEN (1&SQLTaskStatus.DaysOfWeek)=1 THEN 'SUNDAY' END as 'Sunday',
   CASE WHEN (2&SQLTaskStatus.DaysOfWeek)=2 THEN 'MONDAY' END as 'Monday',
   CASE WHEN (4&SQLTaskStatus.DaysOfWeek)=4 THEN 'TUESDAY' END as 'Tuesday',
   CASE WHEN (8&SQLTaskStatus.DaysOfWeek)=8 THEN 'WEDNESDAY' END as 'Wednesday',
   CASE WHEN (16&SQLTaskStatus.DaysOfWeek)=16 THEN 'THURSDAY' END as 'Thursday',
   CASE WHEN (32&SQLTaskStatus.DaysOfWeek)=32 THEN 'FRIDAY' END as 'Friday',
   CASE WHEN (64&SQLTaskStatus.DaysOfWeek)=64 THEN 'SATURDAY' END as 'Saturday',
SQLTaskStatus.RunNow,
SQLTaskStatus.SiteCode,
SQLTaskStatus.DeleteOlderThan
from vSMS_SQLTaskStatus AS SQLTaskSTatus
order by SQLTaskStatus.TaskName
OPTION(USE HINT('FORCE_LEGACY_CARDINALITY_ESTIMATION'))


Powershell for triggering a task to 'run now'.  
 -replace SiteCode with your site code
 -replace the name with the name of the task you want to run
  and you do have to run it while connected to your Provider

$SiteCode = 'ABC' 
$MT = Get-CMSiteMaintenanceTask -SiteCode $SiteCode -Name 'Delete Aged Scenario Health History' 
$MethodParam = New-Object 'System.Collections.Generic.Dictionary[String,Object]' 
$MethodParam.Add('SiteCode',$($SiteCode)) 
$MethodParam.Add('TaskName',$($MT.TaskName)) 
$ConfigMgrCon = Get-CMConnectionManager 
$ConfigMgrCon.ExecuteMethod('SMS_SQLTaskStatus','RunTaskNow',$MethodParam) 

 

CMCB, SQL

  • Created on .