Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Powershell Get-EventLog and Event Log messages

This post explains how to list Event Log Messages with Powershell Get-EventLog  , on multiple servers and output the results to a HTML file.

It is only one extra step to send an email attachment.

This example iterates through a list of servers and returns Error messages from the System Event Log generated in the last 24 hrs

$isodate=Get-Date -format s 
$isodate=$isodate -replace(":","")
$basepath=(Get-Location -PSProvider FileSystem).ProviderPath

$serverpath=$basepath + "\config\servers.txt"
$outputfile="\logs\sql_server_health_eventviewer_" + $isodate + ".html"
$outputfilefull = $basepath + $outputfile

#invoke stylesheet
. .\modules\stylesheet.ps1

#intro smtp function 
. .\modules\smtp.ps1

$dt = new-object "System.Data.DataTable"
foreach ($server in get-content $serverpath)

	$ServerCurrent = "$server"
	$dt = Get-Eventlog -computername $server -logname system -EntryType Error -After $(Get-Date).AddHours(-24) | sort eventid |`

$dt | select * -ExcludeProperty Data,Category,Index,Category,CategoryNumber,ReplacementStrings,CategoryNumber,Site,SiteContainer,TimeWritten,Container,InstanceId,UserName | ConvertTo-Html -head $reportstyle -body "Server System Errors on Servers" | Set-Content $outputfilefull


See Also

SQL Server – Send email using Powershell

List failed SQL server Jobs with Powershell



Leave a comment on the original post [, opens in a new window]

Loading comments...