Monday, April 11, 2016

Configuration of Central Management Server and Registration of Servers using PowerShell Script

Central Management Server (CMS) is powerful tool for the DBAs, it is very useful when we try run a same script on group of servers to make a configuration changes or to list the databases from all the SQL Servers in your company or setting any database level setting or there are many cases where this CMS comes handy. 

Create and Configure Central Management Server: Choose a server to use as the CMS server. Open SSMS and click on registered Servers form View menu. 













It will open a Registered Servers pane which contains a node saying Central Management Servers, Right click on that Node and click on Register Central management Sever as below. 












It will open a pop window like below, enter the server that you use as CMS in the Server name then click test and save.

























Now it is time to register the Servers on CMS to use the CMS power. We can register the servers manually but if you have lot of servers it will tedious job to register individual servers. Here we can use the power of Powers hell to bulk register servers.

Registering Servers using PowerShell: Create a table SERVERS with a column name as SERVERNAME on the CMS Server. And just load that table with all the SQL Server servers to be registered on the CMS as below.














Create Powershell script as below and name it as RegSvr.ps1 and note the location where you saved the script. Replace CMSSERVER with the CMS server that you chose, DATABASE is the DB name on the CMSSERVER where you have create the SERVERS table with the list of registered servers. And choose the DIRECTORY and SUBDIRECTORY names if you have different server groups for different purposes. Keep reset of the things as it is. 















Running the PowerShell Script: Connect to the CMS server and right on server and click the Start PowerShell. When you see the PowerShell window run SL ,then in the next line enter the name of the PowerShell script Regsvr.ps1 as in the below screen shot.



























As it register, we can see the results as below..











We can see the registered servers as below, click on node and select new query to run a script against all the servers falling under that node. Shown as below. 






























Attaching the PowerShell script here:


$cmsservers = invoke-sqlcmd -serverinstance 'CMSSERVER'-database 'DATABASE' -query "SELECT SERVERNAME FROM Servers"
cd 'SQLSERVER:\sqlregistration\Central Management Server Group\CMSSERVER\DIRECTORY\SUBDIRECTORY'
foreach ($server in $cmsservers)
{
$svrname = $server.serverName
$regsvrname = $svrname
if ($regsvrname -like "*\*")
{
$regsvrname = $regsvrname.replace("\", "%5C")
}
$svrname
new-item $regsvrname -itemtype registration -value "server= $regsvrname;integrated security=true"
}

I have used the post from SQLTIPS on registering servers on CMS, you can find the post here.
Thanks, Hope it helps. 

No comments:

Post a Comment