Quick Tip - Run An SMA Runbook At A Specific Date/Time

Happy New Year’s Eve! Here’s a quick tip just before New Year’s.

I recently answered a question on Technet about scheduling SMA runbooks. It’s no secret that the scheduling engine in Service Management Automation leaves something to be desired. Here’s how I like to use PowerShell to get specific about when an SMA runbook is going to be triggered.

You’ll need the SMA PowerShell tools installed and imported for this to work.

$dateWhen = [DateTime]"<put a date and time in here or otherwise calculate one>"
$strSchedName = "some_prefix_$($strWhen)"
$schedRun = set-smaschedule -name $strSchedName -webserviceendpoint "https://your-endpoint" -scheduletype onetimeschedule -starttime $dateWhen -expirytime $dateWhen.AddHours(3) -description $env:username
$strReturn = start-smarunbook -name "your-runbook" -WebServiceEndpoint "https://your-endpoint" -schedulename $strSchedName -parameters @{ var1 = "var1"; var2 = "var2" }

Line 1 is easy, it’s just a variable for a datetime object and it’s going to represent the time you want to trigger the runbook. Line 2 is a variable for what the name of the SMA schedule asset will be. I like to add something dynamic here to avoid naming collisions.

Now the interesting parts. On Line 3, we’re creating an SMA schedule asset using set-smaschedule. It’s going to be named our Line 2 variable, it’s going to be a onetimeschedule (instead of recurring), start at our start time (Line 1) and expire three hours after the start time. On Line 4, I’m triggering the runbook with start-smarunbook and specifying the schedule we created on Line 3. I’m also passing parameters in a hash table.

You’re done! The only hiccup with this I’ve seen is if one of your parameters for your runbook is a hashtable. Matthew at sysjam.wordpress.com covered this weird situation in a blog post very recently.

Written on December 30, 2014