Adding comments to SRs through PowerShell

I'm looking into a way of adding a comment for a Service Request through PowerShell... and getting the membership error already explained here: http://blogs.technet.com/b/servicemanager/archive/2013/01/16/creating-membership-and-hosting-objects-relationships-using-new-scsmobjectprojection-in-smlets.aspx#3563888

I understand the example of creating an incident and including one comment by using a projection, but what's the alternative when you already have a work item and you just want to add a new comment to the action log via PowerShell?

Does anybody know how to do it?

Thanks!

German

April 8th, 2013 9:01pm

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"


Free Windows Admin Tool Kit Click here and download it now
April 9th, 2013 1:15am

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"


April 9th, 2013 1:15am

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

Free Windows Admin Tool Kit Click here and download it now
May 31st, 2013 4:14pm

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"

June 12th, 2013 2:03am

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

Free Windows Admin Tool Kit Click here and download it now
June 12th, 2013 6:19pm

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"

June 13th, 2013 2:48am

That worked!  Thank you very much for taking the time to provide this.  This will be very useful for me.  Thanks!
Free Windows Admin Tool Kit Click here and download it now
June 13th, 2013 2:48pm

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?

  • Proposed as answer by Sinclair A B 18 hours 19 minutes ago
February 27th, 2014 2:34pm

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?

  • Proposed as answer by Sinclair A B Tuesday, April 14, 2015 12:55 PM
Free Windows Admin Tool Kit Click here and download it now
February 27th, 2014 2:34pm

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?

  • Proposed as answer by Sinclair A B Tuesday, April 14, 2015 12:55 PM
February 27th, 2014 2:34pm

The Problem that Joseph is having will appear on any System outside the USA!  Microsoft only handled the culture for en-US, on any other System culture you get the error like JosephFenly posted.

As each powershell command is a new thread, you cannot simply set the current culture to en-US and have done.  Fortunately the solution is relatively easy...

Encompas the code of the function itself in the following brackets:

&{ # Set thread current culture for Excel [threading.thread]::CurrentThread.CurrentCulture = 'en-US'

# Code goes here }


Therefore the function should look like this:

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
          )
    
    &{
        # Set thread current culture
        [threading.thread]::CurrentThread.CurrentCulture = 'en-US'

        $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
    }
}


Free Windows Admin Tool Kit Click here and download it now
April 14th, 2015 9:04am

The Problem that Joseph is having will appear on any System outside the USA!  Microsoft only handled the culture for en-US, on any other System culture you get the error like JosephFenly posted.

As each powershell command is a new thread, you cannot simply set the current culture to en-US and have done.  Fortunately the solution is relatively easy...

Encompas the code of the function itself in the following brackets:

&{ # Set thread current culture for Excel [threading.thread]::CurrentThread.CurrentCulture = 'en-US'

# Code goes here }


Therefore the function should look like this:

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
          )
    
    &{
        # Set thread current culture
        [threading.thread]::CurrentThread.CurrentCulture = 'en-US'

        $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
    }
}


  • Edited by Sinclair A B Tuesday, April 14, 2015 1:37 PM
  • Proposed as answer by Sinclair A B Tuesday, April 14, 2015 2:07 PM
April 14th, 2015 1:01pm

The Problem that Joseph is having will appear on any System outside the USA!  Microsoft only handled the culture for en-US, on any other System culture you get the error like JosephFenly posted.

As each powershell command is a new thread, you cannot simply set the current culture to en-US and have done.  Fortunately the solution is relatively easy...

Encompas the code of the function itself in the following brackets:

&{ # Set thread current culture for Excel [threading.thread]::CurrentThread.CurrentCulture = 'en-US'

# Code goes here }


Therefore the function should look like this:

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
          )
    
    &{
        # Set thread current culture
        [threading.thread]::CurrentThread.CurrentCulture = 'en-US'

        $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
    }
}


  • Edited by Sinclair A B Tuesday, April 14, 2015 1:37 PM
  • Proposed as answer by Sinclair A B Tuesday, April 14, 2015 2:07 PM
Free Windows Admin Tool Kit Click here and download it now
April 14th, 2015 1:01pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics