Add ticket comment via script

Hello,

When trying to create a ticket comment via a script i get an error.

Here’s the error:
error> System.InvalidOperationException: A circular reference was detected while serializing an object of type ‘System.Management.Automation.PSMethod’.
error> Stack:
error> at ConvertTo-Json20, C:\ProgramData\Syncro\bin\module.psm1: line 310
error> at Call-SyncroApiEx, C:\ProgramData\Syncro\bin\module.psm1: line 262
error> at Call-SyncroApi, C:\ProgramData\Syncro\bin\module.psm1: line 239
error> at Create-Syncro-Ticket-Comment, C:\ProgramData\Syncro\bin\module.psm1: line 185
error> at , C:\ProgramData\Syncro\bin\2eb2f0af-fc34-4b6f-92f1-e8d81cf5b611.ps1: line 36
error> at , : line 1
error> at , : line 1
error> Call-SyncroApi: failure
error> System.InvalidOperationException: A circular reference was detected while serializing an object of type ‘System.Management.Automation.PSMethod’.
error> Stack:
error> at ConvertTo-Json20, C:\ProgramData\Syncro\bin\module.psm1: line 310
error> at Call-SyncroApiEx, C:\ProgramData\Syncro\bin\module.psm1: line 262
error> at Call-SyncroApi, C:\ProgramData\Syncro\bin\module.psm1: line 242
error> at Create-Syncro-Ticket-Comment, C:\ProgramData\Syncro\bin\module.psm1: line 185
error> at , C:\ProgramData\Syncro\bin\2eb2f0af-fc34-4b6f-92f1-e8d81cf5b611.ps1: line 36
error> at , : line 1
error> at , : line 1
error> Call-SyncroApi: failure

Here’s my code:
$Ticket = Create-Syncro-Ticket -Subject ‘Brute force attempt’ -IssueType ‘Security’ -Status ‘New’

Create-Syncro-Ticket-Comment -TicketIdOrNumber $ticket.ticket.id -Subject “Comment” -Body $item -Hidden $false -DoNotEmail $true

$ticket.ticket.id does return a valid ticket number

Thanks,
Ernst.

It may be whatever you have set for $item. I just changed that to ‘Test’ and the code works fine for me. After replacing the smart quotes, but I’m assuming this forum introduced those, not your original code. Use the preformatted text button in the future to avoid that plus gives syntax highlighting. Whatever is going into $item may need to be piped to Out-String or similar to sanitize it.

Syncro’s custom JSON functions are REALLY finicky. I just ran into this and found piping w/e I was working with to | Out-String was doing windows line endings and their cmdlet doesn’t support it.

The way I found the difference is by comparing the output of

$hereString = @'
This is and example
multiline string
'@
$hereString | convertto-json

VS

$item | convertto-json

EDIT: I looked up my ticket history and found the example code. Piping to out-string from my cmdlet was giving me line endings that translated to ‘\r\r\n’ in json, but a HereString just uses ‘\n’ so the fix should be

$bodyText = $item.Replace("`r","")
1 Like