Quantcast
Viewing latest article 1
Browse Latest Browse All 40

Reading Todays SQL Error Log With PowerShell

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

Image may be NSFW.
Clik here to view.
image

Here is the output

Image may be NSFW.
Clik here to view.
image

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
c:\temp\log.txt

or send it by email

Image may be NSFW.
Clik here to view.
image

or as an attachment

Image may be NSFW.
Clik here to view.
image

PowerShell is cool.

The code can be found here Show-Last24HoursSQLErrorLog

#############################################################################################
#
# NAME: Show-Last24HoursSQLErrorLog.ps1
# AUTHOR: Rob Sewell http://newsqldbawiththebeard.wordpress.com
# DATE:22/07/2013
#
# COMMENTS: Load function for reading last days current SQL Error Log for Server
# ————————————————————————
Function Show-Last24HoursSQLErrorLog ([string]$Server) {                      
    $srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $server 
    $logDate = (get-date).AddDays(-1)
    $Results = $srv.ReadErrorLog(0) |Where-Object {$_.LogDate -gt $logDate}| format-table -Wrap -AutoSize 
    $Results         
}

 


Viewing latest article 1
Browse Latest Browse All 40

Trending Articles