#Function 1: Add Analyst Comment to Action Log on IR, SR, PR
Import-Module SMLets -Force

function Add-AnalystComment {
    param (
            [parameter(Mandatory=$true,Position=0)][Alias('Id')]$ParentWorkItemID,
            [parameter(Mandatory=$true,Position=1)][Alias('Comment')][String]$AddComment,
            [parameter(Mandatory=$true,Position=2)][Alias('EnteredBy')][String]$AddEnteredBy,
            [parameter(Mandatory=$true,Position=3)][Alias('PrivateComment')][String]$Private
          )

    #Check what type of WorkItem and set variables    
    If ($ParentWorkItemID -like "SR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.ServiceRequest$
    $WorkItemTypeProjection = "System.WorkItem.ServiceRequestProjection" }

    ElseIf ($ParentWorkItemID -like "IR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.Incident$
    $WorkItemTypeProjection = "Xapity.ActionLog.TypeProjection.Incident"}

    ElseIf ($ParentWorkItemID -like "PR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.Problem$
    $WorkItemTypeProjection = "Xapity.ActionLog.TypeProjection.Problem"}

    $WorkItemObject = Get-SCSMObject -Class $WorkItemClass -Filter "Id -eq $ParentWorkItemID" 
    
    $NewGUID = ([guid]::NewGuid()).ToString()
    $Projection = @{__CLASS = $WorkItemClass;
                    __SEED = $WorkItemObject;
                    AnalystCommentLog = @{__CLASS = "System.WorkItem.TroubleTicket.AnalystCommentLog";
                                          __OBJECT = @{Id = $NewGUID;
                                                       DisplayName = $NewGUID;
                                                       Comment = $AddComment;
                                                       EnteredBy  = $AddEnteredBy;
                                                       EnteredDate = (Get-Date).ToUniversalTime();
                                                       IsPrivate = $Private;
                                                      }
                                         }
                   }
    
    New-SCSMObjectProjection -Type $WorkItemTypeProjection -Projection $Projection
}

#Examples using each Work Item type
Add-AnalystComment -Id "SR1213" -Comment "This is a test 3" -EnteredBy "Analyst, John" -Private $False
Add-AnalystComment -Id "IR1021" -Comment "This is a test 3" -EnteredBy "Analyst, John" -Private $False
Add-AnalystComment -Id "PR1314" -Comment "This is a test 3" -EnteredBy "Analyst, John" -Private $False


#Function 2: Add End User Comment to Action Log on IR, SR
Import-Module SMLets -Force

function Add-EndUserComment {
    param (
            [parameter(Mandatory=$true,Position=0)][Alias('Id')]$ParentWorkItemID,
            [parameter(Mandatory=$true,Position=1)][Alias('Comment')][String]$AddComment,
            [parameter(Mandatory=$true,Position=2)][Alias('EnteredBy')][String]$AddEnteredBy
          )

    #Check what type of WorkItem and set variables 
    If ($ParentWorkItemID -like "SR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.ServiceRequest$ 
    $WorkItemTypeProjection = "System.WorkItem.ServiceRequestProjection"}

    ElseIf ($ParentWorkItemID -like "IR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.Incident$
    $WorkItemTypeProjection = "Xapity.ActionLog.TypeProjection.Incident"}

    $WorkItemObject = Get-SCSMObject -Class $WorkItemClass -Filter "Id -eq $ParentWorkItemID" 
    $NewGUID = ([guid]::NewGuid()).ToString()
    $Projection = @{__CLASS = $WorkItemClass;
                    __SEED = $WorkItemObject;
                    EndUserCommentLog = @{__CLASS = "System.WorkItem.TroubleTicket.UserCommentLog";
                                          __OBJECT = @{Id = $NewGUID;
                                            DisplayName = $NewGUID;
                                            Comment = $AddComment;
                                            EnteredBy  = $AddEnteredBy;
                                            EnteredDate = (Get-Date).ToUniversalTime();
                                            }
                                         }
                   }
   New-SCSMObjectProjection -Type $WorkItemTypeProjection -Projection $Projection
}

#Examples using each Work Item type
Add-EndUserComment -Id "SR1213" -Comment "This is a test 2" -EnteredBy "EndUser, John"
Add-EndUserComment -Id "IR1021" -Comment "This is a test 2" -EnteredBy "EndUser, John"



#Function 3: Add Action Comment to Action Log on IR, SR, PR
Import-Module SMLets -Force

function Add-ActionComment {
    param (
            [parameter(Mandatory=$true,Position=0)][Alias('Id')]$ParentWorkItemID,
            [parameter(Mandatory=$true,Position=1)][Alias('ActionType')][String]$ActionTypeData,
            [parameter(Mandatory=$true,Position=2)][Alias('Title')][String]$AddTitle,
            [parameter(Mandatory=$true,Position=3)][Alias('Comment')][String]$AddComment,
            [parameter(Mandatory=$true,Position=4)][Alias('EnteredBy')][String]$AddEnteredBy
          )

    #Check what type of WorkItem and set variables 
    If ($ParentWorkItemID -like "SR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.ServiceRequest$
    $WorkItemTypeProjection = "System.WorkItem.ServiceRequestProjection" }

    ElseIf ($ParentWorkItemID -like "IR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.Incident$
    $WorkItemTypeProjection = "Xapity.ActionLog.TypeProjection.Incident"}

    ElseIf ($ParentWorkItemID -like "PR*") {
    $WorkItemClass = Get-SCSMClass System.WorkItem.Problem$
    $WorkItemTypeProjection = "Xapity.ActionLog.TypeProjection.Problem"}

    Switch($ActionTypeData) 
    {
        EmailSent {$ActionType = "System.WorkItem.ActionLogEnum.EmailSent"}
        FileAttached {$ActionType = "System.WorkItem.ActionLogEnum.FileAttached"}
        FileDeleted {$ActionType = "System.WorkItem.ActionLogEnum.FileDeleted"}
        RecordAssigned {$ActionType = "System.WorkItem.ActionLogEnum.RecordAssigned"}
        RecordClosed {$ActionType = "System.WorkItem.ActionLogEnum.RecordClosed"}
        RecordDispatched {$ActionType = "System.WorkItem.ActionLogEnum.RecordDispatched"}
        RecordEscalated {$ActionType = "System.WorkItem.ActionLogEnum.RecordEscalated"}
        RecordOpened {$ActionType = "System.WorkItem.ActionLogEnum.RecordOpened"}
        Reopened {$ActionType = "System.WorkItem.ActionLogEnum.Reopened"}
        Resolved {$ActionType = "System.WorkItem.ActionLogEnum.Resolved"}
        SurveyCompleted {$ActionType = "System.WorkItem.ActionLogEnum.SurveyCompleted"}
        TaskExecuted {$ActionType = "System.WorkItem.ActionLogEnum.TaskExecuted"}
        TemplateApplied {$ActionType = "System.WorkItem.ActionLogEnum.TemplateApplied"}
     }


    $WorkItemObject = Get-SCSMObject -Class $WorkItemClass -Filter "Id -eq $ParentWorkItemID" 
    $NewGUID = ([guid]::NewGuid()).ToString()
    $Projection = @{__CLASS = $ProjectionClass;
                    __SEED = $WorkItemObject;
                    ActionLog = @{__CLASS = "System.WorkItem.TroubleTicket.ActionLog";
                                          __OBJECT = @{Id = $NewGUID;
                                                       DisplayName = $NewGUID;
                                                       ActionType = $ActionType;
                                                       Title = $AddTitle;
                                                       Description = $AddComment;
                                                       EnteredBy  = $AddEnteredBy;
                                                       EnteredDate = (Get-Date).ToUniversalTime();
                                                       IsPrivate = $false;
                                                      }
                                         }
                   }
    New-SCSMObjectProjection -Type $WorkItemTypeProjection -Projection $Projection
}

#Examples using each Work Item type
Add-ActionComment -Id "SR1213" -ActionType "FileAttached"  -Title "File Uploaded" -Comment "This is a upload test comment" -EnteredBy "System, John"
Add-ActionComment -Id "IR1021" -ActionType "FileAttached"  -Title "File Uploaded" -Comment "This is a upload test comment" -EnteredBy "System, John"
Add-ActionComment -Id "PR1314" -ActionType "FileAttached"  -Title "File Uploaded" -Comment "This is a upload test comment" -EnteredBy "System, John"