Skip to content

Instantly share code, notes, and snippets.

@halr9000
Created June 5, 2012 15:53
Show Gist options
  • Save halr9000/2875884 to your computer and use it in GitHub Desktop.
Save halr9000/2875884 to your computer and use it in GitHub Desktop.
Splunk Utilities
function ConvertTo-SplunkString
{
[cmdletbinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[Object]$InputObject,
[Parameter()]
[System.Collections.Hashtable]$Properties
)
Process
{
$ScriptRunTime = (get-date).ToFileTime()
$InputObject | foreach-object {
$Current = $_
$output = $Current | Get-Member -MemberType Properties | foreach-object {
$Key = $_.Name
$Value = $Current.$Key -join ";"
'{0}="{1}"' -f $Key,$Value
foreach($Property in $Properties.Keys)
{
if($Key -eq $Property)
{
if($Properties[$Property] -is [system.array])
{
'{0}="{1}"' -f ($Properties[$Property][0]),($Value -replace $Properties[$Property][1],'$1')
}
else
{
'{0}="{1}"' -f $Properties[$Property],$Value
}
}
}
}
$output += '{0}="{1}"' -f "ScriptRunTime",$ScriptRunTime
$Message = ("{0:MM/dd/yyyy HH:mm:ss} GMT - {1}" -f ((get-date).ToUniversalTime()),( $output -join " " ))
Write-Verbose $Message
$Message
}
}
}
function Send-SplunkUDP
{
[cmdletbinding()]
param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true)]
[string]$Text,
[Parameter()]
[int]$Port = 8887,
[Parameter(Mandatory=$true)]
[string]$Server
)
Process
{
Write-Verbose $Text
$endpoint = New-Object System.Net.IPEndPoint([IPAddress]::Parse($Server),$Port)
$udpclient= New-Object System.Net.Sockets.UdpClient
$bytes = [Text.Encoding]::ASCII.GetBytes($Text)
$bytesSent = $udpclient.Send($bytes,$bytes.length,$endpoint)
$udpclient.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment