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.


Then run the code

And voila!


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 Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

3 comments on “Central Management Server
  1. Kevin Parks says:

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

  2. Lusale says:

    Hi Chrissy,

    I am using ‘Delete all objects in CMS’ script and it works as intended. I would like to tweak it so that it only deletes servers in a specified subgroup. What is the best way of achieving that?

  3. Alex Hatcher says:

    Waking up old thread.

    Great script, thank you! I want my server in my registered server as well. fortunately, during migration, i just added the domain name to the CMS, and it added itself.

Leave a Reply

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