Monday, November 23, 2009

Configuration Helpers

This post is mainly so I’ll be able to find this code later.

I’ve been on multiple projects that make heavy use of configuration and I always end up writing these helpers similar to the ones listed below. These are general conversion routines that take a String and try to cast it to a specified value. In this version, I’ve included a special case when casting to Boolean. A “1” is considered TRUE and “0” FALSE.


public bool TryCastFromStringTo<T>(string s, out T value) 
value = default(T);
if (typeof(T) == typeof(bool))
if (s.Trim() == "1")
value = ((T)(object)true);
return true;
if (s.Trim() == "0")
value = ((T)(object)false);
return true;

TypeConverter converter = TypeDescriptor.GetConverter(typeof(T));
object convertedValue;
convertedValue = converter.ConvertFromString(s);
catch (Exception e)
if (e is NotSupportedException
|| e is InvalidCastException || e is FormatException)
return false;
if (null != e.InnerException && e.InnerException is FormatException)
return false;
value = (T) convertedValue;
return true;

public T SafelyCastFromStringTo<T>(string value, T defaultValue)
if (null == value) return defaultValue;
T convertedValue;
if (this.TryCastFromStringTo(value, out convertedValue))
return convertedValue;
return defaultValue;

Wednesday, November 11, 2009

SQL Server XML - Replacing the value of an attribute with a column from the same row

This will hopefully save someone the 45 minutes that it took me to figure it out. I wanted to replace the value of an attribute within an XML column with the value of a column on the same row.

UPDATE myTable
SET xmlColumn.modify('replace value of (/x/y/@a)[1] with sql:column("columnName")')

When I tried wrapping the call to sql:column with braces (ex. {sql:column("columnName")}, I received error 2224 - An expression was expected.

When I tried enclosing the whole thing in double-quotes (ex. "sql:column("columnName")", I received error 2370 - No more tokens expected at the end of the XQuery expression. Found 'columnName'

Tuesday, November 10, 2009

Home Server, Part 3 – Creating a VM for Windows Home Server

This is the third post of a series where I discuss the steps I went through to stand up a server for my home network 

One of my primary goals was to stand up a Windows Home Server (WHS). This post made it clear that it was possible. Now, was it practical and would it perform on Hyper-V Server? Both valid question that I would eventually find the answer to.

I’m not going to go into the details of installing Windows Home Server. There are many articles on the web that cover that. I’m really going to concentrate on the Hyper-V aspect of the install. With that in mind, I will say that I found this WHS whitepaper helpful.

I created the VHD first. Originally I wanted to create a small drive for the system and a larger one for the data. Then I saw a couple of articles that suggested that you use your largest HD as the primary. Something about the way that WHS allocates space? I should have kept the URL of the article. (Please let me know if you find an article that explains this – thanks.). To make things simple, I created a single 500GB fixed disk. Why a fixed disk? According to this whitepaper, fixed drives offer better performance. Also, one of my main goal for WHS is to have it backup all the computers in the house. So I went big.

Next I created the VM. I walked through the wizard.  Windows Home Server doesn’t require a lot of memory. I’ve seen some articles that say you can get by with as little as 512M. I allocated 1GB RAM to the VM. I also allocated one processor. I ended up with two IDE controllers and one SCSI controller. I assigned the 500GB VHD to the SCSI controller because it performs better than the IDE controllers (see the performance whitepaper). I also configured the DVD drive to reference the physical DVD, which contained the Windows Home Server Install DVD.

With that completed, I started the VM which started the WHS install.

After a couple of minutes, I was greeted with the message “Hard disk capable of hosting Windows Home Server was not found. Would you like to load additional storage drivers?” What the hell? How could the install not find the drive? I had no idea what driver to load. I wasn’t sure what the problem was.

I started searching the Internet for clues (Where would we be today without the Internet?) . I hit one article that discussed this very issue. According to the article, the issue is that the Windows Home Server install does not have the drivers for a SCSI drive but you can install them from a virtual floppy drive image called SCSI Shunt Driver.vfd. Unfortunately I couldn’t find it. It wasn’t on the Hyper-V Server host anywhere.

After more searching I bumped into this article. It turns out that Hyper-V Server 2008 R2 cannot boot a guest OS from a SCSI drive. The guest OS boot drive must be an IDE drive. I changed the configuration to move the VHD to the first IDE controller and restarted the VM.

The install proceeded without any more problems.

There was only one more step once the install had completed. I had to install Hyper-V Integration Services. Integration Services is installed on the guest OS and provides an interface to a set of services offered by Hyper-V Server. To install Integration Services, I connected to the VM from the Hyper-V Manager.


This brought up a window that is very similar to Remote Desktop.

After logging in, I was greeted with a very nice warning message of the damage that I could do by managing WHS this way. In this case, it’s safe to close the instance of Internet Explorer to expose the desktop. From the Virtual Machine Connection’s Action menu I selected Insert Integration Services Setup Disk. This loaded the virtual DVD drive with a CD image. Autoplay didn’t kick in so I had to go manually navigate to the DVD and find the setup program. In this case, I needed the version that was in the Support\x86 folder.


Integration with the guest is much smoother once Integrations Services is installed.

With the server install, I followed the the remainder of the Getting Started whitepaper to complete the install. I now have a Window Home Server running very smoothly and it is successfully backing up the other machines in the house. The next thing that I want to do is see how well it will stream media to the PS3.

Friday, November 6, 2009

Home Server, Part 2 – Installing Hyper-V Server 2008 R2

This post is one of a series where I discuss the steps I went through to stand up a server for my home network 

I’m going to be honest: I’ve a huge fan of VMWare Workstation. I tried Microsoft’s Virtual PC when it first came out but the performance was really lacking. A co-worker convinced me to try VMWare and I’ve been loyal since.

So I bet you’re wondering why the title contains Hyper-V Server and not something like VMWare Server or ESXi. I’ve been intrigued by Hyper-V since Microsoft announced that the Azure Fabric is based on it. This was an opportunity to give it a try, especially with the R2 release.

Hyper-V Server 2008 R2 is a free download from Microsoft. It is installed on the bare iron and provides a hypervisor for running virtualized servers. Pull down the configuration guide also. It’s not for R2 but I found it to be very helpful.

I’m going to tell you now that installing Hyper-V Server 2008 R2 was dead simple. I burned the iso to a DVD, popped it into the drive and bounced the machine. Soon the setup was running. After a couple of restarts I was logged in and greeted with the configuration tool – Server Configuration.


Using the information in the configuration guide, I setup the server to be in a Workgroup and enabled all of the remote management options. This is important since you will mainly be managing Hyper-V remotely. Also, enable Remote Desktop (RDP), you’re going to need it.

Hard Drives

When I installed Hyper-V Server 2008 R2, it formatted the partition that I installed it to. It did nothing with the other drives in the machine. I had to use the DISKPART command-line utility to bring the other hard drive online.

Remote Administration

Hyper-V Server 2008 R2 is supposed to be managed remotely. To do that I installed the Hyper-V Manager from the Remote Server Administration Toolkit (RSAT). Unfortunately there are different deployment packages for Vista and Windows 7.

After I installed the toolkit, I had to use the Turn Windows Features On or Off option in the Control Panel to enable the Hyper-V Manager.


Things were going really smoothly at this point. By now the hardest thing that I had to do was locate the RSAT deployment package for Windows 7. But what kind of project goes smoothly? Not this one. Keep reading.

The “Fun” Starts

The first VM that I want to create will be hosting Windows Home Server so I fired up the Hyper-V Manager to create the VM. I was greeted with the error message “You do not have the required permission to complete this task.  Contact the administrator of the authorization policy for the computer <computer name>.”  I started doing what all computer professionals do – searching the web.

I came across John Howard’s blog and specifically this post. John is a PM on the Hyper-V team at Microsoft. This post is part 1 in a series that addresses this issue. He’s even created a tool, HVRemote, to simplify the steps.

As I did more reading, it seems that R2 now takes care of a lot of the configuration that John lists in that post. This would be the Configure Remote Management option in the Server Configuration tool (sconfig).  Unfortunately I still couldn’t connect.

I started looking at the HVRemote utility again. There’s a 10-second Guide on the site that addresses a variety of configurations. Mine is the first one: Client and Server both workgroup. Using the Server Configuration tool, I created an account on the Hyper-V server with the same name and password as my account on my laptop. Like magic the permission error disappeared and I was able to connect to the server. 

Ha! Home free!”  I started to build the VHDs for the new VM.

Until I was greeted by another error message: “Cannot connect to the RPC service on computer <computer name>. Make sure your RPC service is running.” 

This had me stumped for a while. I used the Server Configuration utility to install the most recent updates and bounced the server. I was able to connect again – for about 10 minutes. Then the RPC service error popped up again.

I stopped the firewall on my client to see if it was the issue. Nope. Not it. I pinged my laptop from the server and the error went away – for about 10 minutes. That was interesting. I was actually able to do some of the configuration, like creating a Virtual Network and one of the VHDs. All I had to do was periodically ping my laptop from the server.

Obviously this was not an acceptable solution.

While searching I came across this thread in the Technet forums. The poster had solved a problem similar to mine by doing the following:

From a command window on the server, issue the following command:

netsh advfirewall set allprofiles state off

This disables the firewall on the server. While I really don’t want the firewall disabled, I ran the command anyway.

On the client:

  1. run dcomcnfg
  2. Under Component Services, navigate down to My Computer
  3. Bring up the Property Dialog for My Computer and select the COM Security tab
  4. Click the Edit Limits in the Access Permissions section
  5. Enable Remote Access for Anonymous Logon


Doing these two steps did seem to correct the RPC problem. I’ve not seen it since. I’ve also gone back and enabled the firewall on the server.

Update – I usually sit on a post for a day and re-read it prior to posting. I came back to the Hyper-V Manager after 24 hours and was meet by the “RPC service” error message. I stopped the firewall on the server and I’m now able to connect to the server as expected. I’m not happy about having to run without a firewall. I’m going to start a thread in the forums to find out the exact firewall configuration.

After getting all of this taken care of, I was able to create the two VHDs that I need for my Windows Home Server VM. That will be the topic for the next post in this series.

Next: Installing Windows Home Server

Home Server, Part 1 - Hardware

So begins another saga of large projects. This time, though, I’m in the domain of computers – something that I know a little more about.

I’ve wanted a server for the house for a while now. I periodically scan the Dell site looking for a good deal. A couple of weeks ago I stumbled into the PowerEdge T110 Tower Server. I picked one up with a quad-core 2.4 GHz proc, 2GB RAM (single DIMM) and one 160GB SATA hard drive. I typically purchase the minimum amount of RAM and hard drive that I can get. I’ve found that I can purchase them much cheaper after-market.

What I didn’t realize was that this machine used a Dual-Channel architecture and, as I understand it, operates optimally with matched-pairs of memory modules. The invoice from Dell didn’t say much about the configuration of the DIMM that I purchased, but between the user’s guide for the machine (yes, I read the manual) and Crucial,  I was able to identify the configuration that I needed.

With that information, it was just a matter of checking all the usual online sites. I ended up a (I know – shocking isn’t it!). Newegg had a really good deal on a 6GB  Tri-channel kit from Mushkin that was the correct configuration. I hadn’t heard of them so I checked out their forums. The moderator was really helpful. I gave him all the information that I had and he felt that the memory would work. He also offered to verify it with the engineering team. I like good service so I gambled and made the purchase.

The hardware arrived after a week and a half. After unpacking  the server, I fired it up to verify that things seemed OK. I shut it down after a couple of hours and installed the new memory. Then fired it back up. The server recognized the memory and everything seemed OK. After a couple more hours, I shut it down and installed the new hard drive.

Now I had a new server, eating electricity and doing nothing.

Next: Installing Hyper-V Server 2008 R2