<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: PowerShell sample for HTTP Event Collector in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/510868#M86806</link>
    <description>&lt;P&gt;Often that error ties to TLS settings.&amp;nbsp;Force TLS1.2 with this command :&lt;BR /&gt;&lt;BR /&gt;[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jul 2020 17:35:19 GMT</pubDate>
    <dc:creator>sara_mason</dc:creator>
    <dc:date>2020-07-24T17:35:19Z</dc:date>
    <item>
      <title>PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222277#M43606</link>
      <description>&lt;P&gt;I've been studying and creating several pieces of code to take advantage of the wonders of the HTTP Event Collector and noticed noone published a PowerShell sample, then since I created one I decided to share it with you all:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$response = ""
$formatteddate = "{0:MM/dd/yyyy hh:mm:sstt zzz}" -f (Get-Date)
$arraySeverity = 'INFO','WARN','ERROR'
$severity = $arraySeverity[(Get-Random -Maximum ([array]$arraySeverity).count)]

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Splunk 653C164D-0AFB-4DFC-ADE0-D9084B03490F')

$body = '{
        "host":"' + $env:computername + '",
        "sourcetype":"testevents",
        "source":"Geoff''s PowerShell Script",
        "event":{
            "message":"Something Happened on host ' + $env:computername + '",
            "severity":"' + $severity + '",
            "user": "'+ $env:username + '",
            "date":"' + $formatteddate + '"
            }
        }'

$splunkserver = "http://yoursplunkserver.com:8088/services/collector/event"
$response = Invoke-RestMethod -Uri $splunkserver -Method Post -Headers $headers -Body $body
"Code:'" + $response.code + "' text:'"+ $response.text + "'" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The key to the communication is the "&lt;STRONG&gt;Invoke-RestMethod&lt;/STRONG&gt;" command, which is capable of performing the http call necessary to communicate with Splunk. Authentication headers for this method must be passed as a dictionary object so I created a collection of one to define the Authorization token. Everything else is pretty much straightforward, just the same as using curl.&lt;/P&gt;

&lt;P&gt;Since PowerShell is JSON-friendly, you can capture the JSON response directly and use as variables like I did on the last line and perform some error treatment if necessary. If everything is right, this script should return something like "&lt;STRONG&gt;Code:'0' text:'Success'&lt;/STRONG&gt;"&lt;/P&gt;

&lt;P&gt;Thanks to &lt;STRONG&gt;Glenn Block&lt;/STRONG&gt; for posting useful articles on the HTTP Event Collector and providing invaluable help on this and C# SDK.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 15:15:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222277#M43606</guid>
      <dc:creator>gmartins_splunk</dc:creator>
      <dc:date>2016-02-25T15:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222278#M43607</link>
      <description>&lt;P&gt;Nice one. I have some comments to simplify the example:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Try Get-Date's -format parameter, it's easier than doing using a string formatter as you did in line 2. It takes the same .NET datetime format strings.&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Speaking of format strings, try this one: "o". Yup, one character, and the output is very easy for Splunk to parse.&lt;/P&gt;

&lt;P&gt;PS C:\Users\hal&amp;gt; Get-Date -format o&lt;BR /&gt;
2016-02-25T10:52:20.8004451-08:00&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Line 4: nothing wrong with it, but casting to [array] isn't necessary, nor are those parens. This would work fine: &lt;CODE&gt;... -Max $arraySeverity.Count&lt;/CODE&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;My favorite tip here, you don't need to make .NET dictionaries! A regular PowerShell hashtable will be coerced correctly. Example:&lt;/P&gt;

&lt;P&gt;$header = @{Authorization = "Splunk $token"}&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Last but not least, hashtables are also great for making your JSON input, along with the ConvertTo-Json cmdlet. This eliminates the need for some of the syntax bits you used. (Although if you do this as a one-liner, you will need to terminate each assignment with a semi-colon. I left them out here because CRLF does the trick.)&lt;BR /&gt;
$event = @{&lt;BR /&gt;
    host = $env:COMPUTERNAME&lt;BR /&gt;
    sourcetype = "testevents"&lt;BR /&gt;
    event = @{&lt;BR /&gt;
        message = "something happened"&lt;BR /&gt;
        severity = $severity&lt;BR /&gt;
    }&lt;BR /&gt;
} | ConvertTo-Json&lt;BR /&gt;
Output:&lt;/P&gt;

&lt;P&gt;{&lt;BR /&gt;
    "host":  "DESKTOP-S7US6VG",&lt;BR /&gt;
    "event":  {&lt;BR /&gt;
                  "message":  "something happened",&lt;BR /&gt;
                  "severity":  null&lt;BR /&gt;
              },&lt;BR /&gt;
    "sourcetype":  "testevents"&lt;BR /&gt;
}&lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Other notes:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;PowerShell (technically, the underlying .NET methods) hates self-signed certificates. Best to test with SSL off on your Splunk event collector settings, and if using SSL in production, use a real certificate. For much more detail and workarounds, see @jaykul post/code about it: &lt;A href="https://github.com/Jaykul/Tunable-SSL-Validator"&gt;https://github.com/Jaykul/Tunable-SSL-Validator&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Thu, 25 Feb 2016 19:17:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222278#M43607</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-02-25T19:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222279#M43608</link>
      <description>&lt;P&gt;Here's what I came up with for a very basic example taking the above into account:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$token = "C3AD6A0B-2499-4070-AA51-765640DB9107"
$server = "192.168.99.101"
$port = 32771
$url = "http://${server}:$port/services/collector/event"
$header = @{Authorization = "Splunk $token"}
$event = @{event = "hello world"} | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri $url -Headers $header -Body $event
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note: the reason there are curly braces around "server" above is to prevent the following colon from being picked up as a scope modifier. Otherwise it would parse to &lt;CODE&gt;$server:32271&lt;/CODE&gt; which is valid syntax, but would result in an empty result.&lt;/P&gt;

&lt;P&gt;And the one-liner to compete with curl!&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;PS &amp;gt; irm -Method Post -Uri "http://192.168.99.101:32771/services/collector/event" -Headers @{Authorization = "Splunk C3AD6A0B-2499-4070-AA51-765640DB9107"} -Body '{"event": "hello world"}'

text    code
----    ----
Success    0
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Feb 2016 19:23:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222279#M43608</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-02-25T19:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222280#M43609</link>
      <description>&lt;P&gt;Great remarks and modifications to my humble original example. great job @halr9000 !&lt;/P&gt;</description>
      <pubDate>Thu, 25 Feb 2016 20:32:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222280#M43609</guid>
      <dc:creator>gmartins_splunk</dc:creator>
      <dc:date>2016-02-25T20:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222281#M43610</link>
      <description>&lt;P&gt;You get all the credit for doing it first, and inspiring me to add some polish. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Feb 2016 03:15:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222281#M43610</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-02-26T03:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222282#M43611</link>
      <description>&lt;P&gt;Hi @halr9000 ,&lt;/P&gt;

&lt;P&gt;I was able to send test events using the below command few days back.&lt;/P&gt;

&lt;P&gt;irm -Method Post -Uri "&lt;A href="https://URL.com/services/collector/event"&gt;https://URL.com/services/collector/event&lt;/A&gt;" -Headers @{Authorization = "Splunk token"} -Body '{"event": "test1 "}'&lt;/P&gt;

&lt;P&gt;But when I tried sending a test event today it gave me an error.&lt;/P&gt;

&lt;P&gt;irm : The underlying connection was closed: An unexpected error occurred on a send.&lt;BR /&gt;
At line:1 char:1&lt;BR /&gt;
+ irm -Method Post -Uri "&lt;A href="https://URL.com/"&gt;https://URL.com/&lt;/A&gt; ...&lt;BR /&gt;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;BR /&gt;
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebE&lt;BR /&gt;
eption&lt;BR /&gt;
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand&lt;/P&gt;

&lt;P&gt;Any idea what could be causing this?&lt;/P&gt;

&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 11:29:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222282#M43611</guid>
      <dc:creator>Arpit_S</dc:creator>
      <dc:date>2019-09-03T11:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222283#M43612</link>
      <description>&lt;P&gt;Token expired? Try getting more information from your error. Look here: &lt;A href="https://stackoverflow.com/questions/38419325/catching-full-exception-message"&gt;https://stackoverflow.com/questions/38419325/catching-full-exception-message&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2019 12:36:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222283#M43612</guid>
      <dc:creator>efavreau</dc:creator>
      <dc:date>2019-09-03T12:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222284#M43613</link>
      <description>&lt;P&gt;The is working fine when sending data using curl command from Linux OS.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2019 09:32:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/222284#M43613</guid>
      <dc:creator>Arpit_S</dc:creator>
      <dc:date>2019-09-10T09:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: PowerShell sample for HTTP Event Collector</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/510868#M86806</link>
      <description>&lt;P&gt;Often that error ties to TLS settings.&amp;nbsp;Force TLS1.2 with this command :&lt;BR /&gt;&lt;BR /&gt;[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jul 2020 17:35:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/PowerShell-sample-for-HTTP-Event-Collector/m-p/510868#M86806</guid>
      <dc:creator>sara_mason</dc:creator>
      <dc:date>2020-07-24T17:35:19Z</dc:date>
    </item>
  </channel>
</rss>

