PowerShell

PowerShell: Creating Event Entries with Write-EventLog for Monitoring Purposes

Did you know that you can easily write something to the event log? For example, if you try something and it didn’t work then you can use the event log for monitoring. In this blog post I will show you an example how to write to then event log if something fails.

Let’s say we want to montior the availability of a website. By chance this website is called sid-500.com 😉 If the website is unreachble, PowerShell should write an error in the eventlog and if its reachable PowerShell should write an information in the event log.

PowerShell will perform a ping with a count 1 to sid-500.com. If the website is unreachable then PowerShell writes an error with EventId 13 to the eventlog, otherwise PowerShell writes an information of event 13 to the eventlog. That’s our objective for this blog post.

Ok, here is the code:


$test=Test-Connection sid-500.com -Count 1

If (!$test) {

Write-EventLog -LogName "System" -Source "Eventlog" -EventId 13 -EntryType Error -Message "Ping to sid-500.com failed"

}

else {

Write-EventLog -LogName "System" -Source "Eventlog" -EventId 14 -EntryType Information -Message "Ping to sid-500.com successful"

}

My website is always reachable, by all means. 😉 The information is written to the eventlog.

Unbenannt.PNG

Categories: PowerShell, Windows Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.