0

According to the documentation, I should enclose both values within square brackets, but this doesn't seem to work.

My code is as follows :

$params = @{
    body = @{
        "jsonrpc"= "2.0"
        "method"= "item.get"
        "params"= @{
            "host"= $HostName
            "search"= @{
            "key_"= "[vm.memory.size[total],system.cpu.num]"
            }
            "searchByAny"= "true"
        }
            "id"= 50
            "auth"="e57c8231e4d8f0f1c8d3ea2209299e65864a8c394458f670a9bc35583c19e129"
        } | ConvertTo-Json
        uri = "$baseurl/api_jsonrpc.php"
        headers = @{"Content-Type" = "application/json"}
        method = "Post"
    }
$HostDetails = $($_.HostName)

$result = Invoke-WebRequest @params -useBasicParsing
$rawdata = $result | ConvertFrom-Json
#$rawdata.result

but it doesn't work, it produces the following result -

StatusCode        : 200
StatusDescription : OK
Content           : {"jsonrpc":"2.0","result":[],"id":50}
RawContent        : HTTP/1.1 200 OK
                    Access-Control-Allow-Origin: *
                    Access-Control-Allow-Headers: Content-Type
                    Access-Control-Allow-Methods: POST
                    Access-Control-Max-Age: 1000
                    Strict-Transport-Security: max-age=315360...
Forms             :
Headers           : {[Access-Control-Allow-Origin, *], [Access-Control-Allow-Headers, Content-Type], [Access-Control-Allow-Methods,
                    POST], [Access-Control-Max-Age, 1000]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        :
RawContentLength  : 37

I tried changing the line "key_" = .... to all sorts of variants of what I have above but nothing seemed to produce any results, some threw errors.

locvez
  • 3
  • 1

1 Answers1

2

According to the documentation, I should enclose both values within square brackets

I highly doubt that - the documentation probably states you need to supply an array of keys, which in JSON would be [ "<key1>", "<key2>", ... ].

In PowerShell, you'll want to use the array subexpression operator:

@{
  "key_" = @('vm.memory.size[total]', 'system.cpu.num')
}

Once converted to JSON, it will eventually look like "key_":["vm.memory.size[total]", "system.cpu.name"]

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks for replying, yes you're right, it should be an array of keys, I mistyped what I meant in the initial question. So I had tried your suggestion previously as well but it still returns no results, when viewing $params.body the key value appears as follows : "key_": "vm.memory.size[total] system.cpu.num" I did add a "," in between the keys and it shows as "key_": "vm.memory.size[total] , system.cpu.num" – locvez Mar 14 '23 at 20:32
  • Sorry, to be clearer (I'm new to stackEX, please forgive me) I did add a ',' like follows - "key_": @('vm.memory.size[total]' ',' 'system.cpu.num') in between the keys and it shows as "key_": "vm.memory.size[total] , system.cpu.num" but can't seem to get it to show the json with with quotes before and after the comma – locvez Mar 14 '23 at 20:42
  • @locvez Change `| ConvertTo-Json` to `| ConvertTo-Json -Depth 10` to prevent the array values being "flattened". – Mathias R. Jessen Mar 14 '23 at 22:40
  • Thank you @Mathias adding -Depth 10 fixed the issue and returned the expected results in a single request. Much appreciated. – locvez Mar 15 '23 at 07:36