Skip to content

Instantly share code, notes, and snippets.

@indented-automation
Created September 17, 2019 17:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indented-automation/8d182ad226e051b8190c58301b727a32 to your computer and use it in GitHub Desktop.
Save indented-automation/8d182ad226e051b8190c58301b727a32 to your computer and use it in GitHub Desktop.
Send a message to a SysLog instance
function Send-Syslog {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[String]$Message,
[String]$LogLevel = 'Information',
[Parameter(Mandatory)]
[IPAddress]$IPAddress,
[Switch]$Tee,
[Switch]$UseTcp
)
begin {
if ($UseTcp) {
$tcpClient = [System.Net.Sockets.TcpClient]::new()
$tcpClient.Connect($IPAddress, 514)
$stream = $tcpClient.GetStream()
}
}
process {
if ($Tee) {
$Message
}
$Message = '{0,-20} {1} {2,-15} {3}' -f
$env:COMPUTERNAME,
[DateTime]::Now.ToString('u'),
$LogLevel,
$Message
$bytes = [System.Text.Encoding]::Default.GetBytes($Message)
if ($UseTcp) {
$stream.Write($bytes, 0, $bytes.Count)
} else {
$null = [System.Net.Sockets.UdpClient]::new().Send(
$bytes,
$bytes.Count,
[System.Net.IPEndPoint]::new($IPAddress, 514)
)
}
}
end {
if ($UseTcp) {
$stream.Close()
$tcpClient.Close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment