Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PowerShell for SQL Server Essentials

You're reading from   PowerShell for SQL Server Essentials Manage and monitor SQL Server administration and application deployment with PowerShell

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781784391492
Length 186 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Donabel Santos Donabel Santos
Author Profile Icon Donabel Santos
Donabel Santos
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Getting Started with PowerShell FREE CHAPTER 2. Using PowerShell with SQL Server 3. Profiling and Configuring SQL Server 4. Basic SQL Server Administration 5. Querying SQL Server with PowerShell 6. Monitoring and Automating SQL Server A. Implementing Reusability with Functions and Modules Index

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 using ConvertTo-Html. Optionally, you can specify an external CSS with ConvertTo-Html using the -CssUri parameter.

The full script that picks out and e-mails the last 10 entries using a Windows authenticated local account is as follows:

Import-Module...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image