In the last four parts of this series (Part I, Part II, Part III, Part IV), we discussed sending telemetry data to Splunk using HEC (HTTP Event Collector). This requires no software to be installed. We can send data for ingestion to Splunk using REST and a REST endpoint. In the previous four parts, we’ve included Indexer Acknowledgement. This set up uses a random GUID we create and send to Splunk in our initial connection using Invoke-RestMethod.
Then, in additional requests we use the GUID to continually poll Splunk to determine if the data has been more than just received, but that it’s being processed. There was much too much delay for me to consider its use and so I disabled Indexer Acknowledgment. In this post, we’ll take our final code for Part IV, below, and remove all the parts that were in place for Indexer Acknowledgment. This greatly reduces the amount of code and overall complexity seen in the previous parts of this series. Compare the two code examples below as we wrap up the series. Hopefully, if you were looking for ways to send data to Splunk using PowerShell that you found this series of articles and in time that they were helpful for you. If you’ve got Splunk available to you, then don’t forget that you have a place where data can be sent and stored after it’s been collected with PowerShell. And it’s not just about data storage. In fact, that’s such a small portion of the benefits to Splunk. If you’re collecting good data, then there’s nothing you can’t find by searching the data.
With Indexer Acknowledgement
#region: Read .env file and create environment variables. $FilterScript = {$_ -ne '' -and $_ -notmatch '^#'} $Content = Get-Content -Path 'C:\Users\tommymaynard\Documents\_Repos\code\functiontemplate\random\telemetry\.env' | Where-Object -FilterScript $FilterScript If ($Content) { Foreach ($Line in $Content) { $KVP = $Line -split '=',2; $Key = $KVP[0].Trim(); $Value = $KVP[1].Trim() Write-Verbose -Message "Adding an environment variable: `$env`:$Key." [Environment]::SetEnvironmentVariable($Key,$Value,'Process') } # End Foreach. } Else { '$Content variable was not set. Do things without the file/the environment variables.' } # End If. #endregion. #region: Read clixml .xml file and obtain hashtable. $HashTablePath = 'C:\Users\tommymaynard\Documents\_Repos\code\functiontemplate\random\telemetry\eventhashtable.xml' $EventHashTable = Import-Clixml -Path $HashTablePath #endregion. #region: Create Splunk / Invoke-RestMethod variables. $EventUri = $env:SplunkUrl + '/services/collector/event' $AckUri = $env:SplunkUrl + '/services/collector/ack' $ChannelIdentifier = (New-Guid).Guid $Headers = @{Authorization = "Splunk $env:SplunkHECToken"; 'X-Splunk-Request-Channel' = $ChannelIdentifier} $Body = ConvertTo-Json -InputObject $EventHashTable $HttpRequestEventParams = @{URI = $EventUri; Method = 'POST'; Headers = $Headers; Body = $Body} #endregion. #region: Make requests to Splunk REST web services. $Ack = Invoke-RestMethod @HttpRequestEventParams -StatusCodeVariable StatusCode -Verbose $StatusCode $AckBody = "{`"acks`": [$($Ack.ackId)]}" $HttpRequestAckParams = @{URI = $AckUri; Method = 'POST'; Headers = $Headers; Body = $AckBody} Measure-Command -Expression { Do { $AckResponse = Invoke-RestMethod @HttpRequestAckParams -Verbose $AckResponse.acks.0 Start-Sleep -Seconds 30 } Until ($AckResponse.acks.0 -eq $true) } # End Measure-Command #endregion.
Without Indexer Acknowledgement
#region: Read .env file and create environment variables. $FilterScript = {$_ -ne '' -and $_ -notmatch '^#'} $Content = Get-Content -Path 'C:\Users\tommymaynard\Documents\_Repos\code\functiontemplate\random\telemetry\.env' | Where-Object -FilterScript $FilterScript If ($Content) { Foreach ($Line in $Content) { $KVP = $Line -split '=',2; $Key = $KVP[0].Trim(); $Value = $KVP[1].Trim() Write-Verbose -Message "Adding an environment variable: `$env`:$Key." [Environment]::SetEnvironmentVariable($Key,$Value,'Process') } # End Foreach. } Else { '$Content variable was not set. Do things without the file/the environment variables.' } # End If. #endregion. #region: Read clixml .xml file and obtain hashtable. $HashTablePath = 'C:\Users\tmaynard\Documents\_Repos\code\functiontemplate\random\telemetry\eventhashtable.xml' $EventHashTable = Import-Clixml -Path $HashTablePath #endregion. #region: Create Splunk / Invoke-RestMethod variables. $EventUri = $env:SplunkUrl + '/services/collector/event' $Headers = @{Authorization = "Splunk $env:SplunkHECToken"} $Body = ConvertTo-Json -InputObject $EventHashTable $HttpRequestEventParams = @{URI = $EventUri; Method = 'POST'; Headers = $Headers; Body = $Body} #endregion. #region: Make requests to Splunk REST web services. Invoke-RestMethod @HttpRequestEventParams -StatusCodeVariable StatusCode -Verbose $StatusCode