4

Should I use convert-tojson or roll my own. Should I use invoke-restrequest, invokeweb-request, System.net.WebClient or system.net.http.HttpClient?

Sometimes I seem to post successfully, although the data isn't indexed.

{"_index":"dropstorage","_type":"connectionstats","_id":"97e156a5-4d16-48d4-84e8-fcc723aea1ae",
"_version":1,"found":true,"_source":{
"hnas":  1,
"active":  70,
"id":  "97e156a5-4d16-48d4-84e8-fcc723aea1ae",
"total":  "70",
"date":  "4/9/2015 9:03:36 AM"
}}
Drakes
  • 23,254
  • 3
  • 51
  • 94
Paul Miller
  • 513
  • 1
  • 6
  • 11

3 Answers3

4

I would recommend the following approach:

$body = ConvertTo-Json $curatorResult -Compress
Invoke-RestMethod -Method Post -Uri $uri -ContentType 'application/json'-Body $body -ErrorAction Stop | Out-Null
  • 2
    Also fun fact. convertto-json doesn't do a convert get-date in a way that elastic search likes. – Paul Miller Apr 09 '15 at 17:45
  • 2
    Apprarently you want it in iso 8601 cause that's what newtonsoft does. [netwonsoft](http://www.newtonsoft.com/json/help/html/DatesInJSON.htm) and [example](http://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-a-iso-8601-date-in-string-format) – Paul Miller Apr 09 '15 at 18:15
1

Here is a walkthrough for using Elasticsearch from first-principles using PowerShell. There are notes on when to use GET and POST, how to do serialization, mass data generation, and how to interact with many calls directly.

Near the start, you'll find a discussion of making PowerShell calls compatible with Elasticsearch (yes, there's a trick).

Learning Elasticsearch with PowerShell

David Betz
  • 377
  • 4
  • 17
0

Check out Elastic.Console, a small PowerShell module for working with Elasticsearch using PowerShell and PowerShell Core. It offers autocompletion on API endpoint paths and methods for a chosen Elasticsearch version.

After installing with

Install-Module -Name Elastic.Console -AllowPrerelease

You can send data with

$body = ConvertTo-Json $curatorResult -Compress
$response = es -Method POST "/{index}/_doc/{id}" -Body $body

The request body can be a JSON string literal, a Hashtable, or a path to a file containing JSON.

Run the following to see full docs and examples

Get-Help es -Full
Russ Cam
  • 124,184
  • 33
  • 204
  • 266