Snow-Shell / servicenow-powershell

PowerShell module to automate ServiceNow service and asset management. This module can be used standalone, with Azure Automation, or Docker.
Apache License 2.0
359 stars 170 forks source link

JSON Square Brackets Added to Array's First and Last Objects #260

Closed tehfonz closed 2 months ago

tehfonz commented 8 months ago

Environment

Operating System: Windows Server 2016
ServiceNow module version: 4.0.3
PowerShell version: 5.1

Steps to reproduce

$CIs = @('Server1', 'Server2')
$UpdateChangeRequestDetails = @{
    u_affected_ci_list = $CIs
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

Expected behavior

Field in ServiceNow should just be populated with Server1 & Server2

Actual behavior

Field in ServiceNow is populated with [Server1 & Server2]

Screenshots

image image

Not sending an array and instead casting a single object as a string removes the brackets

[string]$CIs = 'Server1'
$UpdateChangeRequestDetails = @{
    u_affected_ci_list = $CIs
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

image

Sending a variable cast as a string with "array" objects separated by a comma adds them to the field separated by a line (i.e. unique entries) and no square brackets:

[string]$CIs = 'Server1, Server2'
$UpdateChangeRequestDetails = @{
    u_affected_ci_list = $CIs
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

image

So if the variable is an array:

[array]$CIs = @('Server1', 'Server2')
[string]$CIsString = $CIs -join', '
$UpdateChangeRequestDetails = @{
    u_affected_ci_list = $CIsString
}
Update-ServiceNowRecord -ID $CHG -InputData $UpdateChangeRequestDetails -Verbose

Works, sending the data without brackets. Unsure what you can do with this info, I can build my script around it, not sure if this matters to anyone else or if I'm just using the function incorrectly.

Cheers _Fonz

gdbarron commented 7 months ago

Hi @tehfonz, this was great info, thanks.

From my research, although SN says it wants json for the payload, that's not applicable to List based fields. Values for those fields are just comma separated and not arrays which add [ ] when converting to json. Converting to a string with [string]$CIs = 'Server1, Server2' has a value of Server1, Server2 which is what SN wants.

I'll give some thought to the best way to handle this and update you when I've come up with a solution.

gdbarron commented 6 months ago

@tehfonz would you mind testing the fix?

tehfonz commented 3 months ago

Hey! So sorry for the long delay. I have finally been able to test this, however I'm not sure how as I have no updated module or code? It looks like this fix was blocked? Am I reading that wrong?

gdbarron commented 2 months ago

My turn to apologize for the long delay. The updated code is in the PR, https://github.com/Snow-Shell/servicenow-powershell/pull/261/files.