Skip to content

Instantly share code, notes, and snippets.

@ninegrid
Created February 4, 2019 19:44
Show Gist options
  • Save ninegrid/7895c3d4510fbe86d5a18437c5183050 to your computer and use it in GitHub Desktop.
Save ninegrid/7895c3d4510fbe86d5a18437c5183050 to your computer and use it in GitHub Desktop.
send syslog with powershell
Function Send-Syslog {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)] [string]$ServerName,
[Parameter(Mandatory=$false)] [string]$Severity = '1',
[Parameter(Mandatory=$false)] [string]$Facility = '22',
[Parameter(Mandatory=$false)] [string]$HostName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false, ValueFromPipeline=$true)][string]$Message = 'From Powershell'
)
$Priority = ([int]$Facility * 8) + [int]$Severity
$Timestamp = Get-Date -Format "MMM dd HH:mm:ss"
$FullSyslogMessage = "<{0}>{1} {2} {3}" -f $Priority, $Timestamp, $HostName, $Message
$Encoding = [System.Text.Encoding]::ASCII
$ByteSyslogMessage = $Encoding.GetBytes($FullSyslogMessage)
$UDPClient = New-Object System.Net.Sockets.UdpClient
$UDPClient.Connect($ServerName, 514)
$UDPClient.Send($ByteSyslogMessage, $ByteSyslogMessage.Length)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment