Public/Write-Statsd.ps1

Function Write-StatsD {
    <#
        .SYNOPSIS
            Send metrics to a statsd server via UDP for writing to Influx.

        .DESCRIPTION
            PowerShell cmdlet to send metric data to a statsd server. Unless server or port is passed, uses the default 127.0.0.1 and port of 8125.
    
        .PARAMETER InputObject
            A metric object (generated by one of the Get-*Metric cmdlets from this module) which can be provided as pipeline input
            and will be automatically converted to StatsD strings.

        .PARAMETER Data
            Metric data to send to statsd. If string is not enclosed in quotes (single or double), the pipe character needs to be escaped.
    
        .PARAMETER IP
            IP address for statsd server
    
        .PARAMETER Port
            Port that statsd server is listening to
    
        .EXAMPLE
            Write-StatsD 'my_metric:123|g'

        .EXAMPLE
            Write-StatsD 'my_metric:321|g' -ip 10.0.0.10 -port 8180

        .EXAMPLE
            'my_metric:1|c' | Write-StatsD
    #>

    [cmdletbinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    param(
        [Parameter(ParameterSetName = 'MetricObject', Mandatory = $True, ValueFromPipeline = $True, Position = 0)]
        [PSTypeName('Metric')]
        [PSObject[]]
        $InputObject,
        
        [parameter(ParameterSetName = 'StatsDString', Mandatory = $True, ValueFromPipeline = $True, Position = 0)]
        [string[]]
        $Data,

        [ipaddress]
        $IP = '127.0.0.1',
  
        [int]
        $Port = 8125
    )
    Process {
        if ($InputObject) {
            $Data = $InputObject | ConvertTo-StatsDString
        }

        foreach ($Item in $Data) {

            if ($PSCmdlet.ShouldProcess("$($IP):$Port", "$($MyInvocation.MyCommand) -Data $Item")) {
                $Item | Invoke-UDPSendMethod -IP $IP -Port $Port
            }
        }
    } 
}