Authoring Friday

Every friday, adventures in System Center authoring

SCSM portal user input to description of the work item

In Service Manager you can create request offerings to publish on the portal. Information from the portal can be mapped to a property of the work item, this is one on one mapping. This can be sometimes difficult, for example if you want all information (questions and answers) from the portal in the description field of the Incident. You can do create this functionality with Orchestrator runbooks that monitor the object creation event, in the solution described in this post a PowerShell script and SCSM workflow are used to add this functionality in the environment.

This blog post provides information around the PowerShell scripts and describes the procedure to create the workflow. The goal of this post is to document the logic so you can adjust it to your needs. The files created in this blog post can be downloaded from TechNet Gallery. The complete flow of the example scenario is illustrated below. Incident or Service Request is created via portal, workflow kicks-off a PS script that updates the new created work item.

image

The PowerShell script

This part of the blog post provides an overview of the PS script. Glue all script information together and you have the complete script. (PS script files are in the download). Let’s start…first we need to initialize the variables in the PS script and get the work item.

For Incidents:

#initialize all variables
[Array]$Array = $null
$builder = [string]::Empty
[Array]$ListArray = $null
$listBuilder = [string]::Empty
$IR_ID=Get-SCSMObject (get-SCSMClass System.WorkItem.Incident$) -Filter "Name -eq $IRId"

For Service Requests:

set-executionpolicy -executionPolicy ByPass
import-module smlets
#initialize all variables
[Array]$Array = $null
$builder = [string]::Empty
[Array]$ListArray = $null
$listBuilder = [string]::Empty
$SR_ID=Get-SCSMObject (get-SCSMClass System.WorkItem.ServiceRequest$) -Filter "Name -eq $SRId"

Once we have the work item object we can define the input (UserInput xml), format it to xml and start filling the array. This is the same for both process, PS info is an example for Service Requests.

#Define input
$UserInput= $SR_ID.UserInput
$content=[XML]$UserInput
#Format input as XML
$inputs = $content.UserInputs.UserInput
$Array += "************ Service Request information **********"
$Array += ""

Now we can loop through the UserInput xml information. Important in this execution part of the script is to capture the case where multiple answers can be provided to the question on the portal. The script detects the multivalue answer (check value "<value*") and enumerates the values selected. All enumerated information is added to the array.

#loop thru all the inputs
foreach ($input in $inputs)
{

#check for multivalues
if($($input.Answer) -like "<value*")
{      
    #format the content of the answer as XML
    [xml]$answer = $input.answer
    foreach($value in $($($answer.values)))
    {
            foreach($item in $value)
            {                  
                foreach ($txt in $($item.value))
                {                    
                    #Add the Property displayname to an array
                    $ListArray += $($txt.DisplayName)                 
                }#End Foreach list value items
                #flat the array using ; as separator
                $Array += $input.Question + " = " + [string]::Join(" ; ",$ListArray)
                $ListArray = $null
            }#End Foreach list              
    }#End foreach multiple answers      
}#End IF

In case when no multiple answers are provided, there are 2 possible input types: Enumeration values or string values. For the enum values the Get-SCSMEnumeration is used to get the displayname of the selected enumeration value. All enumerated information is added to the array.

else
{
     if ($input.type -eq "enum")
    {
        $ListGuid = Get-SCSMEnumeration -Id $input.Answer
        $Array += $($input.Question + " = " + $ListGuid.displayname)
    }
    else
    {
    $Array += $($input.Question + " = " + $input.Answer)
    }#End ELSE
}#End ELSE

The array holds now all information and this information can be set as the description property of the work item. For Incidents we can simple glue this to the Incident work item property, for Service Request it is also important to get this information in the Activity’s related to the request. 

For Incidents:

The Set-SCSMObject PS cmdlet is used to update the description property of the Incident. Before we can set this information the output (array) must be formatted (to string).

$Builder = [string]::Join("`r`n",$Array)
Set-SCSMObject -SMObject (Get-SCSMObject (get-SCSMClass System.WorkItem.Incident$) -Filter "Name -eq $IRID") -Property ‘Description’ -Value $Builder

For Service Requests:

For Service Request the same approach can be used to update the description property on the work item. Additionally, we need to do this also for the Activity’s related to the work item. The Get-SCSMRelatedObject cmdlet get’s the object, The output is filtered based on Service Request ID and the relationshipclass (System.WorkItemContainsActivity). In this example script all activity’s are updated, if you want to limit the update to only Review Activity’s you can extend the script with a filter on the Activity class.

$Builder = [string]::Join("`r`n",$Array)
Set-SCSMObject -SMObject (Get-SCSMObject (get-SCSMClass System.WorkItem.ServiceRequest$) -Filter "Name -eq $SRId") -Property ‘Description’ -Value $Builder
# Get the activities in the SR
$Activities = Get-SCSMRelatedObject -SMObject (get-scsmobject (get-scsmclass System.workitem.ServiceRequest$)|?{$_.Name -eq $SRId}) -Relationship (Get-SCSMRelationshipClass System.WorkItemContainsActivity)
foreach($activity in $activities)
{
    Set-SCSMObject -SMObject $activity -Property ‘Description’ -Value $Builder
}

All done on the scripting part! You can easily extend this script with other updates on work item properties. The best way to do this is to use PropertyHashtable method to update multiple properties on the work item. More information can be found on the cmdlet description – Set-SCSMObject.

Creation of the SCSM workflow via Authoring tool

The procedure to start the Authoring tool and create your management pack is already well documented. The steps to create the management pack for this workflow are simple : create the workflow, add a PowerShell Activity and save the MP.

Create the workflow

Right click on Workflows –> Click Create and follow the wizard. Specify a name for the workflow, optionally a description of the WF can be specified. On the Trigger Condition page, select “Run only when a database object meets specified conditions”.

image

On the Trigger Criteria page, specify the class name. For the event criteria the object created event is selected. Optionally you can specify additional criteria for this workflow. In the example WF an additional criteria is added to only run for portal created work items (Source=Portal).

image

Review Summary and click create. On the Completion page click Close to finalize the workflow creation wizard.

image

Add PowerShell Activity

This procedure requires to create 2 workflows, one for Incidents and one for Service Requests. For both workflows, drag-end-drop the Windows PowerShell Script Activity in the workflow. Navigate to the PS script Activity details and open the Script Body property from the Activity Inputs. This opens the “Configure a Script Activity” wizard. On the Script Body, expand the View and Edit Script view and Import or paste your script information in the script body.

image

On the Script Properties page, make sure that you configure the ID as input for the workflow. In this example the Incident ID is set to the IRId script parameter.

image

Depending on your needs you can do this for both classes or only for one class (if you only need this functionality for Service Requests for example). Save your management pack, copy the dll file(s) to the SCSM program folder of the mgmt server that executes the workflows and import the management pack. All done, ready for testing. The result of this solution is that you have nice formatted description fields of the work items that are created via the portal. In case of a Review activity, the Approver has all information to answer the request for approval. Example output below.

image

The output dll files and management pack created for this blog post can be downloaded via the TechNet Gallery. (http://gallery.technet.microsoft.com/SCSM-Portal-Userinput-to-4b9acee4)

Have fun!

Kurt Van Hoecke

1 Comment “SCSM portal user input to description of the work item”

  1. [...] used some of the script published by Kurt Van Hoecke at Authoring Friday http://www.authoringfriday.com/2013/12/08/scsm-portal-user-input-to-description-of-the-work-item/ in my orchestrator [...]

    Pingback by User Inputs from SCSM Portal to Description field using Orchestrator | ITBlog.no — December 16, 2013 @ 18:01

RSS feed for comments on this post. TrackBack URL

Leave a Response