PowerShell for SCSM - Delete Action Log Comments
In the previous blog post we added comments to the Action Log. If you are anything like me, you will now have a few work items with a ton of Action Log comments. This also came up on the TechNet Service Manager Forums. So how do we delete them?
Essentially we will use the following command to delete the comments (but contained in a ForEach loop to control what can be deleted):
Get-SCSMObject –Class (Get-SCSMClass –Name CommentClass) -Filter "Filter to target comments)" | Remove-SCSMObject -Force
Be careful with this command. It is only limited by the Filter which may not be limited to just one work item - it will globally delete all comments that meet the filter.
In other words it will apply to all work items based on the class and there is the potential to delete a lot more comments than you think. In situations where you want to do this, test very carefully before applying it to a production environment.
Classes and Relationships These are used in the $CommentRel variable definition and in the $WorkItemClass variable. Unfortunately these are not consistent between the different work items that use Action Log:
Incidents and Problem
The following code will display all the comments of one type (Analyst or End User or Action) on an existing work item. The example below gets the Analyst comments on an IR.
To get the other types of Action Log comments change the $CommentRel variable relationship. You can also change the $ WorkItemClass variable to get Service Requests or Problem Records.
On the output display I have used $ActionLogComments | FL to give all the details, otherwise it is a bunch of GUIDS that is hard to tell what you have. You could also use a select command to choose the fields you want to display.
Delete Action Log Comment
To limit the delete action to just one comment I generally use a filter based on DisplayName. The display name on each comment is a unique GUID and will allow you to target the delete to a specific comment.
The issue now is how to find the DisplayName on the particular comment we are interested in. I have used a variation of the script from above to place the action log comments into an array and then loop through them looking for the comment(s) I want to delete.
Place the comments in an array $ActionLogComments:
Then loop through each comment using the ForEach command and evaluate the comment to see if you want to delete it or not - the IF Else block.
In this example I am deleting all the Analyst comments entered by an analyst with Smith at the beginning of their name.
Change the parameters on the IF statement to target what you want to delete - you can use any of the fields displayed from the first code example above.
Download Scripts: DeleteActionLogComments.zip
Script 1: Delete Analyst comments on IR
Script 2: Delete Analyst and End User comments on SR
Xapity PowerShell Activity The Xapity PowerShell Activity is an Activity for SCSM that enables PowerShell scripts to be run in SCSM. Automate more of your processes directly in SCSM. Scripts are stored and updated from a central script repository. The scripts execution output can be viewed and if a failure occurs, an email notification can be sent.
Xapity - Innovative Software for SCSM - Discover our Products