Aamir Syed

PowerShell: Get a daily database status email

November 22, 2016 by

The need for this script came about when I took a new job as a DBA. One of my responsibilities was to make sure all databases were available after the maintenance window which ran during the weekend. Rather than log into each database server and check that the databases were online, I had a script do the work for me and shoot me an email.

It evolved overtime, at first it was a simple PowerShell script that gave the output in the PowerShell console. Eventually I learned how to pipe the information into a table and then into HTML, and THEN send an email. After that I learned how to color code it so that if any database was offline, it would color the cell in red.

I prefer to do my PowerShell development in the windows provided Windows PowerShell ISE. I especially like the fact that it has a scripting environment with easily accessible tools and autocomplete, as well as the PowerShellPowerShell console/output window.

In this post I’ll take the time to go over some key parts of the script so that you can manipulate as you wish in order to implement it into your environment. I will provide the finished script at the bottom of the post.

First thing we have to do is import the sql PowerShell module in PowerShell.

Next let’s quickly design the html output using a simple CSS style. Keep in mind I have very little experience with web development. I got this information from SQL Server 2012 with PowerShell V3 Cookbook. I highly suggest all Database Administrators and Database Developers pick this up. It contains so many useful tips. I also believe that a new one for 2014 has recently been released.

The part of this that defines .offline is a property that will set the cell color red (#E01B1B) for any database that is seen as offline.

Also, for a more in depth beginner’s tutorial on CSS please visit this link.

Now let’s define the function. I like to use functions to encompass my PowerShell script. It allows for easier parameterization and you can also import it as a module into your personal PowerShell profile and just call it from the console whenever necessary. I called it “Get-DbStatus” as it made sense to me and would be easy to remember.

Now we need to import a previously created list of servers we’re pulling this information from. It’s just a simple text file with a list of servernames and ports. I store the contents of that list into a variable called $ServerList and use the Get-Content command to pull the data out of the list.

Then I use Foreach to loop through each item in $serverlist and store it in $servername. I then use the data stored in $servername to create an SMO object for each server. This is so we can use PowerShell commands to get the information we need.

Next, I use a nested foreach loop in order to loop through each database in each sql server (SMO object).

The “IsAccessible” property will determine whether the database is available or not. The default output is “True/False” however I used the switch command to return “online/offline”. You can leave it, but I much prefer this method.

Next I define properties of the function. This will make it easy to call the function with specific information.

You will see me use the “Server, DBName, and Status” properties in the next portion of the script.

We would then close out the function with the necessary closing brackets (“}”) and then call the function within the script.

Follow this next step closely. I’m taking the output of the function and piping it into an html, and piping that into a foreach loop so I can color code the cells if it’s offline by referencing the css style sheet we created at the beginning of the script. This should give you an idea of how powerful the piping feature in PowerShell is.

Finally, we can take this output and email an embedded HTML file to us every morning using the Send-MailMessage feature in PowerShell. This will make your daily tasks easier. I’ve provided a generic template below, be sure to confirm the proper settings for the SMTP server, and the “to” and “from” addresses. You may have to work with your mail administrator in order to get this information.

As promised, here is the script in its entirety. Remember that you must modify and customize it so that it will work in your environment. You can get really creative with using functions and modular programming. You can even turn this into a module, import it into your profile and call it as a regular command. I suggest modifying it so that the output comes out in the PowerShell console. Otherwise you’d have to dig through your file system in order to open the html file. But this is entirely up to you. PowerShell is so versatile that it can automate most of your day-to-day tasks. Or use a simple command to return a plethora of information almost instantly. Saving a lot of people a lot of time. You can pass on the scripts to your juniors or team and increase productivity.

Here is an example of the output. (I’ve obscured server and database names for obvious reasons, but you should get the idea).

And what the offline databases would look like:

I hope some of you find this script useful. And highly encourage everyone who works in a windows environment to learn PowerShell. It’s such a powerful and versatile tool.

If you’re looking for a place to start, this is a great book to get you primed. Learn Windows PowerShell in a Month of Lunches

Also be sure to checkout Microsoft’s own PowerShell Script Center

Also, please visit a more in depth beginner’s tutorial on CSS

I highly suggest all Database Administrators and Database Developers pick this up: SQL Server 2012 with PowerShell V3 Cookbook


Aamir Syed

Aamir Syed

Aamir is a SQL Server Database Administrator in the NYC/NJ area (and has recently taken a role as a Database Developer).

He started his IT career in helpdesk world and eventually moved into the networking/systems administrator side of things. After some time, he developed an affection for working with Databases (mainly SQL Server) and has focused his career on that for the past eight years. He has worked for various industries in both large and small environments all with different needs.

SQL Shack has provided him with an opportunity to contribute to a community that has given him so much throughout the years.

View all posts by Aamir Syed
Aamir Syed
SQL Database development

About Aamir Syed

Aamir is a SQL Server Database Administrator in the NYC/NJ area (and has recently taken a role as a Database Developer). He started his IT career in helpdesk world and eventually moved into the networking/systems administrator side of things. After some time, he developed an affection for working with Databases (mainly SQL Server) and has focused his career on that for the past eight years. He has worked for various industries in both large and small environments all with different needs. SQL Shack has provided him with an opportunity to contribute to a community that has given him so much throughout the years. View all posts by Aamir Syed

755 Views
  • kiran patel

    Really Useful Script Aamir. Thanks for sharing.

    I am new to powershell and one question is how to put server name(InstanceName) in text file
    1. Instance name or server name?
    2. ‘Instance Name’ or ” Instance name””
    Etc ?

    I just put instance name with single quote and got below error

    Moreover, Do you have script available without Email Code?

    Thanking you in Advance.