Created
February 4, 2019 19:44
-
-
Save ninegrid/7895c3d4510fbe86d5a18437c5183050 to your computer and use it in GitHub Desktop.
send syslog with powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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