none
Adding comments to SRs through PowerShell

Answers

  • I'll answer myself, but in case anybody else needs it:

    function Add-SRComment {
        param (
                [parameter(Mandatory=$true,Position=0)][Alias('SRObject')]$pSRObject,
                [parameter(Mandatory=$true,Position=1)][Alias('Comment')][String]$pComment,
                [parameter(Mandatory=$true,Position=2)][Alias('EnteredBy')][String]$pEnteredBy
              )
        $NewGUID = ([guid]::NewGuid()).ToString()
        $Projection = @{__CLASS = "System.WorkItem.ServiceRequest";
                        __SEED = $pSRObject;
                        AnalystCommentLog = @{__CLASS = "System.WorkItem.TroubleTicket.AnalystCommentLog";
                                              __OBJECT = @{Id = $NewGUID;
                                                           DisplayName = $NewGUID;
                                                           Comment = $pComment;
                                                           EnteredBy  = $pEnteredBy;
                                                           EnteredDate = (Get-Date).ToUniversalTime();
                                                           IsPrivate = $false
                                                          }
                                             }
                       }
        New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection" -Projection $Projection
    }
    
    
    Add-SRComment -SRObject $SR -Comment "This is a test" -EnteredBy "Doe, John"


    Tuesday, April 09, 2013 1:15 AM

All replies

  • I'll answer myself, but in case anybody else needs it:

    function Add-SRComment {
        param (
                [parameter(Mandatory=$true,Position=0)][Alias('SRObject')]$pSRObject,
                [parameter(Mandatory=$true,Position=1)][Alias('Comment')][String]$pComment,
                [parameter(Mandatory=$true,Position=2)][Alias('EnteredBy')][String]$pEnteredBy
              )
        $NewGUID = ([guid]::NewGuid()).ToString()
        $Projection = @{__CLASS = "System.WorkItem.ServiceRequest";
                        __SEED = $pSRObject;
                        AnalystCommentLog = @{__CLASS = "System.WorkItem.TroubleTicket.AnalystCommentLog";
                                              __OBJECT = @{Id = $NewGUID;
                                                           DisplayName = $NewGUID;
                                                           Comment = $pComment;
                                                           EnteredBy  = $pEnteredBy;
                                                           EnteredDate = (Get-Date).ToUniversalTime();
                                                           IsPrivate = $false
                                                          }
                                             }
                       }
        New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection" -Projection $Projection
    }
    
    
    Add-SRComment -SRObject $SR -Comment "This is a test" -EnteredBy "Doe, John"


    Tuesday, April 09, 2013 1:15 AM
  • Hello,

    I have the same need for adding comments to an existing service request.  I'm trying your script above but run into this error.  I'm not sure what to make of it.  Do you know what this error might indicate?  Thanks.

    New-SCSMObjectProjection : Projection

    At C:\Scripts\TestPS.ps1:22 char:5

    + New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection" -P ...

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : InvalidArgument: (SR4807:String) [New-SCSMObjectProjection], NullReferenceException

    + FullyQualifiedErrorId : Bad Projection,SMLets.NewSCSMObjectProjectionCommand

    Friday, May 31, 2013 4:14 PM
  • I think you were trying to pass the SR id as a string... try with this:

    function Add-SRComment {
        param (
                [parameter(Mandatory=$true,Position=0)][Alias('Id')]$pId,
                [parameter(Mandatory=$true,Position=1)][Alias('Comment')][String]$pComment,
                [parameter(Mandatory=$true,Position=2)][Alias('EnteredBy')][String]$pEnteredBy
              )
        $SRClass = Get-SCSMClass System.WorkItem.ServiceRequest$
        $SRObject = Get-SCSMObject -Class $SRClass -Filter "Id -eq $pId" |Select -First 1
        $NewGUID = ([guid]::NewGuid()).ToString()
        $Projection = @{__CLASS = "System.WorkItem.ServiceRequest";
                        __SEED = $SRObject;
                        AnalystCommentLog = @{__CLASS = "System.WorkItem.TroubleTicket.AnalystCommentLog";
                                              __OBJECT = @{Id = $NewGUID;
                                                           DisplayName = $NewGUID;
                                                           Comment = $pComment;
                                                           EnteredBy  = $pEnteredBy;
                                                           EnteredDate = (Get-Date).ToUniversalTime();
                                                           IsPrivate = $false
                                                          }
                                             }
                       }
        New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection" -Projection $Projection
    }
    
    Add-SRComment -Id "SR4807" -Comment "This is a test" -EnteredBy "Doe, John"

    Wednesday, June 12, 2013 2:03 AM
  • Thank you for replying with this.  I tried the code you have above but now I get a different error.  Please reply if you have something else to try on this.  Thank you.

    Cannot overwrite variable pId because it is read-only or constant.

    At C:\Scripts\TestPS.ps1:27 char:1

    + Add-SRComment -Id "SR4807" -Comment "This is a test" -EnteredBy "Doe, John"

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo : WriteError: (pId:String) [], SessionStateUnauthorizedAccessException

    + FullyQualifiedErrorId : VariableNotWritable

    Wednesday, June 12, 2013 6:19 PM
  • Sorry, just have tried it before posting it, there you go:

    function Add-SRComment {
        param (
                [parameter(Mandatory=$true,Position=0)][Alias('Id')][String]$pSRId,
                [parameter(Mandatory=$true,Position=1)][Alias('Comment')][String]$pComment,
                [parameter(Mandatory=$true,Position=2)][Alias('EnteredBy')][String]$pEnteredBy
              )
        $SRClass = Get-SCSMClass System.WorkItem.ServiceRequest$
        $SRObject = Get-SCSMObject -Class $SRClass -Filter "Id -eq $pSRId" |Select -First 1
        if ($SRObject) {
            $NewGUID = ([guid]::NewGuid()).ToString()
            $Projection = @{__CLASS = "System.WorkItem.ServiceRequest";
                            __SEED = $SRObject;
                            AnalystCommentLog = @{__CLASS = "System.WorkItem.TroubleTicket.AnalystCommentLog";
                                                  __OBJECT = @{Id = $NewGUID;
                                                               DisplayName = $NewGUID;
                                                               Comment = $pComment;
                                                               EnteredBy  = $pEnteredBy;
                                                               EnteredDate = (Get-Date).ToUniversalTime();
                                                               IsPrivate = $false
                                                              }
                                                 }
                           }
            New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection" -Projection $Projection
        } else {
            Write-Host $pSRId "could not be found"
        }
    }
    
    Add-SRComment -Id "SR4807" -Comment "This is a test" -EnteredBy "Doe, John"

    Thursday, June 13, 2013 2:48 AM
  • That worked!  Thank you very much for taking the time to provide this.  This will be very useful for me.  Thanks!
    Thursday, June 13, 2013 2:48 PM
  • Hi German

    I've tried to run this code in my own environment and I'm getting the following error:

     

    New-SCSMObjectProjection : Simple object value was not the proper type.
    At line:23 char:9
    +         New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (27/02/2014 14:19:45:DateTime) [New-SCSMObjectProjection], InvalidSimpleObjectVal 
       ueException
        + FullyQualifiedErrorId : Could not assign date ,SMLets.NewSCSMObjectProjectionCommand

    New-SCSMObjectProjection : A value was not supplied for a required property.
    At line:23 char:9
    +         New-SCSMObjectProjection -Type "System.WorkItem.ServiceRequestProjection ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Microsoft.Enter...bjectProjection:EnterpriseManagementObjectProjection) [New-S 
       CSMObjectProjection], DiscoveryDataInvalidNullPropertyValueException
        + FullyQualifiedErrorId : projection commit failure,SMLets.NewSCSMObjectProjectionCommand

    Any ideas what the issue might be?

    Thursday, February 27, 2014 2:34 PM