Syspreped Windows 7 fail with “Windows could not finish configuring the system”

After you have installed a Windows 7 or Windows Server 2008 R2, install and configure windows features, install your own app, and then run sysprep command to create an image that is ready to be deployed. When you boot windows for the first time,  it might comes up with following message at the windows setup stage "Setup is starting services" window.

Windows could not finish configuring the system. To attempt to resume configuration, restart the computer.

After you restart the system, you might keep getting the same error.

This is usually caused one of windows features installed or configured not supported by sysprep. The workaround is move the configuration to post setup scripts.