Create Runbook activities from Orchestrator

September 16, 2013 Posted by Anders Asp

Have you tried adding Runbook Activities to your Service Requests dynamic through Orchestrator? Depending on the information provided by the end user on the self-service portal you might want to automate different things or in different ways. So the idea here is to have a Service Request template containing one initial Runbook Activity that adds the rest of the activities to the actual Service Request, whether it’s Manual, Review or Runbook Activities.

Creating/addning Manual or Review activities through Orchestrator isn’t that complicated, as long as you remember to set some essential properties, such as Sequence ID or adding the prefix to ID. The challange comes when trying to add Runbook Activities to your Work Item.

Using the same approach as you do when creating Manual or Review Activities, this is how you would do it:

SS1This will create the Runbook Activity in the SR but if you want the Runbook Activity to function properly, you have to set some pretty complex parameters in the configuration of the “Create related RB activity”. It’s simply not manageable.

So how do we solve this? Well, how does it work from the console? When adding an activity to a Work Item, you actually have to select an activity template, and that’s exactly what we are going to do here as well. It’s just that we are going to apply the template to the Runbook Activity after it’s been created.

To do this we need SMLets (http://smlets.codeplex.com/) and the “PS Script Execution” IP for Orchestrator (http://orchestrator.codeplex.com/releases/view/76101). I tend to use this IP to run SMLets on the SCSM mgmt. server when using SMLets from Orchestrator. Install SMLets on your SCSM management server and deploy and configure the “PS Script Execution” IP on your Orchestrator Runbook Server.

Now, let’s add two activities to our runbook – a “Get Object” activity from the SCSM IP to get the RB activity we just created, and a “Execute PS Script – Global” from the “PS Script Execution” IP.

SS2

I won’t go into details on how all activities are configured or used, as I think you already know that (if not, drop me a comment below and I’ll add more details!), but let’s take a closer look at the “Execute PS Script – Global” activity.

ss3

The activity is configured to call my SCSM mgmt. server where I have SMLets installed. The script I’m running to apply a template on the Runbook Activity itself is actually re-used from an earlier blogpost of mine (http://www.scsm.se/?p=239) and works really good. Using the Runbook Activity ID from the “Get RB” activity we can get the actual object using SMLets. Then we are getting the Runbook Automation Activity template by the displayname of the template itself. In this example the displayname of my template is “Runbook test” (due to lack of imagination 🙂 ). That’s the only two thing you have to reconfigure in this script to make it work!

Here’s the complete script for you to copy/paste:


Import-Module Smlets

$Projection = Get-SCSMTypeProjection Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity.Projection

$RBID = "RUNBOOK ACTIVITY ID FROM DATABUS" $RBObject = get-scsmobjectprojection $projection -filter "Id -eq $RBID"

$template = Get-SCSMObjectTemplate -displayname "Runbook test"

$RBObject.__base.ApplyTemplate($template) $RBObject.__base.Commit()

Remove-Module Smlets

 

10 Responses to Create Runbook activities from Orchestrator

  1. Kenneth Sundby says:

    Great post Anders!

    I was actually asking about this in the Technet forums one month ago with no luck! 🙂
    (I’ll test this later today and add it to the post if it solves my problem)

    I had a suspicion that I had to do this through Powershell, but never had the time to play with it. Thanks for saving me lots of time! Now I can try to create my dynamic approvals workflow.

  2. Ralph says:

    Thank you Anders for all the great posts and sharing your tremendous knowledge, they have been a huge help. I can create SR (with UserInput) and Apply the SR Template (which contains an RB Activity) without issues. However the UserInput does not trickle down to the RB Activity (user input populates the parameter mappings in the RB Activity). I tried the method you specified above but am having no luck. Is there something I am doing wrong or missing? Thanks a bunch in advance for the assistance. Here is my slimmed down code (removed owners, reviewers, status changes etc).

    Import-Module SMLets
    $UserInputsForUpload = ‘


    $SRTemplateName = “My Test SR Template Name”
    $RBTemplateName = “My Test RB Template Name”

    $Title = “My Test SR Title”

    ###################################################################################
    ##### ALIASES / CLASSES #####
    ###################################################################################
    $CLASSReviewer = Get-SCSMclass System.Reviewer$
    $CLASSSystemUser = Get-SCSMClass -Name system.user$
    $CLASSADUser = Get-SCSMClass -Name Microsoft.AD.User$
    $CLASSServiceRequest = Get-SCSMClass -Name System.WorkItem.ServiceRequest$
    $CLASSAffectedUser = Get-SCSMRelationshipClass -Name System.WorkItemAffectedUser$
    $CLASSCreatedByUser = Get-SCSMRelationshipClass -Name System.WorkItemCreatedByUser$
    $CLASSManualActivity = Get-SCSMClass -Name System.WorkItem.Activity.ManualActivity$
    $CLASSReviewActivity = Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$
    $CLASSReviewerIsUserRelationship = Get-SCSMRelationshipClass System.ReviewerIsUser$
    $CLASSReviewActivityHasReviewerRelationship = Get-SCSMRelationshipClass System.ReviewActivityHasReviewer$
    $CLASSRunbookAutomation = Get-SCSMClass -Name Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity$ # NOT System.WorkItem.Activity.AutomatedActivity$

    ###################################################################################
    ##### FUNCTIONS #####
    ###################################################################################
    function Format-XML ([xml]$xml, $indent=2)
    {
    $StringWriter = New-Object System.IO.StringWriter
    $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
    $xmlWriter.Formatting = “indented”
    $xmlWriter.Indentation = $Indent
    $xml.WriteContentTo($XmlWriter)
    $XmlWriter.Flush()
    $StringWriter.Flush()
    return $StringWriter.ToString()
    }

    ###################################################################################
    ##### CREATE SERVICE REQUEST #####
    ###################################################################################
    $DateTimeUTC = [DateTime]::UtcNow
    $GUID = [guid]::NewGuid()
    $Description = “Automatically Generated By Script On $DateTimeUTC [$GUID]”

    $UserInputsForUpload = Format-XML $UserInputsForUpload

    #Service Request Arguements. These will all be overwritten by the template.
    $ServiceRequestArguments = @{
    ID = “SR{0}”; # Don’t forget to create the prefix SR before the ID
    Title = $Title;
    Source = “Other”;
    Description = $Description;
    UserInput = $UserInputsForUpload;
    }

    #Create Service Request
    $Results = New-SCSMOBject -Class $CLASSServiceRequest -PropertyHashtable $ServiceRequestArguments -PassThru

    #Get The Projection and new service request
    $ServiceRequestTypeProjection = Get-SCSMTypeProjection -Name System.WorkItem.ServiceRequestProjection$
    $NewServiceRequestProjection = Get-SCSMobjectprojection -ProjectionName $ServiceRequestTypeProjection.Name -filter “Description = $Description” #Ensure this is unique or you will apply the template TO ALL SRs

    # Get the newly created service Request
    $ServiceRequestID = $NewServiceRequestProjection.id # This will be used later.
    $ServiceRequest = Get-SCSMObject -Class $CLASSServiceRequest -Filter “ID -eq $ServiceRequestID”

    ###################################################################################
    ##### APPLY TEMPLATE / GET NEW SERVICE REQUEST #####
    ###################################################################################
    #Get Template Service Request
    $Template = Get-SCSMObjectTemplate -DisplayName “$SRTemplateName” # The Template To Use. If you rename the template you MUST rename this.

    #Apply the Template to the previously found Service Request.
    Set-SCSMObjectTemplate -Projection $NewServiceRequestProjection -Template $Template

    ###################################################################################
    ##### ASSOCIATED ACTIVITIES #####
    ###################################################################################
    # Do something to each of the child activities
    $Activities = (Get-SCSMRelationshipObject -BySource $ServiceRequest | Where-Object {$_.RelationshipID -eq “2da498be-0485-b2b2-d520-6ebd1698e61b”}).TargetObject

    ForEach ($Activity in $Activities) {
    $ActivityName = $Activity.Name
    If ($Activity.ClassName -eq “Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity”) {
    # Since the request was auto created complete the Runbook Automaton
    $RunbookActivity = Get-SCSMObject -Class $CLASSRunbookAutomation -Filter “Name -eq $ActivityName” #Find by the ID (Name) example RA63421
    $TypeProjection = Get-SCSMTypeProjection Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity.Projection
    $RBObject = Get-SCSMObjectProjection $TypeProjection -filter “Id -eq $ActivityName”
    $Template = Get-SCSMObjectTemplate -displayname $RBTemplateName
    $RBObject.__base.ApplyTemplate($Template)
    $RBObject.__base.Commit()
    } #If Then
    } #foreach

  3. Ralph says:

    The site does not like the xml code as it probably thinks they are xml tags. I can assure your the userinput data is in properly formatted xml as UserInput Question = “MyQuestion” Answer=”MyAnswer” Type=”string”

    $UserInputsForUpload = ‘[UserInputs]
    [UserInput Question=”UserInput1″ Answer=”SMLet Input 1″ Type=”string” /]
    [UserInput Question=”UserInput2″ Answer=”SMLet Input 2″ Type=”string” /]
    [/UserInputs’

  4. Paul Chals says:

    Great Stuff but I wish it was in C#

  5. Great Post Anders!

    When the script was executed I received this error: Unexpected token ‘\’ in expression or statement.

    The following code throw this error as I see that the method isn´t available.

    $RBObject.__base.ApplyTemplate($template)
    $RBObject.__base.Commit()

    I had to change the code to:

    Import-Module Smlets

    $Projection = Get-SCSMTypeProjection Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity.Projection

    $RBID = “RUNBOOK ACTIVITY ID FROM DATABUS”
    $RBObject = get-scsmobjectprojection $projection -filter “Id -eq $RBID”

    $template = Get-SCSMObjectTemplate -id “ID of your template”

    set-scsmobjectTemplate -Projection $RBObject -Template $template

    Remove-Module Smlets

    Thanks for sharing!

  6. Robson says:

    Hello Anders,
    How can I mapping this new runbook with values ?

    Do you have some example ? Can be SMLets

    Thanks

  7. Anders Asp says:

    Hi Robson,
    In the example above the mappings should be defined in the Runbook Activity Template that you’re applying. Does that make sense?
    Regards
    //Anders

  8. Adam says:

    Hi Anders

    I am looking to implement this in a RB, but I am a Scorch newbie. Could you explain how the Create related RB activity is configured?

    Adam

  9. Your blog is actually a very easy post. I shared this to my
    instagram followers and they enjoyed reading so wanted to say thanks for posting a
    high quality post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*