Home » Orchestrator » SMA » Building a SMA activity for Service Manager

Building a SMA activity for Service Manager

Service Manager and Orchestrator are well connected with a connector. It is easy to use Orchestrator runbooks as activities in for example change requests and service requests. When SMA was released as part of Orchestrator 2012 R2 a number of customers asked how to use SMA runbooks as activities in Service Manager. Some runbooks running in (classical) Orchestrator would fit better in SMA, for example long running jobs that can benefit from check pointing and also jobs that need to scale large.

In this blog post I will show you a way to quickly create an activity for Service Manager to start a specific runbook. The example will trigger a SMA runbook from Service Manager through the SMA web service. SMA will then execute the runbook and update the Service Manager SMA activity, mark it as completed. In this example we will invoke a SMA runbook that build new VMs in Windows Azure. We could build a new VM with the Windows Azure integration pack in Orchestrator runbook designer too, but it is handy to use Powershell in this scenario and if we want to build multiple VMs at the same time SMA can use parallel execute to save some time.

These are the steps we need to take

  1. Install SMA powershell module on the Service Manger management server running workflows
  2. Install SMLets on the Service Manger management server running workflows
  3. Create a new activity that starts a SMA runbook in a new management pack
  4. Import the management
  5. Use the new activity in any work item

SMLets is a PowerShell module built by the community. It can be used to automate common tasks in Service Manager. You can download SMLets here. The SMA PowerShell module can be found on the Orchestrator 2012 R2 installation media.

Building the SMA activity

First step is to build the SMA activity. We can use the Service Manager Authoring Tool for that.

  1. Start the Service Manager Authoring Tool
  2. Service Manager Authoring Tool, click File and select New
  3. New Management Pack, input the name for the new management pack, for example Contoso.SMA. Click Save
  4. In the Management Pack Explorer, right-click Classes and select Create Other Class
  5. Base Class, select Activity and click OK
  6. Create Class, input Contoso.SMA.DeployVM, click Create
  7. In the class properties list, delete the default property named Proerty_XX

    We will create two new properties on the new class, on for virtual machine name (VMName) and one for virtual machine size (VMSize). We will configure the VMSize property with data type List. In Service Manager we will configure a list of values used to select virtual machine size.

  8. Click Create property and input VMName as internal name, click Create
  9. Click Create property and input VMSize as internal name, click Create
  10. In the Details pane for the VMSize property, change Data Type to List
  11. In the Select a list dialog box, click Create List
  12. Create List, input VMSizeList as internal name and VM Size as Display name. Click Create
  13. In the Select a list dialog box, select the new VM Size list, click Ok
  14. In the Management Pack Explorer, right-click Workflows and select Create
  15. Create Workflow Wizard, General, input ContosoSMAInvokeRunbook as name. Click Next
  16. Create Workflow Wizard, Trigger Condition, select Run only when… click Next
  17. Create Workflow Wizard, Trigger Criteria, click Browse and select the Contoso.SMA.DeployVM class. Change Change event to When an object of the selected class is updated, click Additonal Criteria
  18. Pick additional criteria, click the Change To tab, add Criteria as
    [Activity] Status equals In Progress
    Click Ok
  19. Create Workflow Wizard, Trigger Criteria, click Next
  20. Create Workflow Wizard, Summary, click Create
  21. Create Workflow Wizard, Completion, click Close
  22. Once you click Close the workflow designer will be displayed. Add a Windows PowerShell Script to the workflow
  23. We will configure the WindowsPowerShell activity to run a Powershell script to start the SMA runbook. Select the WindowsPowerShell activity, in the Details pane click Script Body
  24. Configure a Script Activity, paste the following script to the Script Body text fieldimport-module SMLets
    $size1 = “$InstanceSize”
    $size2 = $size1 -replace “{“, “”
    $size3 = $size2 -replace “}”, “”
    $size4 = get-scsmenumeration -id $size3
    $SizeName = $size4.DisplayName
    Start-SmaRunbook -WebServiceEndpoint https://wap01 -Name “SCSM__Deploy_New_VM” -Parameters @{“InstanceSize”=”$SizeName”; “VMName”=”$VMName”;”ActivityID”=”$ActivityID”}The script first imports the SMLets PowerShell module. The SMLets module is used to convert the GUID for the list value (enum value) into displayname. For example from Service Manager the workflow gets {300dfe04-a0a0-e5fc-8892-963e7146d86c} instead of Medium, as VM Size. Before we can convert the GUID into a displaystring we must remove “{” and “}”, which the two replace lines do. We then start the runbook and pass on three parameters, VM name, VM Size and the activity ID. This example script starts a SMA runbook named SCSM__Deploy_New_VM and the SMA web service has adress https://wap01
  25. Configure a Script Activity, click Script Properties. Create three parameters. Use class properties from the Contoso.SMA.DeployVM class for all three parameters. Use the ID property from the class for the ActivityID property. Click OK
  26. The activity and management pack is now ready to import. Great work! Save the management pack in the Service Manager Authoring Tool
  27. Copy the workflow dll, ContosoSMAInvokeRunbook.dll from the management pack folder to Service Manager installation folder (C:\Program Files\Microsoft System Center 2012 R2\Service Manager) and then restart the Microsoft Monitoring Agent on the Service Manager management server running workflow
  28. You can now open Service Manager console and import the management pack
  29. In the Service Manager Console, browse to Library and Lists, open the VM Size list
  30. List Properties, add the following items, that are all Azure VM sizes, click OKExtraSmall
    Small
    Medium
    Large
    ExtraLargeFor more information about Windows Azure VM sizes look at http://www.windowsazure.com/en-us/pricing/details/virtual-machines/
  31. In the Service Manager Console, browse to Library and Templates, click Create Template
  32. Create Template, input a name, for example Contoso SMA Deploy Azure VM. Select Contoso.SMA.DeployVM as Class and click OK
  33. Contoso.SMA.DeployVM Properties, input Contoso SMA Deploy Azure VM as Display Name and click OK
  34. You can now use the new activity anywhere you like, for example service request templates.

 

But what about the SMA runbook?

SMA Runbook

The SMA runbook is attached to this blog post. The important part of the runbook, or the really interesting part of it, is the last part of the runbook. This part creates a remote PowerShell session to the Service Manager server (SM01) and marks the activity as completed, the work item will now move on to the next activity.

Inlinescript {
$session = new-pssession -ComputerName SM01
Invoke-Command -Session $session {
$workitem = Get-SCSMObject -Class (get-scsmclass -name Contoso.SMA.DeployVM) | Where-Object {$_.ID -eq “$using:ActivityID”}
$workitem | Set-SCSMObject -Property Status -Value Completed
}

 

In the Windows Azure Pack portal you can see the SMA runbook start and you can also see the input parameters coming from Service Manager

 Summary

This post show you an example of how to invoke a SMA runbook from Service Manager with a custom activity.

This example is written in a very simply way, no complicated configuration or requirements. For example you might want to add error handling in the script, handle situation when SMLets can’t be loaded or Service Manager can’t be contacted. In both those cases you want to set the activity status to failure in Service Manager. Also it is a good practices to seal management packs that include class structure.

Note that this is provided “AS-IS” with no warranties at all. This is not a production ready solution, just an idea and an example.


5 Comments

  1. I’m stuck on step 27. Where is the workflow DLL and the Management Pack folder? My management pack is an XML file? Thanks.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.