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
- Install SMA powershell module on the Service Manger management server running workflows
- Install SMLets on the Service Manger management server running workflows
- Create a new activity that starts a SMA runbook in a new management pack
- Import the management
- 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.
- Start the Service Manager Authoring Tool
- Service Manager Authoring Tool, click File and select New
- New Management Pack, input the name for the new management pack, for example Contoso.SMA. Click Save
- In the Management Pack Explorer, right-click Classes and select Create Other Class
- Base Class, select Activity and click OK
- Create Class, input Contoso.SMA.DeployVM, click Create
-
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.
- Click Create property and input VMName as internal name, click Create
- Click Create property and input VMSize as internal name, click Create
- In the Details pane for the VMSize property, change Data Type to List
- In the Select a list dialog box, click Create List
- Create List, input VMSizeList as internal name and VM Size as Display name. Click Create
- In the Select a list dialog box, select the new VM Size list, click Ok
- In the Management Pack Explorer, right-click Workflows and select Create
- Create Workflow Wizard, General, input ContosoSMAInvokeRunbook as name. Click Next
- Create Workflow Wizard, Trigger Condition, select Run only when… click Next
- 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
- Pick additional criteria, click the Change To tab, add Criteria as
[Activity] Status equals In Progress
Click Ok
- Create Workflow Wizard, Trigger Criteria, click Next
- Create Workflow Wizard, Summary, click Create
- Create Workflow Wizard, Completion, click Close
- Once you click Close the workflow designer will be displayed. Add a Windows PowerShell Script to the workflow
- 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
- 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 - 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
- The activity and management pack is now ready to import. Great work! Save the management pack in the Service Manager Authoring Tool
- 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
- You can now open Service Manager console and import the management pack
- In the Service Manager Console, browse to Library and Lists, open the VM Size list
- 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/ - In the Service Manager Console, browse to Library and Templates, click Create Template
- Create Template, input a name, for example Contoso SMA Deploy Azure VM. Select Contoso.SMA.DeployVM as Class and click OK
- Contoso.SMA.DeployVM Properties, input Contoso SMA Deploy Azure VM as Display Name and click OK
- 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.
I’m stuck on step 27. Where is the workflow DLL and the Management Pack folder? My management pack is an XML file? Thanks.
[…] http://contoso.se/blog/?p=3948 […]
great post .. big help.
thanks
Willem
Hi, the big parts are in the blog post. But if you need the complete code send me a email anders @ contoso se and I will send it to you.
Hi Anders,
Where can I find SMA runbook code? I don’t see an attachment.