Todays post from my PowerShell Box of Tricks series is about the SQL Error Log.
DBAs need to read the error log for many reasons and there are different ways to do it. sp_readerrorlog, xp_readerrorlog, using SSMS opening the file in notepad. I’m sure every DBA has their own favourite. This one is mine.Of course, it uses PowerShell
It is very simple as there is a method on the server property called ReadErrorLog.
In this function I read the latest Error Log and filter it for the last 24 hours using the Get-Date cmdlet and the AddDays Method
Here is the output
You can also save the output to a text file and open it by piping the function to Out-File
Show-LatestSQLErrorLog fade2black|Out-File -FilePath c:\temp\log.txt
or send it by email
or as an attachment
PowerShell is cool.
The code can be found here Show-Last24HoursSQLErrorLog