Summary: Guest blogger, Rolf Masuch, talks about using Windows PowerShell to customize Server Manager.
Microsoft Scripting Guy, Ed Wilson, is here. Today we have a guest post written by Rolf Masuch, who is a senior consultant for Microsoft in Germany. Today is Rolf’s birthday, and he wanted to start the celebration off right by sharing a couple of cool scripts with us. Take it away Rolf…
When it comes to the new versions of Windows Server 2012 R2 and Windows Server 2012, one of my most-loved changes is to Server Manager. The new Server Manager Dashboard looks like this:
Lots of documents have been written already about this new way of working. Today I’d like to share two extension scripts that I thought would be helpful for the community because when it comes to automating Server Manager, you cannot do two things:
- Add servers to your list of managed servers
- Create groups that contain a custom list of servers
You will find the respective menu entries in Server Manager when you click Manage.
But first, let’s look behind the curtain of Server Manager. It stores information in an XML file that is saved per user at this location:
Additionally, it is useful to know that every time you close a running Server Manager instance, it can overwrite your scripted changes. We want to take care that before we make changes to the XML file, we close the Server Manager process. We also need to make all changes in an elevated process.
When it comes down to writing additional entries to the file, you may think, “Hey, it is just XML, this is easy.” But when you look at the structure of the XML file, you see that this is not so simple. The following example is from a freshly installed system with one additional group:
<?xml version="1.0" encoding="utf-8"?>
<ServerInfo name="MyCloudComp001" status="1" lastUpdateTime="2013-08-08T09:01:26.7659233+00:00" locale="en-US" /><ServerGroupInfo id="3" name="MyGroup" />
<ServerGroupMembership server="MyCloudComp001" serverGroupId="3" />
As you see from the initial lines, the XML structure is using an additional namespace that is named urn:serverpool-schema. When you want to add new elements to this structure, you need to take care of that. So how is it done?
Let’s look at the script Add-ServerToManagedServerList.ps1. Here are the essential lines:
$NewServer = $ServerList.CreateElement("ServerInfo")
$NewServer.SetAttribute("name",$ComputerName.ToString()) | Out-Null
$NewServer.SetAttribute("xmlns","urn:serverpool-schema") | Out-Null
$ServerList.ServerList.AppendChild($NewServer) | Out-Null
So setting the right attribute in the right notation does the trick for us. Now I have the newly added server in Server Manager.
The same is true when it comes to adding the server to an existing group.
$NewServerInGroup = $ServerList.CreateElement("ServerGroupMembership")
$NewServerInGroup.SetAttribute("server",$ComputerName.ToString()) | Out-Null
$NewServerInGroup.SetAttribute("serverGroupId",$PSItem.Id) | Out-Null
$NewServerInGroup.SetAttribute("xmlns","urn:serverpool-schema") | Out-Null
$ServerList.ServerList.AppendChild($NewServerInGroup) | Out-Null
Because this is an existing group, we have to make sure that the group’s ID is added to the attributes of the entry. So this is how you add a new server to your list of managed servers. Simple, isn’t it? A new server group is shown here:
You may also want to add some custom groups. The added script, New-ServerManagerGroup.ps1, helps you take care of that. It creates a random number and uses it as the needed group ID.
$Global:ServerGroupID = Get-Random -Maximum 1000
$NewServerGroup = $ServerList.CreateElement("ServerGroupInfo")
$NewServerGroup.SetAttribute("id",$Global:ServerGroupID) | Out-Null
$NewServerGroup.SetAttribute("name",$Name) | Out-Null
$NewServerGroup.SetAttribute("xmlns","urn:serverpool-schema") | Out-Null
$ServerList.ServerList.AppendChild($NewServerGroup) | Out-Null
I’ve added additional switches, such as –Backup (to save the previous ServerManager.xml) and –StartServerManager (for launching Server Manager after your additions). Be aware that the provided scripts always check for a servermanager.exe process and end this process silently for you.
I hope these little scripts help the community, and I’m looking forward to your comments.
The following scripts are uploaded on the Script Center Repository:
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy