Sunday, August 18, 2013

Automatically creating scheduled change request workflow with powershell using scsm authoring tool

Last week I got rid of creating windows update change requests every month for every product  in SCSM. Therefore I decided to create a workflow for automatically creating scheduled change requests using powershell.

We use templates for change requests in our SCSM environment.

According to our needs ;

- Filling areas left empty after standart change template applied which are activity implementer, CI's, parent CR child RA and MA descriptions, created by and most importantly planned start and end dates.
-Keeping this script in a  management pack (you will need SCSM authoring tool to create a scheduled workflow)
-Chance to able to enable and disable this workflow when needed

After a deep search in google we have found some usefull powershell codes and smlet commands below;

SCSM: Set Scheduled Start and End Date in Manual Activites of CR via Powershell:

apply template to change request object via powershell: Creating recurring Change Requests:

Using SCSM to Automatically Create Work Items. Specifically: Change Requests!:

and also Automatically Creating Incidents Periodically:

Add a Implementer to Manual Activity via PowerShell:

I have 1 RA and 2 MAs in my template therefore code is arranged according to our needs. After all combined and modified I got the powershell code below ;
Import-Module Smlets

$CrClass = Get-SCSMClass |?{$ -eq "System.WorkItem.ChangeRequest"}

$o = ""

$Params = @{


Title = "your title"

Description = "your description"

Reason = "your reason"



Area = "Operations"

Priority = "Medium"

Impact = "Standard"

Risk = "Medium"

$o = New-SCSMObject -Class $CrClass -PropertyHashtable $Params -pass

#-------adding description to standart change---------
$changeRequest = Get-SCSMObjectProjection System.WorkItem.ChangeRequestProjection -filter "Id -eq '$o'"

$template = Get-SCSMObjectTemplate Template.700f759a82014344bc0bb2b732e95b47 #(template ID got from custom template MP used)



#-------adding "created by user" to CR----------------
$CreatedByRelClass = Get-SCSMRelationshipClass System.WorkItemCreatedByUser

$ChangeReq = Get-SCSMObject $CrClass|?{$_.Id -eq $o}

$UserClass = Get-SCSMClass System.Domain.User

$CreatedByUser = Get-SCSMObject $UserClass|?{$_.UserName -eq "borgamentes"}

New-SCSMRelationshipObject -RelationShip $CreatedByRelClass -Source $ChangeReq -Target $CreatedByUser -Bulk

#-------------adding description into RA------------------
$WIContainsRAActivityRel = Get-SCSMRelationshipClass -Name System.WorkItemContainsActivity

$AllCRActivities = Get-SCSMRelatedobject -SMObject $o -Relationship $WIContainsActivityRARel

$RActivities = $AllCRActivities | where {$_.ClassName -eq "System.WorkItem.Activity.reviewActivity"}

$RAdescription="SCSM update2"


Set-SCSMObject -SMObject (Get-SCSMObject (get-SCSMClass System.WorkItem.activity.reviewactivity) -Filter

"Id -eq $RAID") -Property 'Description' -Value $RAdescription

#-------------filling empty areas left in MA's------------------
$MActivities = $AllCRActivities | where {$_.ClassName -eq "System.WorkItem.Activity.manualActivity"}

$MAdescription1="SCSM MA1 update"

$MAdescription2="SCSM MA2 update"

$MAProp1 = @{

Description = "your description"

Area = "Software"



$MAProp2 = @{

Description = "your description"

Area = "Software"



$ActivityImpRelClass = Get-SCSMRelationshipClass System.WorkItemAssignedToUser

$MAUserClass = Get-SCSMClass System.Domain.User

$MAActivityImplementer = Get-SCSMObject $MAUserClass|?{$_.UserName -eq "borgamentes"}

New-SCSMRelationshipObject -RelationShip $ActivityImpRelClass -Source $MActivities[0] -Target $MAActivityImplementer -Bulk

New-SCSMRelationshipObject -RelationShip $ActivityImpRelClass -Source $MActivities[1] -Target $MAActivityImplementer -Bulk

$configItem= get-SCSMRelationshipClass System.WorkItemAboutConfigItem

$computerclass = Get-SCSMClass |?{$_.Name -eq ""}

$computername1 = Get-SCSMObject $computerclass|?{$_.PrincipalName -eq "fqdn of the first CI"}

$computername2 = Get-SCSMObject $computerclass|?{$_.PrincipalName -eq "fqdn of the second CI"}

$computername3 = Get-SCSMObject $computerclass|?{$_.PrincipalName -eq "fqdn of the third CI"}

New-SCSMRelationshipObject -RelationShip $configItem -Source $MActivities[0] -Target $computername1 -Bulk

New-SCSMRelationshipObject -RelationShip $configItem -Source $MActivities[0] -Target $computername2 -Bulk

New-SCSMRelationshipObject -RelationShip $configItem -Source $MActivities[0] -Target $computername3 -Bulk

New-SCSMRelationshipObject -RelationShip $configItem -Source $MActivities[1] -Target $computername1 -Bulk

New-SCSMRelationshipObject -RelationShip $configItem -Source $MActivities[1] -Target $computername2 -Bulk

New-SCSMRelationshipObject -RelationShip $configItem -Source $MActivities[1] -Target $computername3 -Bulk


Set-SCSMObject -SMObject (Get-SCSMObject (get-SCSMClass System.WorkItem.activity.manualactivity) -Filter "Id -eq $MAID1") -PropertyHashtable $MAProp1 -pass


Set-SCSMObject -SMObject (Get-SCSMObject (get-SCSMClass System.WorkItem.activity.manualactivity) -Filter "Id -eq $MAID2") -PropertyHashtable $MAProp2 -pass

remove-module smlets


When you try to run the script from powershell command windows you will see its working fine but to run it through a custom MP you have to modify your custom template MP.

For RA;
<Property Path="$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Id$">RA{0}</Property>

and for MA's ;
<Property Path="$Context/Property[Type='CustomSystem_WorkItem_Activity_Library!System.WorkItem.Activity']/Id$">MA{0}</Property>

Then you can save your project in SCSM Authoring tool.  Still the work is not competed. Final step is to copy both the .DLL authoring tool and MP created to SCSM installation directory and import it.

All done! :)

No comments:

Post a Comment