ConvertTo-Json : An item with the same key has already been added.
Closed this issue · 11 comments
Environment
PowerShell ISE
Operating System: Windows 10
ServiceNow module version: 3.0.1
PowerShell version: 5.1.19041.1151
Steps to reproduce
This was not previously receiving an error on ServiceNow module 3.0.0
Splatting $Params
$Record = New-ServiceNowRecord -Table 'Change Request' -Values $Params -PassThru
Write-Output -InputObject $Record.number
Expected behavior
No ConvertTo-Json errors
Actual behavior
Error shows twice
ConvertTo-Json : An item with the same key has already been added.
I'm getting the same error on a brand new dev instance
~
❯ New-ServiceNowSession @params
~ took 2s
❯ Get-ServiceNowRecord -Table request
ConvertTo-Json: /Users/nexxai/.local/share/powershell/Modules/ServiceNow/3.0.1/Private/Invoke-ServiceNowRestMethod.ps1:152
Line |
152 | Write-Debug ($response | ConvertTo-Json)
| ~~~~~~~~~~~~~~
| An item with the same key has already been added. Key: Content
number short_description state assigned_to approval opened_at
------ ----------------- ----- ----------- -------- ---------
REQ0000001 Open Approved 2021-02-24 12:58:44 …
REQ0010001 Open Approved 2021-09-09 7:28:49 AM
Is this on every call to Get-ServiceNowRecord? Is the user an admin?
I've run it 6 or 7 times in a row here, and yes, it happened every time.
The user is the one named "admin" when you stand up a developer instance.
Sorry if I wasn't clear. Have you tried different Get-ServiceNowRecord calls, with different parameters, and received that message each time or is it only with specific calls? Also, it's odd to be failing on a write-debug line when your command line didn't call for it. Let me try and repro.
Ahhh sorry for misunderstanding.
I just tried for -table Incident
and it happened there too.
If I comment out line 152 in the Invoke-ServiceNowRestMethod.ps1 file as the error mentions, it works without error (obviously) but what I don't understand is why it's trying to dump every single response out as JSON. It feels like that line should be wrapped in some kind of If statement, no?
Looks like it was added in this PR: https://github.com/Snow-Shell/servicenow-powershell/pull/151/files
I think there's 2 things at play. I'm seeing a different error as $response is null at times, eg. 401 unauth. That line should be a few lines down in the if ($response.content)
to solve that one.
Your issue I've seen before, but not often. Still digging in.
As background, I added that line so folks could see the raw response if they wanted. It should only show if debugging is enabled.
Replace lines 150-152 with
try {
$response = Invoke-WebRequest @params
Write-Debug $response
}
catch {
$ProgressPreference = $oldProgressPreference
throw $_
}
I think this covers all scenarios.
Looks like it works good. No more errors at least.