Part 2.[PSJSON] The Powershell JSON TCP Connection

Part 1.[Overview] ElasticSearch, Kibana, Logstash, and Windows Metrics over PowerShell TCP Connections
Part 2.[PSJSON] The Powershell JSON TCP Connection
Part 3.[PSGet-Counter] The Powershell Get-Counter cmdlet

The JSON TCP connection is as follows:

Function Send-JsonOverTcp {
    param ( [ValidateNotNullOrEmpty()]
    [string] $LogstashServer,
    [int] $Port,
    $JsonObject)
    $JsonString = $JsonObject -replace “`n”,’ ‘ -replace “`r”,’ ‘ -replace ‘ ‘,”
    $Ip = [System.Net.Dns]::GetHostAddresses($LogstashServer)
    $Address = [System.Net.IPAddress]::Parse($Ip)
    $Socket = New-Object System.Net.Sockets.TCPClient($Address,$Port)
    $Stream = $Socket.GetStream()
    $Writer = New-Object System.IO.StreamWriter($Stream)
    $Writer.WriteLine($JsonString)
    $Writer.Flush()
    $Stream.Close()
    $Socket.Close()
}

Basically, what we have here is a function called Send-JsonOverTCP that requires a few arguments.
You invoke the function as follows: “Send-JsonOverTcp stringOfLogStashServerName Port ‘stringOfPSVariable'”

This was written by willemdh at OutsideIT.net