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.

Categories: PowerShell, Windows Server