My Favorite Blogs

Saturday, February 4, 2012

In SQL Management Studio resgiter multiple servers using Powershell

Recently our team builted around 60+ DR servers in our new datacenter and I thought to register them in SQL management studio, of course it will take some time to register them all together and thought that if a script does the same that would be wonderful.

After searching the indernet for sometime I found a peice of code and tweaked it to register a server and getting an input parameter from a text file.

Here is the code.
param (
 [string] $filename
 add-pssnapin SqlServerProviderSnapin100
add-pssnapin SqlServerCmdletSnapin100
  $readbuffer=get-content -path $filename
 foreach ($line in $readbuffer)
write-output "$i : $line"
sl "SQLSERVER:\SQLRegistration\Database Engine Server Group\SQLDBOps" ;
##remove-Item $(Encode-Sqlname "$line") -Force
new-Item $(Encode-Sqlname "$line") -itemtype registration -Value “server=$line;integrated security=true”;
remove-pssnapin SqlServerProviderSnapin100
remove-pssnapin SqlServerCmdletSnapin100

In the path name I’ve specified \SQLDBOps is which I’ve created under Registered ServersàDatabase EngineàLocal Server Groups and the \SQLRegistration\Database Engine Server Group is the default existing Local Servers Groups.
Code snap

Save the above script in a poweshell file as RegisterServer.ps1 and save your SQL instance names in a txt file as below.

Open the Powershell command window and navigate to the path where the script and text file exists and execute the file like  .\RegisterServer.ps1 SQLSrvrs.txt, the servers will be registered under your group as below.