Checking logs

We can use SQL Server Management Objects (SMO) to check the SQL Server error log. The script that picks out anything in the logs that have the words failed or error looks like the following:

$content = ($server.ReadErrorLog() |
Where-Object {$_.Text -like "*failed*" -or $_.Text -like "*error*"})

We can wrap this in code that allows us to send these entries to our DBA (or DBA team) via e-mail. Sending e-mail in PowerShell can be done using the cmdlet Send-MailMessage. Send-MailMessage will accept sender and recipient e-mail addresses, mail server information, subject, content, and, optionally, attachments. The content can either be in text or HTML format. If you are sending an HTML e-mail, you can convert the message into HTML by ...

Get PowerShell for SQL Server Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.