Does dbatools Have a Function for That?

Page content

dbatools has a lot of functions. A lot. Over 550. There is a great command index on the website, and the documentation gets updated every time a new version is published. But sometimes, it feels like you can’t find what you need unless you already know the name of the thing you’re looking for. Other times, you might start writing your own wrapper around dbatools functions, or maybe start a new function from scratch, because it seems like the functionality you need isn’t there.

I Heard You Like Functions…

…so the team made a function to find functions so you don’t accidentally write functions you don’t need. And it’s called Find-DbaCommand. This function not only searches function names but delves into the comment-based help to get a more complete picture of what each function does, with the goal of surfacing more candidate functions for what you’re trying to accomplish.

The first time you run Find-DbaCommand, or after a significant update of the module on your system, it may take some extra time. That’s because it’s rebuilding its cache so that future runs are much faster.

The easiest way to use Find-DbaCommand is to do a basic keyword search. For example, Find-DbaCommand compression (or Find-DbaCommand -Pattern "compression") will bring back a host of functions - not just the ones relating to compressing tables and indexes. It’ll include (for example) backup-related functions, because backups can be compressed and there’s a switch parameter for that. Find-DbaCommand is searching all the comment-based help to find your search pattern.

But that’s not the only way to search. You can search by tag and author name as well. In each function’s comment-based help .NOTES section, there are (or should be) lines where tags and the author(s) of the function are recorded. Those are searchable via parameter as well!

Let’s use Get-DbaDbCompression (I’ve linked to a particular line on purpose) as an example. It’s tagged with Compression, Table, Database and the author is Jess Pomfret (blog | twitter). Let’s try a couple searches.

find-dbacommand compression -tag table              
CommandName Synopsis
Get-DbaDbCompression Gets tables and indexes size and current compression settings.
Set-DbaDbCompression Sets tables and indexes with preferred compression setting.
Test-DbaDbCompression Returns tables and indexes with preferred compression setting.

This brought back the compression-related functions that are tagged with table. Okay. But what if I only want to see the table compression function(s) Jess is tagged as the author of? Just add that filter.

find-dbacommand compression -tag table -author jpomfret
CommandName Synopsis
Get-DbaDbCompression Gets tables and indexes size and current compression settings.

What Next?

It’s kind of frustrating to discover, 3 hours into writing up a new function, that dbatools already has what you need but you didn’t realize it. Or maybe it has most of what you need in one function, or you need to glue a few functions together to get the report you need for your security team.

So before you start coding, poke around with Find-DbaFunction for a few minutes. Try a few different keywords. If you’re looking to collect security-related information, look for user or login as well as permission, for example.

The dbatools team has built an incredible library, but it’s easy to get lost out there. Take advantage of this function to help you navigate, and possibly save yourself a bunch of work.