Get the Service Request ID from a nested RBAA

I came across a very annoying issue a few days ago… we normally get the Service Request ID using a reusable runbook we created that just grabs the parent Service Request ID from a Runbook Automation Activity. However, I put an RBAA inside a Parallel Activity, and this meant it no longer worked.

Therefore, after some very hard thinking and a eureka moment, I’ve created a script that keeps looping until it grabs an ID that starts with SR. I thought I’d share it here!

$RBClass = Get-SCSMClass -Name Microsoft.SystemCenter.Orchestrator.RunbookAutomationActivity
$ContainsActivityID = (Get-SCSMRelationshipClass -Name System.WorkItemContainsActivity).id

$relationship = $null
$srid = $null
$RBAA = Get-SCSMObject -Class $RBClass -Filter "name -eq '{Insert RBAA ID from published data}'"

$relationship = Get-SCSMRelationshipInstance -TargetInstance $RBAA | ? { $_.relationshipid -eq $ContainsActivityID }

while ($relationship.SourceObject.Name -notlike "SR*" ) {

$relationship = Get-SCSMRelationshipInstance -TargetInstance $relationship.SourceObject | ? { $_.relationshipid -eq $ContainsActivityID }

}

$srid = $relationship.sourceobject.name

Please note I always import the SCSM module from Program Files\Microsoft System Center 2016\Service Manager\PowerShell\System.Center.Service.Manager.psd1 rather than smlets.