Useful PowerShell snippets – Add-SRComment

November 1, 2014 Posted by Anders Asp

I will be posting a series of PowerShell snippets that you can use together with Service Manager. This first one is a function called “Add-SRComment” it is simply used whenever you would like to add an End User or Analyst comment to one of your Service Requests. I’ve been using this a lot when doing automated stuff in SCO and SMA lately.

The function have the following parameters:

-SRObject
Requires the actual SR object to which you would like to add the comment.

-Comment
Requires the comment to add to the action log

-EnteredBy
Requires the name of the person who’s writing the comment

-AnalystComment
Switch to Analyst comment instead of End User comment

-IsPrivate
Switch to a Private comment instead of a public comment

The script itself:


# This function adds a comment to the SR Action Log
# NOTE: SMlets must be loaded in order for the function to work

Function Add-SRComment {
    param (
        [parameter(Mandatory=$True,Position=0)]$SRObject,
        [parameter(Mandatory=$True,Position=1)]$Comment,
        [parameter(Mandatory=$True,Position=2)]$EnteredBy,
        [parameter(Mandatory=$False,Position=3)][switch]$AnalystComment,
        [parameter(Mandatory=$False,Position=4)][switch]$IsPrivate
    )

    # Make sure that the SR Object it passed to the function
    If ($SRObject.Id -ne $NULL) {
        

        If ($AnalystComment) {
            $CommentClass = "System.WorkItem.TroubleTicket.AnalystCommentLog"
            $CommentClassName = "AnalystCommentLog"
        } else {
            $CommentClass = "System.WorkItem.TroubleTicket.UserCommentLog"
            $CommentClassName = "EndUserCommentLog"
        }

        # Generate a new GUID for the comment
        $NewGUID = ([guid]::NewGuid()).ToString()

        # Create the object projection with properties
        $Projection = @{__CLASS = "System.WorkItem.ServiceRequest";
                        __SEED = $SRObject;
                        EndUserCommentLog = @{__CLASS = $CommentClass;
                                            __OBJECT = @{Id = $NewGUID;
                                                        DisplayName = $NewGUID;
                                                        Comment = $Comment;
                                                        EnteredBy = $EnteredBy;
                                                        EnteredDate = (Get-Date).ToUniversalTime();
                                                        IsPrivate = $IsPrivate.ToBool();
                                            }
                        }
        }

        # Create the actual comment
        New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection" -Projection $Projection
    } else {
        Throw "Invalid Service Request Object!"
    }
}

Script syntax

Comment
You can also download the whole script as a file here: Add-SRComment

Leave a Reply

Your email address will not be published. Required fields are marked *

*