Automating software installation isn't anything new to us. At Habanero, the creation of SharePoint 2007 development environments are automated using some combination of batch file and PowerShell scripts, and we use psexec.exe to perform all the remote software installations. With Windows Server 2008 R2, we want to use PowerShell Remoting instead of PsExec. In the process of converting the scripts to Powershell Remoting I ran into an interesting issue that I'd like to share. It may save you some headaches.

First, I enabled PowerShell remoting in my base virtual machine (VM) image (syspreped). This allows me to start automating the moment the VM is provisioned. The steps to turn on WinRM are well documented and easy. However, I ran into a miserable StackOverflow Exception when installing SQL Server 2008 remotely, although the installation works when executed locally on the VM. And occasionally you get an OutofMemoryException instead of StackOverflow. It turns out that there is MaxMemoryPerShellMB setting that limits the amount of memory available to any given remote session. The default limit was 150MB. I guess the SQL Server installation goes over the limit. You can change this memory limitation on the VM by running this command to set to 1 GB.

WinRM set winrm/config/Winrs @{MaxMemoryPerShellMB = "1000"}

One other tip if you need to use PowerShell remoting from a domain machine to a workgroup VM, you will need to add the workgroup VM as a Trusted Host from the calling machine. In my case, the base VM is not joined to any domain when it is provisioned.

Note: SCVMM R2 doesn't automatically join the VM to our development domain. I need to automate the process of joining the development VM to our domain as well.  The following command allows you  to remote to any machine:

WinRM set winrm/config/client @{ TrustedHosts = "*"}

And on the base VM image, run this (only if you are not using user from Administrators group):

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f

Once I have configured these settings properly on the base VM image and the calling machine, remote installing of SQL Server 2008, SharePoint 2010 Public Beta and Visual Studio 2010 Beta 2 using PowerShell becomes very easy. The resulting PowerShell automation scripts are a lot more maintainable compared to my previous version using PsExec.