Update the Request Action Log in Service Manager from SMA
Almost a month ago I posted on Updating the Request Action Log in Service Manager from Orchestrator. I thought it might be helpful to also post how to do this from PowerShell so it can be used with Service Management Automation (SMA).
To do this straight from PowerShell you can call the following script, ex: .\UpdateActionLog.ps1 –SMManagementServer “sm1.contoso.com” –ServiceRequestID “SR1500” –Title “Script Success” –Description “The Script Ran Successfully”
param(
[Parameter(Mandatory=$true)]
$SMManagementServer,
[Parameter(Mandatory=$true)]
$ServiceRequestID,
[Parameter(Mandatory=$true)]
$Title,
[Parameter(Mandatory=$true)]
$Description
)
function GetSMManagementGroupConnection
{
param ($computerName)
$smDir = (Get-ItemProperty 'hklm:/software/microsoft/System Center/2010/Service Manager/Setup').InstallDirectory
try { Import-Module ($smDir + "\Powershell\System.Center.Service.Manager.psd1") -ErrorAction Stop }
catch {}
try { $SM = New-SCManagementGroupConnection -computerName $computerName -ErrorAction Stop }
catch { exit }
return $SM
}
#Main
$sm = GetSMManagementGroupConnection $SMManagementServer
$mp = Get-SCManagementPack -Name "System.WorkItem.Library"
$srClass = Get-SCClass -name 'System.WorkItem.ServiceRequest'
$actionLogClass = Get-SCClass -name 'System.WorkItem.TroubleTicket.ActionLog'
$relationshipClass = Get-SCRelationship -name 'System.WorkItemHasActionLog'
#Get Service Request
$sr = Get-SCClassInstance -class $srClass -filter ('Id -eq {0}' -f $ServiceRequestID)
#Create new Action Log Object
$aLog = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($SMManagementServer, $actionLogClass)
[string]$id = [guid]::NewGuid()
$actionType = "System.WorkItem.ActionLogEnum.TaskExecuted"
$aLog.Item($actionLogClass.Item("DisplayName")).Value = $id
$aLog.Item($actionLogClass.Item("Id")).Value = $id
$aLog.Item($actionLogClass.Item("ActionType")).Value = $mp.GetEnumerations().GetItem($actionType)
$aLog.Item($actionLogClass.Item("Title")).Value = $Title
$aLog.Item($actionLogClass.Item("Description")).Value = $Description
$aLog.Item($actionLogClass.Item("EnteredBy")).Value = "Script"
$aLog.Item($actionLogClass.Item("EnteredDate")).Value = (Get-Date).ToUniversalTime()
#Create new Relationship between the Service Request and Action Log
New-SCRelationshipInstance -RelationshipClass $relationshipClass -Source $sr -Target $aLog
To do this in SMA a few script modifications need to be made:
workflow UpdateActivityLog
{
param(
[Parameter(Mandatory=$true)]
$SMManagementServer,
[Parameter(Mandatory=$true)]
$ServiceRequestID,
[Parameter(Mandatory=$true)]
$Title,
[Parameter(Mandatory=$true)]
$Description
)
inlinescript
{
#Connect to SM
$smDir = (Get-ItemProperty 'hklm:/software/microsoft/System Center/2010/Service Manager/Setup').InstallDirectory
Import-Module ($smDir + "\Powershell\System.Center.Service.Manager.psd1")
$SM = New-SCManagementGroupConnection -computerName $Using:SMManagementServer
#Get SM Classes
$mp = Get-SCManagementPack -Name "System.WorkItem.Library"
$srClass = Get-SCClass -name 'System.WorkItem.ServiceRequest'
$actionLogClass = Get-SCClass -name 'System.WorkItem.TroubleTicket.ActionLog'
$relationshipClass = Get-SCRelationship -name 'System.WorkItemHasActionLog'
#Get Service Request
$sr = Get-SCClassInstance -class $srClass -filter ('Id -eq {0}' -f $Using:ServiceRequestID)
#Create new Action Log Object
$aLog = New-Object Microsoft.EnterpriseManagement.Common.CreatableEnterpriseManagementObject($Using:SMManagementServer, $actionLogClass)
[string]$id = [guid]::NewGuid()
$actionType = "System.WorkItem.ActionLogEnum.TaskExecuted"
$aLog.Item($actionLogClass.Item("DisplayName")).Value = $id
$aLog.Item($actionLogClass.Item("Id")).Value = $id
$aLog.Item($actionLogClass.Item("ActionType")).Value = $mp.GetEnumerations().GetItem($actionType)
$aLog.Item($actionLogClass.Item("Title")).Value = $Using:Title
$aLog.Item($actionLogClass.Item("Description")).Value = $Using:Description
$aLog.Item($actionLogClass.Item("EnteredBy")).Value = "Script"
$aLog.Item($actionLogClass.Item("EnteredDate")).Value = (Get-Date).ToUniversalTime()
#Create new Relationship between the Service Request and Action Log
New-SCRelationshipInstance -RelationshipClass $relationshipClass -Source $sr -Target $aLog
}
}
The output from using this script would look the same as if you were updating the log from Orchestrator: