General / Other Scripts

This category page is part of a series of Professional PowerShell SMO Recipes. You can find an index of all recipes on the main SMO Recipes page, and if you want to learn more about each recipe and see some sample screenshots, click on its category page.

These scripts were created and tested on Windows 8.1 with PowerShell v4 and SQL Server 2014, though most recipes should work with earlier versions of SQL Server. PowerShell v3 and above is required for many of the recipes. If you have a recipe request, leave a comment and I'll see what I can do. This cookbook will be continuously built, as I work more with SMO.

Recipe Categories

General / Other Scripts

Below I’ve included a couple SMO, and non-SMO bonus scripts that can be really useful for automated SQL Server management.

Return query with just one result

SqlCommand’s ExecuteScalar() is cool because it’s made to return just one result. Think of it as one column of one row.

The closest I can find to that in SMO is calling a single item in a single row using ExecuteWithResults().

Echo your connection string

For when you don’t want to look up the syntax on the Internet..

Get list of all user (non-system) databases

I’ve found SMO’s IsSystemObject to be super useful in a number of my scripts. It’s good to be aware of it.

Parse SMO URN

Oddly enough $server.GetSmoObject($urn) didn’t work in this case. The Microsoft.SqlServer.Management.Smo.SmoException it produced was: For the moment we don’t support Urn’s that do not start with Server

So I had to get creative when traversing backwards (as seen in “Get CMS Servers and Groups“)

Populate Dynamic Parameter with database names

This is one of my favorite and most used snippets; it will build a new Dyanamic Parameter named Databases which makes -Databases tabbable from the command line. For example: .\Test-Code.ps1 -ServerName sqlserver -Databases master. Or you can specify multiple databases by using a comma: -Databases master, otherdb.

To limit database selection to just one, modify the code below to reflect (“Databases”, [String], $dbattributes) instead of (“Databases”, [String[]], $dbattributes).

Populate Dynamic Parameters with database and login names

The above code is highly reusable for other Dynamic Parameters, as well. The script below will generate a list of all user databases and logins, and then add them to the dynamic parameters IncludeLogins, ExcludeLogins, IncludeDBs, ExcludeDBs.

Want to see more? You can find an index of all recipes on SMO Recipes Index Page page or click on any specific category at the top of this page.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Leave a Reply

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

*