How do I add a Reviewer to the "reviewer's container" with SMLets
http://aquilaweb.net/2015/07/13/scsm-assign-reviewer-to-review-activity-automatically/
After receiving the following errors, I learned about a type projection.
"Exception calling "Commit" with "0" argument(s): "A discovery data item was rejected because the item is already bound to
another Membership relationship." blah blah blah
Finally I found this link that uses a type projection to create the "reviewers container", but how do you add a Reviewer to the container?
https://marcelzehner.ch/2014/07/18/service-manager-create-a-reviewers-object-by-using-powershell-scsm/
See screenshot of sample RA below. This is as far as I get.
Answers
looks like you've mashed two scripts together, line 59 and 61 aren't required or doing anything in the script
line 72 is failing as $Reviewer1 isn't the blank reviewer container you've created,the container isn't defined anywhere in the snippet
you need to find the reviewer container you have created and then relate that to the user
Import-module smlets -Force
$ms = 'HOUSCSMUAP1'
$Smdefaultcomputer = $ms
$name = 'chnelson'
$SR_id = 'SR345862'
$ra_id = 'RA345903'
# classes
$User_Class = Get-SCSMClass -Name System.Domain.User$
$SR_Class = Get-SCSMClass -computername $ms System.WorkItem$
$RA_class = Get-SCSMClass -computername $ms System.WorkItem.Activity.ReviewActivity$
$Reviewer_Class = Get-SCSMClass -computername $ms System.Reviewer$
# relationship classes
$RelWorkItemContainsActivity = Get-SCSMRelationshipClass System.WorkItemContainsActivity$
$RelWorkItemContainsReviewer = Get-SCSMRelationshipClass System.ReviewActivityHasReviewer$
$RelReviewerIsUser = Get-SCSMRelationshipClass System.ReviewerIsUser$
# get AD user
$User = Get-SCSMObject -computername $ms -Class $User_Class | ?{$_.UserName -eq $name}
# get SR
$SR = Get-SCSMObject -computername $ms -Class $SR_Class -filter "Id -eq $SR_Id"
# get RA for testing purposes
#$RA = Get-SCSMObject -computername $ms -Class $RA_class -filter "Id -eq $RA_Id"
$RA_Title = 'Approval for '+$SR.title
$RA_Description = "Parent Ticket : "+$SR.ID,"`r`n",$SR.Description
# RA class
$RA_Class = Get-SCSMClass -Name System.WorkItem.Activity.ReviewActivity$
# RA properties
$RA_PropertyHash = @{Title = $RA_Title; Description = $RA_Description; ApprovalCondition = 'Percentage'; `
ApprovalPercentage = 49; Stage = 'Manager Review'; SequenceId = 6; ID = 'RA{0}'}
# creates RA
$RA = New-SCSMObject -Class $RA_Class -PropertyHashtable $RA_PropertyHash -NoCommit
# relates RA to SR
$RelObjectRA = New-SCSMRelationshipObject -Relationship $RelWorkItemContainsActivity -Source $SR -Target $RA -NoCommit
# Commit RA add actions
$RelObjectRA.commit()
# adds Reviewers 'container'
$projection = @{__CLASS = "System.WorkItem.Activity.ReviewActivity"; __SEED = $RA;
Reviewer = @{__CLASS = “System.Reviewer”;__OBJECT = @{"ID"="{0}"; Mustvote = $true; Veto = $true}}
}
New-scsmobjectprojection -type System.WorkItem.Activity.ReviewActivityViewProjection$ -projection $projection
# gets Reviewer 'container'
$Reviewer1 = Get-SCSMRelatedObject -SMObject $RA -Relationship $RelWorkItemContainsReviewer | sort reviewerid -Descending | select -First 1
#$User1 = Get-SCSMRelatedObject -SMObject $Reviewer1 -Relationship $RelReviewerIsUser
# creates Reviewer-to-User relationship
New-SCSMRelationshipObject -relationship $RelReviewerIsUser -source $Reviewer1 -target $User
Anyway I had a flaw in my original testing scenario/logic. I scrapped the type projection stuff and went with the original logic (like below). Thanks for the responses and excuse the ring.
Import-module smlets -Force
$ms = 'HOUSCSMUAP1'
$Smdefaultcomputer = $ms
$name = 'sbellard'
$SR_id = 'SR345910'
$ra_id = 'RA345909'
# classes
$User_Class = Get-SCSMClass -Name Microsoft.AD.User$
$SR_Class = Get-SCSMClass -computername $ms System.WorkItem$
$RA_class = Get-SCSMClass -computername $ms System.WorkItem.Activity.ReviewActivity$
$Reviewer_Class = Get-SCSMClass -computername $ms System.Reviewer$
# relationship classes
$RelWorkItemContainsActivity = Get-SCSMRelationshipClass System.WorkItemContainsActivity$
$RelWorkItemContainsReviewer = Get-SCSMRelationshipClass System.ReviewActivityHasReviewer$
$RelReviewerIsUser = Get-SCSMRelationshipClass System.ReviewerIsUser$
# get AD user
$User = Get-SCSMObject -computername $ms -Class $User_Class | ?{$_.UserName -eq $name}
# get SR
$SR = Get-SCSMObject -computername $ms -Class $SR_Class -filter "Id -eq $SR_Id"
$RA_Title = 'Approval for '+$SR.title
$RA_Description = "Parent Ticket : "+$SR.ID,"`r`n",$SR.Description
# RA properties
$RA_PropertyHash = @{Title = $RA_Title; Description = $RA_Description; ApprovalCondition = 'Percentage'; `
ApprovalPercentage = 49; Stage = 'Manager Review'; SequenceId = 4; ID = 'RA{0}'}
# creates RA
$RA_obj = New-SCSMObject -Class $RA_Class -PropertyHashtable $RA_PropertyHash -NoCommit
# relates RA to SR
$RelObjectRA = New-SCSMRelationshipObject -Relationship $RelWorkItemContainsActivity -Source $SR -Target $RA_obj -NoCommit
# Commit RA add actions
$RelObjectRA.commit()
$Reviewer1 = $null
# Set options on the Reviewer
$Reviewer_args = @{ReviewerID = "{0}"; Mustvote = $true; Veto = $false}
# add Reviewer
$Reviewer1 = New-SCSMObject -Class $Reviewer_Class -PropertyHashtable $Reviewer_args -NoCommit
$RAStep1 = New-SCSMRelationshipObject -Relationship $RelWorkItemContainsReviewer -Source $RA_obj -Target $Reviewer1 -NoCommit
$RAStep2 = New-SCSMRelationshipObject -Relationship $RelReviewerIsUser -Source $Reviewer1 -Target $User -NoCommit
$RAStep1.Commit()
$RAStep2.Commit()