How Long has Your SQL Server Been Online2 min read

This week’s blog post will help you check your SQL Servers up-time. There are numerous reasons I can think of that you would want to know how long your SQL Server has been online. Was the server recently patched, did it crash and come back online, or did someone restart it by mistake? These are all valid questions about a single SQL Server or your entire estate. I will show you how you can easily check one too many servers quickly for uptime.

We will start by using every DBA’s favorite PowerShell module…  dbatools

Basics

For this example, I will introduce you to the Get-DbaUpTime function in the dbatools PowerShell module. This function has all the information we are attempting to gather.


This example displays when Windows and SQL Server were last started. These are critical pieces of information, depending on what you are troubleshooting.

For a different view of the output, we can pipe the command to Out-Gridview.


Now you have a graphical interface to filter records to find what you are looking for as your investigation grows.

Enhanced

What if your Infrastructure team just finished their monthly Windows Patching of all development servers? Do you know if the SQL Servers have been rebooted? Do you want to RDP each of them to see when the last reboot was? No, you sure do not?

Combining SQL Server Central Management Server and the Get-DbaUpTime function work well together.


From the output, you can see that one server has been up four days less than the rest of the servers.  So if you had maintenance six days ago, that one server would stand out as something that might need investigation.

You get the idea of where you can go with this command and how it can be very beneficial to monitor your SQL Server environment.

Extra

Lastly, let me show you how to take this information to another level and save it to a report format that can be sent to a website or e-mailed to a list of managers.

I ran across this freely distributed module that allows you to send your PowerShell output to an HTML file. As part of the PSWriteHTML module, you have the Out-HtmlView, an HTML replacement for Out-GridView.


Look at that beautiful HTML output. Not only do you get a clean HTML report, but it is also sortable, searchable, and exportable. Attach this to an e-mail or a service ticket, and you have your validation of SQL Server uptime.

Post it to a website, and you automatically have a web page that shows SQL Server Uptime.

 

Enjoy and see you on the next blog.

One Reply to “How Long has Your SQL Server Been Online2 min read

  1. Since the tempdb will be created every time your SQL server service will be started, you could also do a simple
    SELECT d.create_date FROM sys.databases AS d WHERE d.name = ‘tempdb’
    the know, when it was started the last time…

    Of course this returns only a datetime and you have to do all the fancy formating, DateDiff()-stuff of the PowerShell by yourself (if you really need it). And of course this works only for a single instance, except you use the Registered Server Feature in SSMS (allows you to run queries on multiple instances)

Leave a Reply

Your email address will not be published. Required fields are marked *