Thursday, November 24, 2005

Sending a SysLog Message using Monad

I’m a bit fan on using Syslog for monitoring and logging and after putting the new version of MSH on my machine that works with the release version of 2.0 .NET framework (make sure you uninstall any old versions of the monad beta before you install the new framework or your going to be in for some problems) I thought I’d have a go at seeing if I could use my C# code from this post in a monad script. After a little bit of translating and lot of learning I managed to get something that works. It’s about six lines so it’s nice and lean. The good part is you could throw it into a command-let and then use it as a one-liner in another script. The code itself looks like this. This sample sends a USER:Warning Priorty message

$SL = New-Object System.Net.Sockets.UdpClient("", 514)
$Message = "<12>date=" + [DateTime]::get_Now().tostring("yyyy-MM-dd") + ",time=" + [DateTime]::get_Now().tostring("hh:mm:ss")
$Message = $Message + ",msg=`"The Sky is Falling`""
[byte[]] $rawMSG = $(new-object System.Text.ASCIIEncoding).GetBytes($message)
$SL.Send($rawMSG, $rawMSG.Length);

1 comment:

Ravjeet Singh said...

This is exactly what I was looking for..small and slick. Thanks for sharing!