Monday, 15 December 2014

Create SLA Calendars in Service Manager 2012 R2 using PowerShell and XML

Every task I do these days, I look to automate. Even if this means doing something manually and then later going back to see if it can be.

This is probably because I work with multiple customer, with different environments, rather than just setting things up once within my own environment so this “Automate everything” approach will not apply to everyone. It’s about finding the value and ROI of spending the time designing/writing the automation versus the effort of manually doing the task.

That said, the latest one I went to tackle, was creating SLA working hours calendars within System Center 2012 R2 Service Manager (SCSM).

I had been on and off trying it for a while but picked it back up again the other week, but finally gave up banging my head on the desk (especially when I forgot to move my cup of coffee) and reached out for some help.

I sent a tearful email to my fellow SCSM Cookbook authors and Dieter Gasser, like the SCSM coding guru that he is, replied back in triple quick time with how to do this in PowerShell.
The basic outline principal for this is:
  • Gather the classes for Calendar and Working Days
  • Gather the Relationship between the two
  • Gather the Enumerations for the Days
  • Set an array for the Calendar and each Day with the Name, TimeZones, Start/End times and if they’re enabled or not
  • Create new Objects for the Class based on those arrays
  • Create the Relationships between the Days and the Calendar
  • Commit them to the database

This was so easy in the end I (as usual) had been trying to boil the ocean with a bonfire instead of taking a cup full and using a kettle.

So thanks to Didi, I was able to take his simple code which went line by line setting each day and wrapped it into a script that queries an XML file allow modelling of multiple calendars and the streamlined creation of them.

I’ve uploaded the script and example XML file to the TechNet gallery as a zip file here:

The XML is fairly self-explanatory. For each calendar you would like to create, add a <SLACalendar> </SLACalendar> section and fill in the details for the days.

If you don’t want a day as a working day, just set it as Enabled=”False”

The only part I’ve not made easier yet is the TimeZone setting.
Currently you will need to find the string value used within SCSM and copy this to the XML.

This is fairly easy to find, just create a temporary calendar with the timezone you want and then run this PowerShell one-liner to display all the timezones in use:
(Get-SCSMObject -Class (Get-SCSMClass System.Calendar$)).TimeZone

Copy and paste the output to the XML.

Once you have the XML ready, create the calendars by running the script as so:

New-SCSMCalendar.ps1 –XMLFile <Path to XML>

e.g. New-SCSMCalendar.ps1 –XMLFile C:\Temp\Calendars.xml

Again, huge thanks to Didi with his help on this, be sure to check out his blog ( or follow him on Twitter @DiGaBlog


electronic signature said...

A friend of mine pointed out your blog to me last summer and I have been following you ever since. I love all your diy posts. They are always full of innovative ideas and your tutorials are terrific!!

Jaime Palazuelos said...

Hi Steve,

Thanks for this code, it was really helpful.

I hope you do not mind, but I added the holidays. I know there is an option using the Outlook calendar, but just in case.

This is the code in the XML, after the last day:

And this is the PowerShell code:

$_.Holidays | ForEach-Object {
$_.Holiday | ForEach-Object {
for([int]$i=0;$i -le $_.RecurreForNYears;$i++){
$NewGUID = ([guid]::NewGuid()).ToString()
$NewGUID = 'SLACalendarHoliday_' + $NewGUID
[datetime]$StartTime = $_.StartTime
$Projection = @{__CLASS = "System.Calendar";
__SEED = $oCalendar;
Holidays = @{__CLASS = "System.Calendar.Holiday";
__OBJECT = @{
Id = $NewGUID;
DisplayName = $_.DisplayName;
StartTime = $StartTime;
$NewHoliday = New-SCSMObjectProjection -Type ServiceManager.Calendar.Projection -Projection $Projection -PassThru -ErrorAction Stop


Jaime Palazuelos

Jaime Palazuelos said...

I see the XML code is not visible, I am going to add it modifying the "<" and ">" by "[" and "]":

[Holiday DisplayName="Boxing day" StartTime="2016/12/26" RecurreForNYears="10"/]
[Holiday DisplayName="New Year's Day" StartTime="2016/01/01" RecurreForNYears="10"/]
[Holiday DisplayName="Easter Monday" StartTime="2017/4/17" RecurreForNYears="0"/]