Send Syslog Messages from PowerShell

In most cases, the scripts, functions and cmdlets we develop have to save events to a log file to make troubleshooting easier. With PowerShell, the easiest thing would be to write an event to the event log or a file.

When it comes to centralized log management, most organizations have based their strategy on the syslog server and protocol. There may be agents on the windows server machines that your code is running on to collect the messsages but that's not always the case.

So how can we send messages to a syslog server directly using PowerShell? Although that's not that hard, I've put together a cmdlet to do just that!

My General PowerShell module that is published on the Gallery contains the  Send-SyslogMessage cmdlet from version 2.12.0 onwards.

A call to the Send-SyslogMessage would be pretty much like the following:

Send-SyslogMessage -Server syslog.lab.local `
                   -Severity Error `
                   -Facility Local0 `
                   -Message "An error occured." `
                   -Application "MyScript" `
                   -Protocol TCP -Verbose

Here we specify the syslog server, the severity of the message, the facility, the application and the message itself. You just have to update the values based on your needs.

Now, depending on your Syslog server configuration, you should start getting messages from the Windows machines.

Below is a screenshot from my syslog server. A directory has been automatically created for my server - DC3 - and a file for my application which is "MyScript".

Don't get frustrated if the messages don't show up right away, the syslog server may have been configured to not flush the messages to disk automatically.

Have fun!

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS