Central Management Server

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

Central Management Server

Instead of using a text file for your server list, consider using SQL Server’s Central Management Server. Unfamiliar with CMS? Check out Phil Factor’s in-depth post about its benefits.

Connecting to a SQL Server Central Management Server

Most code examples show how to connect using a manually specified SqlConnectionObject as such:

But did you know you can get the SqlConnectionObject from within SMO’s ConnectionContext? This is useful when you’ll be reusing the SMO server connection, and don’t want to create additional objects.

Create SQL Servers and Group Names from CSV

Create a CSV (copy/paste headers in comment of code). The CSV allows you to set the server name, group, registered instance name, and server description.

Note that if you want to create servers within subgroups, just use the following format: FirstGroup\Subgroup\OtherSubGroup, etc. If a group does not exist, it will be created.

csv

Then run the code

And voila!

regservers

Get simple list of servernames from CMS

Get CMS Servers and Groups

This returns an object called $servers which contanis server names, their group shortname, and the long name (Group\SubGroup\Subgroup2). Thanks goes out to Derik Hammer for providing a straightforward way to recurse through groups.

Migrate Central Management Server

Here are two ways to migrate your CMS. The first enumerates each group and instance and adds it via SMO. The second uses SMO’s built in import/export method.

This script is the core of my CMS migration script on ScriptCenter, Copy-CentralManagementServer.ps1.

Below is a snippet which uses Export/Import to perform the migration. There are two small downsides to this method. The first is that if you run into any issues, the Import() exception isn’t very useful, so knowing what went wrong is guesswork. When testing this script, I found the failure to be caused either by duplicate server names or that the import attempted to add the CMS Server to itself during the import.

The second downside is that this script generates a temporary file, which I always dislike, but ScriptCreate() sucked because it didn’t traverse all the groups. Nevertheless, it works well when it works.

Delete all objects in CMS

This is especially handy when testing scripts. Drop all instances and server groups within a CMS.

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.

One comment on “Central Management Server
  1. Kevin Parks says:

    Thanks for the great information and the CSV import script. Two thumbs up!

Leave a Reply

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

*