Home Powershell

How do I add a Reviewer to the "reviewer's container" with SMLets

Stephen_BellardStephen_Bellard Customer IT Monkey ✭
This approach does not work.  Taken from the script in this URL.
http://aquilaweb.net/2015/07/13/scsm-assign-reviewer-to-review-activity-automatically/

getReviewActivities -WorkItem $ChangeObj
 
$ReviewActivity = $ReviewActivities[0]
$reviewerArgs = @{ ReviewerId = "Reviewer{0}" }
 
$Reviewer = $null
$Reviewer = new-scsmobject -class $ReviewerClass -PropertyHashtable $reviewerArgs -NoCommit
$RAStep1 = new-scsmrelationshipobject -NoCommit -relation $RAHasReviewerRel -source $ReviewActivity -target $Reviewer
$RAStep2 = new-scsmrelationshipobject -NoCommit -relation $ReviewerIsUserRel -source $Reviewer -target $OwnerUser
$RAStep1.Commit()
$RAStep2.Commit()

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

  • Stephen_BellardStephen_Bellard Customer IT Monkey ✭
    Sorry here is my screenshot with the "phantom" reviewer.  ;)

  • Stephen_BellardStephen_Bellard Customer IT Monkey ✭
    edited May 2018
    This is the snippet of my code with the type projection.  Line 72 always fails.


  • David_Morris1David_Morris1 Member Advanced IT Monkey ✭✭✭
    Hi Stephen,
    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

    $raid = “RA63247”
    
    $raobj = get-scsmobject -class (get-scsmclass System.WorkItem.Activity.ReviewActivity$) -filter “Id = $raid”
    
    $projection = @{__CLASS = “System.WorkItem.Activity.ReviewActivity”; __SEED = $raobj;
    
    Reviewer = @{__CLASS = “System.Reviewer”;__OBJECT = @{“ID”=”{0}”; MustVote = $true; Veto = $true}}
    
                           }
    
    new-scsmobjectprojection -type System.WorkItem.Activity.ReviewActivityViewProjection$ -projection $projection
    
    $RAHasReviewerRel = Get-SCSMRelationshipClass System.ReviewActivityHasReviewer
    $ReviewerIsUserRel = Get-SCSMRelationshipClass System.reviewerisuser
    $reviewer1 = Get-SCSMRelatedObject -SMObject $raobj -Relationship $RAHasReviewerRel | sort reviewerid -Descending | select -First 1
    new-scsmrelationshipobject -relation $ReviewerIsUserRel -source $Reviewer -target $user

  • Stephen_BellardStephen_Bellard Customer IT Monkey ✭
    Thanks, but the last line fails for me.  Here is my script/function in its entirety.

    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


  • David_Morris1David_Morris1 Member Advanced IT Monkey ✭✭✭
    sorry add -bulk to the end of the last line
  • Stephen_BellardStephen_Bellard Customer IT Monkey ✭
    Well, I did find this good how-to from Nick online, which helped out.  But it's the same logic that I initially found online, detailed in my original post.  https://player.vimeo.com/video/171798784

    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() 


Sign In or Register to comment.