Share via


Scheduling with Service Management Automation

If you have used SMA you will soon learn there is something to be desired in the scheduler that currently exists. As it can do daily and on-demand tasks there are some tasks that need to be run weekly, monthly, and certain days of the week. This was a challenge for me for many weeks not finding very much on the net on how to do this within SMA itself. I finally released how simple it can be to add it into our scripts and figured I would share what I did.  I'll use System Center Service Manager (SCSM) as my example runbook in this article. My company patches System Center servers outside the normal patch process that way we can continue to have monitoring during the normal patching process for production and non-production servers. We do them Wednesday morning starting at 4:00 AM until 12:00 PM Wednesday after the third Sunday of each month. I got hit in February where I got to busy and forgot disable my SCSM Data Warehouse Jobs and caused them to hang. I figured for March I needed to get some automation in there but couldn't get SMA to run the task without running it daily. I finally figured it out in my sleep one night, of all places why just have the script check the date for me?

I was able to find a script on the forums that could give me the third Sunday of the month. I added two days to the return value of the third Sunday of the month to give me the Tuesday before patching my System Center servers. I then created another value just for the current date. I run a if statement that checked if the dates match up and if they do it will disable the jobs. I have provided my entire runbook for disabling SCSM Jobs in example 1. The last step I needed to add was a daily schedule that runs at 7:00 PM.  This resolved my monthly issue, but what if I wanted to do weekly?

I figured modifying the (get-date).dayofweek and assigning it to a variable would work just as easily to the if statement as in example one. Example 2, shows pulling day of the week and if its equal to Monday then run. 

I really hope this helps someone, I felt like I was beating my head against the wall trying to figure it out. I hope this gets fixed in the future as I have been told many a times on the forums and by Microsoft Support. If not at least I have a solid workaround. 

##Example 1
workflow OLH_SCSM_Disable_Jobs
{
 
$SCSMServer = Get-AutomationVariable -Name 'SCSM Server'
$SCSMCredential = Get-AutomationPSCredential -Name 'SCSM Access Account'
 
 InLineScript {
    ##Get Third Sunday
    $FindNthDay=3
    $WeekDay='Sunday'
    [datetime]$Today=[datetime]::NOW
    $todayM=$Today.Month.ToString()
    $todayY=$Today.Year.ToString()
    [datetime]$StrtMonth=$todayM+'/1/'+$todayY

    while ($StrtMonth.DayofWeek -ine $WeekDay ) { $StrtMonth=$StrtMonth.AddDays(1) }

    ##assign Third Sunday to $ThirdSunday
    $ThirdSunday = $StrtMonth.AddDays(7*($FindNthDay-1))
    $ThirdSunday = $ThirdSunday.adddays(2)
    ##end of get patch Third Sunday script

    ##get current date
    $date = get-date

    If ($date.day -eq $ThirdSunday.day)
    {
##Import my cmdlets from installed location on your SCSM Server
        Import-Module "C:\Program Files\Microsoft System Center 2012 R2\Service Manager\Microsoft.EnterpriseManagement.Warehouse.Cmdlets.psd1"

        Disable-SCDWJobSchedule –Jobname Transform.Common
        Disable-SCDWJobSchedule –Jobname MPSyncJob
        Disable-SCDWJobSchedule –Jobname DWMaintenance
        Disable-SCDWJobSchedule –Jobname "Extract_DW_OnLife_Health_Management_Group"
        Disable-SCDWJobSchedule –Jobname "Extract_OnLife Health Management Group"
        Disable-SCDWJobSchedule –Jobname Load.CMDWDataMart
        Disable-SCDWJobSchedule –Jobname Load.Common
        Disable-SCDWJobSchedule –Jobname Load.OMDWDataMart
    }

  }-PSComputerName $SCSMServer -PSCredential $SCSMCredential

}

##Example 2
$date = (get-date).DayOfWeek

If ($date -eq "Monday") 
{<Your script here>}