0

I'm attempting to send a MessageCard to Microsoft Teams and in doing so I need to construct JSON out of many variables. My issue is that a nested array inside my hashtable shows up with the object's type rather than it's value:

PowerShell:

$jsonHash = [ordered]@{
    '@type' = "MessageCard"
    '@context' = "https://schema.org/extensions"
    'summary' = $SummaryText
    'themeColor' = $ThemeColor
    'title' = $Title                
    'sections' = @(
        [ordered]@{
            'activityTitle' = $ActivityTitle
            'activitySubTitle' = $ActivitySubTitle
            'activityImage' = $ActivityImageUrl
            'text' = $BodyText
            'facts' = @(
                [ordered]@{
                    'name' = 'Computer'
                    'value' = $env:COMPUTERNAME
                },
                [ordered]@{
                    'name' = 'IP Address'
                    'value' = $((Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -ne "Disconnected"}).IPv4Address.IPAddress)
                }
            )
        }                                        
    )
    'potentialAction' = @(
        [ordered]@{
            '@type' = 'HttpPOST'
            'name' = 'Activate'
            'target' = $targetUrl
        }
    )                
}
return $jsonHash | ConvertTo-Json

The output of this shows the facts tag as System.Collections.Specialized.OrderedDirectionary and not the correct JSON output I would expect:

JSON Output:

{
"@type":  "MessageCard",
"@context":  "https://schema.org/extensions",
"summary":  "Node Activation for TESTPC",
"themeColor":  "0078D7",
"title":  null,
"sections":  [
                 {
                     "activityTitle":  "Network Node TESTPC requesting activation",
                     "activitySubTitle":  null,
                     "activityImage":  "<removed>",
                     "text":  "Some text here",
                     "facts":  "System.Collections.Specialized.OrderedDictionary System.Collections.Specialized.OrderedDictionary"
                 }
             ],
"potentialAction":  [
                        {
                            "@type":  "HttpPOST",
                            "name":  "Activate",
                            "header":  "System.Collections.Hashtable",
                            "target":  "<removed>"
                        }
                    ]
}

Even the header value shows up as System.Collections.Hashtable instead of the correct value.

Any ideas on how I can get the JSON formatted correctly with this complex object?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Jason Shave
  • 2,462
  • 2
  • 29
  • 47
  • 1
    Use `[pscustomobject]` instead in conjunction with `ConvertTo-Json` – Maximilian Burszley Nov 09 '18 at 16:41
  • I am sorry @TheIncorrigible1, but I do not agree: there is already an `[ordered]` object at the `sections` level (which does work)... – iRon Nov 09 '18 at 17:50
  • 1
    I think you should use the `-Depth` parameter on the `ConvertTo-Json` cmdlet. This parameter defaults to a value of 2, so I suggest you change that to a much higher (int32) value. – Theo Nov 09 '18 at 19:38
  • Ya -Depth fixed it. Why would you not set it to 99? – Jason Shave Nov 09 '18 at 19:49
  • That is also discussed in one of the answers of the duplicate, see: https://stackoverflow.com/a/37206962/1701026 – iRon Nov 09 '18 at 22:57

0 Answers0