André KammanOld School Expertise, Future Ready Solutions
Invoke-DbaDiagnosticQuery

Invoke-DbaDiagnosticQuery


Gary Bargsley (b|t) is the host of this months
T-SQL Tuesday #110 – ” Automate All the Things”
The question he asks is easy:

What do you want to automate or what automation are you proud of completing?

My answer to that is simple, I’m a major contributor to the awesome Powershell library dbatools. What I’ve contributed to that library are commands that will help automate the running and processing of queries from the DMV library of Glenn Berry
At some point in the life of a DBA we’ve all come accross his scripts. For the longest time I would advise people to google “Glenn Berry DMV”, and it will be the top result.
The scripts however, come in a single file per SQL Server version and you can’t run them all in one go. You would have to select a script, run it, and paste the result from Management Studio into an Excel sheet. Glenn provides an empty sheet with tabs ready to paste the various result sets in. I’ve automated this part, hope you like it!

Let’s get started with something simple and run a single script.
First install dbatools though, and to make things more interesting for later on, install a module called importexcel while you’re at it.
All you need to do after that is open a powershell window and type something like the following:

Invoke-DbaDiagnosticQuery -SqlInstance localhost -QueryName "Configuration Values" | Select -ExpandProperty result | Format-Table -AutoSize

Want to know which scripts are available?

Invoke-DbaDiagnosticQuery -SqlInstance localhost -UseSelectionHelper
Just select one or more scripts and hit “ok” to run them

So, where is this Excel sheet you promised us?

Invoke-DbaDiagnosticQuery -SqlInstance localhost | Export-DbaDiagnosticQuery -ConvertTo Excel -Path c:\demo
Besides the instance scripts, there are also scripts that run for every database you have!
One Excel sheet for the instance and one for every database.
(My laptop is called XPS, it shows that even though I specified “localhost” in the query)
Presto! How cool is this!

The directory will be filled with a lot of other files besides the Excel sheets. Some of Glenn’s scripts have columns which contain the query or even the query plan. That doesn’t play nice with an Excel sheet so they will be saved in separate files.

Good to know:

  • There are options available to exclude the .sql and .sqlplan file from the export
  • You can include specific scripts, but also exclude specific scripts
  • Want a more portable format? Pipe the result to Export-CliXml. That compresses nicely if you 7zip it and can be used as input for the Export-DbaDiagnosticQuery later on.
  • The latest version of Glenn Berry’s DMV scripts are always included in dbatools, you don’t have to download them separately. (but you can certainly do that if you want to for whatever reason)
Haven’t seen dbatools yet? You’re in for a treat! Check out not just my little contribution but the literally 100’s of other amazing features!
Share:FacebookX
Written by
André Kamman
Join the discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 comments
  • Why Excel? We put data like this into the database (an ADMIN DB if you like …). We have a web site intranet thing, with decent query/display ability and also report writer capability, which does a good job of slice-and-dice. Excel column-filters are nice, and so on, but Excel is just String-and-Gum to me and the source of lots of time-wasting because of well meaning good intentions poorly executed : someone has finger-trouble / leans on the keyboard and alters data without realising, or stores the file in the wrong place, or the holiday-cover doesn’t know where it is, or someone runs the BATCH with the “old version”, or hangs onto a stone-age copy on their C: drive … all solved with a Intranet type solution. And keep as much historical data as you want – so allowing after-the-fact analysis of “How fast is this thing growing” and so on.

    • Hi Kristen, Excel is a quick and solid way to store and analyze the data. For many people this is more than enough, and way better than staring at text files. However, if you have many instances and want to collect on a daily basis and look at stats over time, then putting the data in a proper database instead of many Excel sheets is indeed a good way to go. We also do that for our systems. But it does mean that you have to figure out a proper data model and perhaps cleanup the history, and “monitor your monitoring”. At some point you might be better of buying a proper monitoring system. Long story short, I agree with you, but I do also think that there sometimes is a need for a simple Excel sheet based export.

André Kamman

André Kamman

I’m a SQL Server consultant based in the Netherlands. I’m a SQL Server MVP and I’m the co-founder and current chairman of the Dutch PASS Chapter and a member of the volunteer team for the Dutch SQL Saturday’s. I’ve done a whole lot of pure DBA work managing 100′s of servers and learning something new every day.

These days I spend a lot of time on just a few servers containing some large databases and the better part of the day is spent with SSIS. In both roles I’ve been using Powershell a lot. I joke sometimes that I replaced myself with a powershell script when leaving the DBA role, but these days I automate so much that I have to watch myself and not actually do just that

Instagram

Instagram has returned empty data. Please authorize your Instagram account in the plugin settings .

Please note

This is a widgetized sidebar area and you can place any widget here, as you would with the classic WordPress sidebar.