#---------------------------------------------------------------------------------------------------------------------
#  Script:   Copy Approvers on CR and RR.ps1
#  Author:   www.xapity.com
#  Date:     09-10-2016
#  Version:  1.0
#  Comments: Used in a Xapity PowerShell Activity to copy two custom User Picker fields from the Parent work item
#            to child Review Activities. It uses a custom Activity Tag list value on the RA to decide which Approver
#            is applied to the Review Activity
#---------------------------------------------------------------------------------------------------------------------
Import-Module SMLets -Force

#Set the Review Activity Class and Relationships
$ReviewActivityClass = Get-SCSMClass -Name System.Workitem.Activity.ReviewActivity$
$ReviewerClass = Get-SCSMClass -Name System.Reviewer$
$ReviewActivityHasReviewer = Get-SCSMRelationshipClass -Name System.ReviewActivityHasReviewer$
$ReviewerIsUser = Get-SCSMRelationshipClass -Name System.ReviewerIsUser$

#Determine if CR or RR and then set Class for the Parent work item, and the Business and Technical Approver Relationships
If ($ParentID -like "CR*")
    {$Class = Get-SCSMClass -name System.WorkItem.ChangeRequest$
    $BusApproverClass = Get-SCSMRelationshipClass -Name ChangeBusinessApprover
    $TechApproverClass = Get-SCSMRelationshipClass -Name ChangeTechnicalApprover
    }
ElseIf ($ParentID -like "RR*")
    {$Class = Get-SCSMClass -name System.WorkItem.ReleaseRecord$
    $BusApproverClass = Get-SCSMRelationshipClass -Name ReleaseBusinessApprover
    $TechApproverClass = Get-SCSMRelationshipClass -Name ReleaseTechnicalApprover
    }

#Get the SCSM objects
$Parent = Get-SCSMObject -Class $Class -Filter "ID -eq $ParentID"
$BusinessApprover = Get-SCSMRelatedObject -Relationship $BusApproverClass -SMObject $Parent
$TechnicalApprover = Get-SCSMRelatedObject -Relationship $TechApproverClass -SMObject $Parent

#Write out the data to be used
Write-Host -ForegroundColor Yellow "The following data will be used:"
Write-Host "     Class is: $Class"
Write-Host "     Business Approver: $BusinessApprover"
Write-Host "     Technical Approver: $TechnicalApprover"
Write-Host ""
Write-host "-------------------------------------"
Write-host -ForegroundColor Green "Running Loop of Activities Now"
Write-Host ""
 
#Find all the review Activities on the Parent Work Item
$Activities = Get-SCSMRelatedObject -SMObject $Parent -Depth Recursive | ?{$_.TypeName -like "System.WorkItem.Activity.ReviewActivity*"}

#Loop through the Review Activities to find the Activity Tag and set the Approver for that Review Activity
foreach ($Activity in $Activities)
    { Write-host "-----------------------------------------------------------"
    Write-Host "Activity ID:  [$($Activity.id)]: $($Activity.Title)"
        
        If ($Activity.ReviewActivityTag.DisplayName -like "*Business Approver*")
        {       
            Write-host "Set Change Business Approver: $BusinessApprover"
            $ReviewActivity = Get-SCSMObject -Class $ReviewActivityClass -Filter "Id -eq $($Activity.ID)"
            $ReviewerOptions = @{ReviewerID = "{0}"; Mustvote = $false; Veto = $false}
            $Reviewer = $null
            $Reviewer = New-SCSMObject -Class $ReviewerClass -PropertyHashtable $ReviewerOptions -NoCommit
            $RAStep1 = New-SCSMRelationshipObject -Relationship $ReviewActivityHasReviewer -Source $ReviewActivity -Target $Reviewer -NoCommit
            $RAStep2 = New-SCSMRelationshipObject -Relationship $ReviewerIsUser -Source $Reviewer -Target $BusinessApprover -NoCommit
            $RAStep1.Commit()
            $RAStep2.Commit()
            Write-host ""
        }
    ElseIf ($Activity.ReviewActivityTag.DisplayName -like "*Technical Approver*")
        {   Write-host "Set Change Technical Approver: $TechnicalApprover"
            $ReviewActivity = Get-SCSMObject -Class $ReviewActivityClass -Filter "Id -eq $($Activity.ID)"
            $ReviewerOptions = @{ReviewerID = "{0}"; Mustvote = $false; Veto = $false}
            $Reviewer = $null
            $Reviewer = New-SCSMObject -Class $ReviewerClass -PropertyHashtable $ReviewerOptions -NoCommit
            $RAStep1 = New-SCSMRelationshipObject -Relationship $ReviewActivityHasReviewer -Source $ReviewActivity -Target $Reviewer  -NoCommit
            $RAStep2 = New-SCSMRelationshipObject -Relationship $ReviewerIsUser -Source $Reviewer -Target $TechnicalApprover  -NoCommit
            $RAStep1.Commit()
            $RAStep2.Commit()
            Write-host ""
        }
      }

Write-Host -ForegroundColor Green "Script Complete"