PowerCLI

 View Only
  • 1.  Write Points from file to InfluxDB using Invoke-RestMethod

    Posted May 08, 2018 11:59 AM

    With respect to Writing data with the HTTP API | InfluxData Documentation we can use curl to post multiple points from file to insert to influxdb

    [ curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt ]

    As I am using PowerCLI script to post points to influxdb using

    [ Invoke-RestMethod -Uri 'http://localhost:8086/write?&db=DBName' -Method Post -Body "measurement-name,tag-name=tag-value value-name=value" ]

    I want know the similar syntax in Invoke-RestMethod to post from file.



  • 2.  RE: Write Points from file to InfluxDB using Invoke-RestMethod
    Best Answer

    Posted May 08, 2018 12:29 PM

    Afaik that is not possible.

    I loop through the file like this

    Get-Content -Path .\cpu_data.txt | %{

        Invoke-WebRequest 'http://localhost:8086/write?db=DBName' -Method POST -Body $_

    }



  • 3.  RE: Write Points from file to InfluxDB using Invoke-RestMethod

    Posted May 08, 2018 01:08 PM

    But, This will take lot of time to insert if the file has 3000+ lines.

    Is there any alternative way to post all lines at one go?



  • 4.  RE: Write Points from file to InfluxDB using Invoke-RestMethod

    Posted May 08, 2018 01:42 PM

    I know, I suffer from the same :smileygrin:

    As an alternative, when using PowerShell Core/PowerCLI Core, you can run your script on a Linux platform.

    And then call curl from your script.

    Or us a curl clone for Windows (but I have no experience with any of those).



  • 5.  RE: Write Points from file to InfluxDB using Invoke-RestMethod

    Posted May 09, 2018 10:36 AM

    I am trying to do work around process

    $postdata = Get-Content -Path ./file.text

    >$postdata

    [ testtime value=2.2 1457628961\ntesttime value=2.3 1457629321 ]

    Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "$postdata"

    or

    Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "testtime value=2.2 1457628961\ntesttime value=2.3 1457629321"

    Either of way it is inserting only a single point into influxdb not two



  • 6.  RE: Write Points from file to InfluxDB using Invoke-RestMethod

    Posted May 15, 2018 06:20 AM

    I have tried to find some alternative way, but not fully successful.

    1. I am able to use Invoke-RestMethod to insert all the points from powercli window using:

         Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "testtime value=2.2 1457628961`ntesttime value=2.3 1457629321"

        

    2. But, if the same text [ testtime value=2.2 1457628961`ntesttime value=2.3 1457629321 ], I write to a file and later read it using Get-Content like,

         $post = Get-Content -Path post.txt

         Invoke-RestMethod -Uri "http://localhost:8086/write?&db=vipl" -Method Post -Body "$post"

    it shows me an error.

    3. Can I know how can I replace EOL with `n

         the post.txt contains

    [ testtime value=2.2 1457628961

      testtime value=2.3 1457629321 ]

    how can I make it in this format [ testtime value=2.2 1457628961`ntesttime value=2.3 1457629321 ]

    I have tried this {$post = Get-Content -Path post.txt -replace "`n`r ","``n" }

    but was unsuccessful



  • 7.  RE: Write Points from file to InfluxDB using Invoke-RestMethod

    Posted May 15, 2018 06:32 AM

    Can you try like this?

    (Get-Content -Path post.txt) | %{

       $_ -replace "`r`n",'`n'

      } | Set-Content -Path new-post.txt