Step-by-Step: Using PowerShell to Add Servers to Server Manager in Windows Server 2012

After being out-of-office for most of July due to vacations, conferences, internal meetings, presentations and off-site events, I’m now back and ready-to-go! 

So … Let’s go! Winking smile

When I’m speaking with IT Pros about the great multi-server management enhancements in Windows Server 2012, I’m frequently asked about ways in which servers can be programmatically added to the new Server Manager tool.  This can be very useful when building many new servers and you’d like to make sure they are all added into the Server Manager console in an automated fashion.  In this article, we’ll step through the method of doing exactly this with PowerShell 3.0 and Server Manager together!

image

Where does Server Manager store its list of Managed Servers?

Before we can programmatically add new servers to the list of managed servers in Server Manager, we first need to understand how Server Manager stores the list of servers it is managing.   When adding servers to Server Manager, the name of each managed server is stored in the following XML configuration file:

C:\Users\[Username]\AppData\Roaming\Microsoft\Windows\ServerManager\ServerList.xml

Since this XML file is stored inside each user’s profile in the “Roaming” folder, by default this file will roam across management workstations if roaming user profiles are configured via Active Directory.

This XML file has a very simple structure consisting of a top-level ServerList node with child ServerInfo elements for individual servers, child ServerGroupInfo elements for custom groups that have been added to Server Manager, and child ServerGroupMembership elements for members of each custom group.

image

Sample ServerList.xml Configuration File

Thankfully, XML files can be easily processed by PowerShell 3.0 and we’ll use this capability to programmatically add new servers into the list of managed servers for Server Manager.

Getting Started …

To work through these steps together, you’ll need Windows Server 2012 to be already installed and accessible.  If you don’t yet have a lab environment running Windows Server 2012, check out our “Early Experts” program for some really easy methods to build a Windows Server 2012 lab in your on-premises environment or in the cloud using Windows Azure Infrastructure Services.

Step 1 - Close Server Manager before updating ServerList.xml

When Server Manager is running, it is continuously reading and writing information to the ServerList.xml file.  When updating this file outside of the Server Manager tool, you’ll want to make sure Server Manager is closed so that it’s not reading and writing to this file at the same time as your own updates.

To programmatically close Server Manager in your PowerShell script prior to making any updates to the ServerList.xml file, use the following PowerShell code:

get-process ServerManager | stop-process –force

Step 2 – Set path of existing ServerList.xml file

The ServerList.xml file is located in a rather lengthy path that is stored on a per-user basis.  Let’s set a variable to make it easy to reference the path to this file in our PowerShell script.

$file = get-item "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\ ServerManager\ServerList.xml"

Step 3 – Backup ServerList.xml

Improper direct modification of ServerList.xml can render this file unusable by Server Manager, so be sure to make a backup copy of this file before proceeding.

copy-item –path $file –destination $file-backup –force

Step 4 – Get content from ServerList.xml file in XML format

Next, we’ll grab the XML content from the ServerList.xml and store it in a PowerShell variable using the get-content cmdlet.

$xml = [xml] (get-content $file )

Step 5 – Clone an existing managed server element to a new XML element

Using the PowerShell clone() method for an existing XML element, we can create a new ServerInfo element that we can then update with the unique values for our new managed server entry in Server Manager.

$newserver = @($xml.ServerList.ServerInfo)[0].clone()

Step 6 – Update the new cloned element with new server information

Once we’ve cloned a new ServerInfo element, we can then easily update the new element to include the information that is related to our new managed server entry in Server Manager.

$newserver.name = “servername.domain.com”

$newserver.lastUpdateTime = “0001-01-01T00:00:00”

$newserver.status = “2”

Step 7 – Append the new cloned element inside the ServerList node

The new cloned element is now updated to reflect our new server information, so we’ll now append this cloned element to the existing list of servers stored inside the ServerList node that we previously read from ServerList.xml.

$xml.ServerList.AppendChild($newserver)

Step 8 – Save the updated XML elements to ServerList.xml

After appending the new ServerInfo element inside the ServerList node, we’ll commit the changes to the ServerList.xml file using the PowerShell save() method.

$xml.Save($file.FullName)

Step 9 – Re-launch Server Manager to see the results

We’ve completed the process to add a new managed server to the ServerList.xml file.  Now, let’s re-launch Server Manager so that we can see our results.

start-process –filepath $env:SystemRoot\System32\ServerManager.exe –WindowStyle Maximized

Completed! What’s Next?

Continue learning about all of the great new end-to-end enhancements in Windows Server 2012, Server Manager and PowerShell 3.0 by joining our FREE “Early Experts” online study group.  You can be our next “Early Expert”!

WS2012EE-Apprentice-Sample

        You can be our next “Early Expert”!