document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. function Send-SyslogMessage
  2. {
  3. <#
  4. .SYNOPSIS
  5. Sends a SYSLOG message to a server running the SYSLOG daemon
  6.  
  7. .DESCRIPTION
  8. Sends a message to a SYSLOG server as defined in RFC 5424. A SYSLOG message contains not only raw message text,
  9. but also a severity level and application/system within the host that has generated the message.
  10.  
  11. .PARAMETER Server
  12. Destination SYSLOG server that message is to be sent to
  13.  
  14. .PARAMETER Message
  15. Our message
  16.  
  17. .PARAMETER Severity
  18. Severity level as defined in SYSLOG specification, must be of ENUM type Syslog_Severity
  19.  
  20. .PARAMETER Facility
  21. Facility of message as defined in SYSLOG specification, must be of ENUM type Syslog_Facility
  22.  
  23. .PARAMETER Hostname
  24. Hostname of machine the mssage is about, if not specified, local hostname will be used
  25.  
  26. .PARAMETER Timestamp
  27. Timestamp, myst be of format, "yyyy:MM:dd:-HH:mm:ss zzz", if not specified, current date & time will be used
  28.  
  29. .PARAMETER UDPPort
  30. SYSLOG UDP port to send message to
  31.  
  32. .INPUTS
  33. Nothing can be piped directly into this function
  34.  
  35. .OUTPUTS
  36. Nothing is output
  37.  
  38. .EXAMPLE
  39. Send-SyslogMessage mySyslogserver "The server is down!" Emergency Mail
  40. Sends a syslog message to mysyslogserver, saying "server is down", severity emergency and facility is mail
  41.  
  42. .NOTES
  43. NAME: Send-SyslogMessage
  44. AUTHOR: Kieran Jacobsen
  45. LASTEDIT: 2014 07 01
  46. KEYWORDS: syslog, messaging, notifications
  47.  
  48. .LINK
  49. https://github.com/kjacobsen/PowershellSyslog
  50.  
  51. .LINK
  52. http://aperturescience.su
  53.  
  54. #>
  55. [CMDLetBinding()]
  56. Param
  57. (
  58.     [Parameter(mandatory=$true)] [String] $Server,
  59.     [Parameter(mandatory=$true)] [String] $Message,
  60.     [Parameter(mandatory=$true)] [Syslog_Severity] $Severity,
  61.     [Parameter(mandatory=$true)] [Syslog_Facility] $Facility,
  62.     [String] $Hostname,
  63.     [String] $Timestamp,
  64.     [int] $UDPPort = 514
  65. )
  66.  
  67. # Create a UDP Client Object
  68. $UDPCLient = New-Object System.Net.Sockets.UdpClient
  69. $UDPCLient.Connect($Server, $UDPPort)
  70.  
  71. # Evaluate the facility and severity based on the enum types
  72. $Facility_Number = $Facility.value__
  73. $Severity_Number = $Severity.value__
  74. Write-Verbose "Syslog Facility, $Facility_Number, Severity is $Severity_Number"
  75.  
  76. # Calculate the priority
  77. $Priority = ($Facility_Number * 8) + $Severity_Number
  78. Write-Verbose "Priority is $Priority"
  79.  
  80. # If no hostname parameter specified, then set it
  81. if (($Hostname -eq "") -or ($Hostname -eq $null))
  82. {
  83.     $Hostname = Hostname
  84. }
  85.  
  86. # I the hostname hasn\'t been specified, then we will use the current date and time
  87. if (($Timestamp -eq "") -or ($Timestamp -eq $null))
  88. {
  89.     $Timestamp = Get-Date -Format "yyyy:MM:dd:-HH:mm:ss zzz"
  90. }
  91.  
  92. # Assemble the full syslog formatted message
  93. $FullSyslogMessage = "<{0}>{1} {2} {3}" -f $Priority, $Timestamp, $Hostname, $Message
  94.  
  95. # create an ASCII Encoding object
  96. $Encoding = [System.Text.Encoding]::ASCII
  97.  
  98. # Convert into byte array representation
  99. $ByteSyslogMessage = $Encoding.GetBytes($FullSyslogMessage)
  100.  
  101. # If the message is too long, shorten it
  102. if ($ByteSyslogMessage.Length -gt 1024)
  103. {
  104.     $ByteSyslogMessage = $ByteSyslogMessage.SubString(0, 1024)
  105. }
  106.  
  107. # Send the Message
  108. $UDPCLient.Send($ByteSyslogMessage, $ByteSyslogMessage.Length)
  109.  
  110. }
');