System Center Orchestrator: Get Current Date / Time using the NOW() Variable and Automate Outlook Calendar Scheduling
Hi Ho – Neil Peterson here, the in-laws have invaded my house, so I am coming to you today from the corner booth at my local coffee shop. I have a quick little treat this morning, nothing to complicated, but pretty cool and important never the less.
Quite recently I have begun work on some runbook automation that includes Outlook scheduling capabilities. In order to achieve this scheduling functionality, I need to get the current date and time (at each runbook execution time). For instance, as a runbook is executed, I want to send an Outlook calendar invite to a specified user for exactly two days from the time of runbook execution, thus the need for the current date and time. For comparison sake, if I were writing my automation in VBScript I might use the ‘Date’ function, or in C# the ‘DateTime.Now’ function, but what is the equivalent when working with an Orchestrator runbook? Quite certainly we could use a Run .NET script activity and enumerate this information using PowerShell, however there may be a better (more native) method for collecting this information.
In this blog posting I will be taking a quick look at the native Orchestrator NOW() variable. I will show how to use the variable, demonstrate some easy methods for modifying the captured time (now plus two days), and then finally show all of these activities in use when creating an Outlook calendar appointment. The components discussed in this posting can then be reused in pretty much any situation which requires any sort of scheduling, or runbook automation which revolves around knowledge of the current date and time.
The NOW() Variable:
Quite simply, a native 'special' variable exists in the Orchestrator design environment that when invoked will return the current date and time. This variable must be created before use. This is no different than creating any other ‘custom’ Orchestrator variable, however will have a value of ‘NOW() ’, as can be seen in the below screen shot. Notice that while in this example the value is NOW(), I have given the variable a name of ‘Current Date and Time: ’
For demonstration purposes, I will create a runbook that simply inserts a line into a text document. This text will be ‘The current date and time is’ and then the newly created NOW() variable. The complete runbook and the Insert Line activity configuration is shown below:
After executing the Runbook we can observe the expected output – simple and straight forward.
More information about the NOW() variable can be found here - https://technet.microsoft.com/en-us/library/hh440543.aspx .
Manipulating the Current Date and Time:
Let’s assume now that we have some automation which once executed needs the current time plus two days. So for instance, when the Runbook is executed, we would like to send a meeting invite for two days later. How can we take NOW() and add two days to the returned value? As it turns out, this is quite simple using the Format Date/Time activity, which is found under Utilities. With this activity, not only can we alter the format of the current date and time, but we can manipulate this date and time as well by incrementing any of the date time values (day, hour, etc.).
For this example, I will build off of the already demonstrated Runbook. I will add a ‘Format Date/Time’ activity and then another Insert Line activity. The end result should be two lines, one with the current date time, and one with the current date time + 2 days.
Before configuring the Format Date/Time activity, let’s examine the activities properties. What you will see is that we must specify an input format (that is the format the date time string is in when invoking this activity), and then an output format (that is the format we would like the activity to re-publish the date time string as). This will become very important as we begin to work with other components such as any Outlook Scheduling, as the NOW() variable and the expected date / time format when using the Exchange User Integration pack are not the same. Furthermore, notice here that the default input format does not match what was output by the NOW() variable (compare the format below to the output in the previous example). We will need to modify the Input format to match what the NOW() variable returns. More on formatting can be found here - https://technet.microsoft.com/en-us/library/hh206037.aspx .
The good news is that I have worked through properly formatting for both input and output (assuming you will be using Outlook integration). For the input format, when using the NOW() variable as our input, we will use: M/d/yyyy h:mm:s tt . For the output format when the desired destination is an activity from the Exchange User Integration Pack, we will use: yyyy-MM-ddTHH:mm:ss .
In addition to the formatting, you will also see in the below screen shot the time adjustment which will return a time of NOW() plus two days.
As shown in the screen shot of this second runbook example, we will now write two lines to a text document, one with the current date / time, and then one with the current date / time +2. Below is the insert line text from each insert line activity.
Current Date and time - notice that this is the 'format results without adjustment' which is native published data from the format Date / Time activity.
And finally below is the second insert line activity text (date/time + 2). Notice here that we are using the ‘Format Result’ published data, which includes the adjustment of 2 days.
After executing the Runbook, the output is as follows. Notice that the date / time + 2 is two days after the initial current date time.
Wrapping this up with actual scheduling:
My intention of this blog post was not to put much weight on the Outlook scheduling aspect of date / time, rather highlight how to use the NOW() variable. However, I do like to provide practical examples to accompany technical detail and theory. In this example I will quickly apply what has been discussed into an example which creates a simple calendar invite. Coming in the next week or so I will be releasing some example runbooks and a blog that will really use this scheduling capability in a very cool way.
This sample runbook does require the Exchange User Integration Pack (and an exchange environment). The solution consists of two Format Date / Time activates (one for the calendar invite start time, and one for the end time), and then an activity to create the calendar invite.
For the first date time we need NOW() plus two days.
And for examples sake, say we want the meeting invite to be four hours in length. So for the second Format Date / Time activity, I am taking the results of the first (NOW() plus two) and adding an extra four hours to that.
The end result of running both of these activities is two sets of dates / time: NOW() + 2 days, which will serve as the appointment start time, and then NOW() + 2 days 4 hours, which will serve as the appointment end time.
On the Create Exchange Item (Calendar Invite) activity we can observe the fruits of our work here.
Finally after runbook execution we can observe the results in Outlook. The sent time on the calendar invite represents when the runbook was executed NOW(), and then we can see that the invite is set to start two days after the sent time, and end four hours after that.
Click Image for Better View:
Closing:
I thought this was a cool exercise; the NOW() variable while not complicated, is not something that was obvious to me when beginning this exercise. To boot, the time spent working with date / time format manipulation was a good exercise onto its own. Stay tuned over the next few days / potential week or so, I will be posting a practical solution that uses the topics discussed here to aid in the scheduling of Operating System Deployments. Until then – tweet link is on the page, sign up for my twitter feed for notification of all new neilp activity, thanks for reading.