Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

Powershell Get-EventLog and Event Log messages

25 February,2012 by Tom Collins

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)
{
    $tmpOut = new-object "System.Data.DataTable"
    $tmpOut = Get-Eventlog -computername $server  -logname system -EntryType Error -After $(Get-Date).AddHours(-24)|sort eventid

$dt += $tmpOut

	
}
$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 sqlserver-dba.com Servers" | Set-Content $outputfilefull

 

See Also

SQL Server – Send email using Powershell

List failed SQL server Jobs with Powershell

 


Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Powershell Get-EventLog and Event Log messages


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer