Home Powershell Activity

Parent Base Id inside nested Activities

Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
edited May 2023 in Powershell Activity

When I want to get the Parent Work Item from a Runbook Automation Activity with the "System.WorkItemContainsActivity" Relationship, and that runbook is inside an SA or PA, I have use it that way, in order to receive the Service Request:

$ContainsActivity = Get-SCSMRelationshipClass -Id '2da498be-0485-b2b2-d520-6ebd1698e61b'
$relatedSR = (Get-SCSMRelationshipObject -ByTarget $activity -Relationship $ContainsActivity | ? {$_.SourceObject.ClassName -eq 'System.WorkItem.ServiceRequest'}).SourceObject


As I want to exchange more and more Orchestrator Runbooks with PS Activities, I wanted to ask, what happens, when I use the Parant Base Id like this and the PS Activitiy is inside an SA or PA:

Is the property "Parent Base Id" ALWAYS the Guid of the Work Item containing the Activities, or will it return the "real" parent object, in that case the SA or PA, a bit like in the RB Activity.

Best Answers

  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited July 2023 Answer ✓

    Tested it and it always takes the SR, even if the PSA itself is inside a nested activity

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Answer ✓

    Hi Simon,

    Yup, you are right, we got you covered here.

    However, if you ever need to deal with nested Activities, here's a self calling recursive function to get the top level parent.

    #Function to get the top Parent WI of an activity
    Function Get-ParentWI {
        Param (
            $Activity,
            [string]$ComputerName
        )
        
        $SCSM = @{
            ComputerName = $ComputerName
        }
        
        #Get Parent of this Activity
        $WIContainsActivityRel = Get-SCSMRelationshipClass "System.WorkItemContainsActivity" @SCSM
        $ParentWI = (Get-SCSMRelationshipObject -TargetObject $Activity -TargetRelationship $WIContainsActivityRel @SCSM).SourceObject
        
        If ($ParentWI -eq $null) {
            # We've reached the top - Return Original Actvity
            Return $Activity
        }
        Else {
            # Parent is not confrmed as the root, and thus, we will loop into another "Get-ParentWI" function.
            Get-ParentWI -Activity $ParentWI @SCSM
        }
    }
    

Answers

  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited July 2023 Answer ✓

    Tested it and it always takes the SR, even if the PSA itself is inside a nested activity

  • Geoff_RossGeoff_Ross Cireson Consultant O.G.
    Answer ✓

    Hi Simon,

    Yup, you are right, we got you covered here.

    However, if you ever need to deal with nested Activities, here's a self calling recursive function to get the top level parent.

    #Function to get the top Parent WI of an activity
    Function Get-ParentWI {
        Param (
            $Activity,
            [string]$ComputerName
        )
        
        $SCSM = @{
            ComputerName = $ComputerName
        }
        
        #Get Parent of this Activity
        $WIContainsActivityRel = Get-SCSMRelationshipClass "System.WorkItemContainsActivity" @SCSM
        $ParentWI = (Get-SCSMRelationshipObject -TargetObject $Activity -TargetRelationship $WIContainsActivityRel @SCSM).SourceObject
        
        If ($ParentWI -eq $null) {
            # We've reached the top - Return Original Actvity
            Return $Activity
        }
        Else {
            # Parent is not confrmed as the root, and thus, we will loop into another "Get-ParentWI" function.
            Get-ParentWI -Activity $ParentWI @SCSM
        }
    }
    
  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited July 2023

    @Geoff_Ross Thanks for the explanation :)

    I wrote something similar for a script where I want to receive the Activities and Child IDs from a certain Template ;-)

    Short question: you wrote that you will release a new add in for handling e.g. template permissions at the end of june ;-) will it be released soon? :)

  • Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭

    @Geoff_Ross There might be some problems with that though:

    Today I moved 2 existing PSAs inside a template into a PA and now the Parent Base ID gives back the Guid of the PA. even removing the 2 activities completely and readding them did not solve the issue. Did I do something wrong here?

Sign In or Register to comment.