Creating a cloneable SharePoint 2010 Development Environment with PowerShell, Windows Sysprep, and SQL Server 2008 R2 Sysprep – PART 2

by josh 30. April 2010 03:24

This is a continuation from Part 1. This post will cover the PowerShell script used to configure SQL Server 2008 R2 and SharePoint 2010 on the machine.

The syntax to run the script is as follows:

.\build.ps1 .\settings.xml

assuming you are running the scripts from the root of the directory where they reside. What does the settings file look like? Glad you asked:

<?xml version="1.0" encoding="utf-8"?>

<Configuration

<Package Id="sts">

<Setting Id="LAUNCHEDFROMSETUPSTS" Value="Yes"/>

</Package>

<Package Id="spswfe">

<Setting Id="SETUPCALLED" Value="1"/>

</Package>

<Logging Type="verbose" Path="%temp%" Template="SharePoint Server Setup(*).log"/>

<PIDKEY Value="xxxxx-xxxxx-xxxxx-xxxxx-xxxxx" />

<Display Level="basic" CompletionNotice="no" />

<Setting Id="SERVERROLE" Value="APPLICATION"/>

<Setting Id="USINGUIINSTALLMODE" Value="0"/>

<Setting Id="SETUP_REBOOT" Value="Never" />

<Setting Id="SETUPTYPE" Value="CLEAN_INSTALL"/>

<SqlServer SqlSetupPath="D:\setup"

InstanceID="App"

InstanceName="MSSQLSERVER"

SqlSvcAccount="contoso\sqlservice"

SqlSvcPassword="P@ssw0rd"

RsSvcAccount="contoso\sqlservice"

RsSvcPassword="P@ssw0rd"

SqlSysadminAccounts="builtin\administrators"

AgtSvcAccount="contoso\sqlservice"

AgtSvcPassword="P@ssw0rd"

SecurityMode="SQL"

SaPassword="P@ssw0rd"

TcpEnabled="1"

>

</SqlServer>

<SharePoint>

<Farm ConfigureEmail="true"

FarmAccount="contoso\svcsharepoint"

FarmAccountPassword="P@ssw0rd"

DBServer="app"

ConfigDB="SharePoint_Config"

AdminContentDB="CentralAdmin_Content"

Passphrase="P@ssw0rd"

EmailFromAddress="sharepoint@contoso.com"

EmailReplyToAddress="sharepoint@contoso.com"

SMTPServer="smtp.contoso.com"

>

<CentralAdmin Port="1234">

<Servers>

<Server Name="app" />

</Servers>

</CentralAdmin>

</Farm>

</SharePoint>

</Configuration>

The settings are accessed from the PowerShell script by getting a reference to the xml

[xml]$config = Get-Content $settingsFilepath

and then referencing settings as follows:

$SqlSetupPath = $config.Configuration.SqlServer.SqlSetupPath

The first 9 elements, from Package down to the last Setting, are copied straight out of the config.xml file on the installation media for SharePoint in the \Files\SetupFarmSilent folder. The only change is to replace the product key with your own SharePoint 2010 product key.

The SqlServer element contains SQL Server specific settings for provisioning the SQL Server instance from the prepared image (from Part 1). The PowerShell script code is below:

invoke-expression "& ""$SqlSetupPath"" /QS /ACTION=CompleteImage /INSTANCEID=""$InstanceID""
/INSTANCENAME=""$InstanceName"" /SQLSVCACCOUNT=""$SqlSvcAccount"" /SQLSVCPASSWORD=""$SqlSvcPassword""
/RSSVCACCOUNT=""$RsSvcAccount"" /RSSVCPASSWORD=""$RsSvcPassword"" /SQLSYSADMINACCOUNTS=""$SqlSysadminAccounts""
/AGTSVCACCOUNT=""$AgtSvcAccount"" /AGTSVCPASSWORD=""$AgtSvcPassword"" /IACCEPTSQLSERVERLICENSETERMS 
/TCPENABLED=$TcpEnabled   "

Here are the settings explanations:

  • $SqlSetupPath – the path to the setup executable (e.g. D:\setup)
  • /QS – specifies that the installer should show progress, but not allow any input from user
  • /ACTION – for provisioning an instance from a sysprepped image, the action is "CompleteImage"
  • $InstanceID – the instance id (string) specified when creating the sysprep image
  • $InstanceName – the name to be used for the new instance
  • $SqlSvcAccount,$SqlSvcPassword – the username and password for the sql server account
  • $RsSvcAccount,$RsSvcPasswordusername and password for reporting services account
  • $SqlSysadminAccounts – accounts that should be added to the SQL Server sysadmin group
  • $AgtSvcAccount, $AgtSvcPassword – username and password for the SQL Server Agent account
  • /IACCEPTSQLSERVERLICENSETERMS – automatically accepts license terms without user interaction
  • $TcpEnabled – specify '1' to enable TCP for the SQL Server Service

There are many more options for configuring the server and they can be found here under Complete Image Parameters.

SharePoint Configuration

The SharePoint element in the settings.xml file contains the basic settings for creating a SharePoint farm and configuring it. These settings are used by the PowerShell cmdlets to perform the configuration.


Here is the SharePoint configuration script: (thanks to Gary Lapointe, SharePoint MVP and PowerShell guru (http://stsadm.blogspot.com) , for the examples on his blog that I incorporated into this script)

 (Build.ps1)

#Add SharePoint cmdlets reference (if you're running the SharePoint 2010 Management Shell you won't need this line)
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Configure Farm
Write-Host -foregroundcolor yellow "# Configuring SharePoint..."
$farmAcct = New-Object System.Management.Automation.PSCredential $config.Configuration.SharePoint.Farm.FarmAccount, (ConvertTo-SecureString $config.Configuration.SharePoint.Farm.FarmAccountPassword -AsPlainText -force)


$configDb = $config.Configuration.SharePoint.Farm.ConfigDB  
$adminContentDb = $config.Configuration.SharePoint.Farm.AdminContentDB   
$server = $config.Configuration.SharePoint.Farm.DBServer
$smtp = $config.Configuration.SharePoint.Farm.SMTPServer
$fromAddr = $config.Configuration.SharePoint.Farm.EmailFromAddress
$replyAddr = $config.Configuration.SharePoint.Farm.EmailReplyToAddress

#Check farm passphrase - if null then use admin account password
if ($config.Configuration.SharePoint.Farm.Passphrase.Length -gt 0)
{       
    $passphrase = (ConvertTo-SecureString $config.Configuration.SharePoint.Farm.Passphrase -AsPlainText -force)   
}
else
{
     Write-Warning "Using the Farm Admin's password for a passphrase"
     $passphrase = $farmAcct.Password
}

#--------1. CREATE FARM----------
Write-Host -foregroundcolor green "# Creating Farm..."
New-SPConfigurationDatabase -DatabaseName $configDb -DatabaseServer $server -AdministrationContentDatabaseName $adminContentDb -Passphrase $passphrase -FarmCredentials $farmAcct
#Verifying farm creation
$spfarm = Get-SPFarm -ErrorAction
SilentlyContinue -ErrorVariable err
if ($spfarm -eq $null -or $err)
{

    throw "Unable to verify farm creation."
}

#--------2. SECURE SHAREPOINT RESOURCES----------
Write-Host -foregroundcolor green "# Securing SharePoint resources..."
Initialize-SPResourceSecurity

#--------3. INSTALL SERVICES----------
Write-Host -foregroundcolor green "# Installing SharePoint services..."
Install-SPService

#--------4. INSTALL FEATURES----------
Write-Host -foregroundcolor green "# Installing SharePoint features..."
Install-SPFeature -AllExistingFeatures

#--------5. INSTALL CENTRAL ADMIN----------
Write-Host -foregroundcolor green "# Installing Central Administration website..."
$installSCA = (($config.Configuration.SharePoint.Farm.CentralAdmin.Servers.Server | where {$_.Name -eq $env:computername}) -ne $null)
$url = "http://$($env:computername):$($config.Configuration.SharePoint.Farm.CentralAdmin.Port)"

$sca=[Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)

if ($installSCA -and $sca -eq $null)
{

  #Provisioning Central Administration
   New-SPCentralAdministration -Port $config.Configuration.SharePoint.Farm.CentralAdmin.Port -WindowsAuthProvider "NTLM"
   #Installing Help
   Install-SPHelpCollection -All
   #Installing Application Content

   Install-SPApplicationContent

}

#--------6. CONFIGURE OUTGOING EMAIL----------
if ($config.Configuration.SharePoint.Farm.ConfigureEmail -eq $true)
{
    Write-Host -foregroundcolor green "# Configuring Outgoing (SMTP) E-Mail..."
    ExecuteCommand "& stsadm -o email -outsmtpserver "" $smtp "" -fromaddress "" $fromAddr "" -replytoaddress "" $replyAddr ""  -codepage 65001  "

}
else
{
    Write-Host -foregroundcolor green "# Skipping Configuration of Outgoing (SMTP) E-Mail..."
}
echo ""
Write-Host -foregroundcolor green "# SharePoint install and initial configuration finished."

 

Ok, so this post is already way long so I'm not going to explain the pieces of this script. Most of them are self-explanatory and well-document elsewhere. Some great resources for scripting SharePoint are

   

 

Creating a cloneable SharePoint 2010 Development Environment with PowerShell, Windows Sysprep, and SQL Server 2008 R2 Sysprep – PART 1

by josh 26. March 2010 02:53

In the previous post, I outlined the steps involved in creating a cloneable image that can be quickly spun up into a working SharePoint 2010 development machine. In this post, I want to visit Steps 1-5 in more detail.

 

  1. Install a virtual machine with Windows Server 2008 R2, PowerShell 2.0 and all other desired client software (Visual Studio, Office, SharePoint Designer, etc…)
  2. Install SQL Server 2008 R2 as a Prepared Image
  3. Install SharePoint 2010 Prerequisites (http://technet.microsoft.com/en-us/library/cc262485(office.14).aspx) either manually or using the Prerequisites Installer tool
  4. Install SharePoint 2010 bits, but DO NOT run configuration wizard
  5. Run Sysprep.exe to prepare server

 

Step 1 – Install OS and Client software

I won’t go into a whole lot of detail on this step because it should be pretty straightforward. For my SharePoint 2010 development machine I run Windows Server 2008 R2 and the following client software:

  • Visual Studio 2010
  • SQL Management Studio
  • Office 2010 (including Visio Premium for workflow visualization)
  • SharePoint Designer 2010
  • Other useful tools (Reflector, U2U CAML Query tool, CodeRush/Refactor Pro, …)

Step 2 – Install SQL Server 2008 R2 as a Prepared Image

**This is a feature of R2. SQL Server 2008 (pre-R2) does not have the Sysprep functionality.

Performing these steps will install the SQL Server 2008 R2 bits and prepare the server to be configured at a later time.

  1. Run the SQL Server 2008 R2 setup executable.
  2. On the Installation Center dialog, choose Advanced from the menu on the left. 
    sql installation center
  3. Click the Image preparation of a stand-alone instance of SQL Server link. 
    sql installation center - prepare image
  4. When the System Configuration Checker completes, click OK.
  5. Accept the license terms and click OK.
  6. On the Setup Support Files screen, click Install.
  7. When the setup files are installed, click Next and choose the features to be installed. 
    sql install feature sel
    **Only server features are available as part of the image preparation. Client features, such as Management Studio, can be installed normally before running Sysprep on the machine.
  8. On the Instance Configuration screen, enter an Instance ID for the instance being prepared. This will be required when completing the image after cloning. 
    sql installation center - instance config
  9. On the Disk Space Requirements screen, review requirements and click Next.
  10. On the Prepare Image Rules screen, verify that there are no errors or warnings and click Next.
  11. On the Ready to Prepare Image screen, review settings and click Prepare.
  12. On the Complete screen, click Close.

Step 3 – Install SharePoint 2010 Prerequisites

SharePoint 2010 includes a new Prerequisites installer that will automatically download and install the necessary prerequisites. Of course, you must be connected to the internet for the prerequisites to be downloaded. You can alternatively download the prerequisites manually and install them one-by-one. (http://technet.microsoft.com/en-us/library/cc262485(office.14).aspx)

  1. Open the SharePoint 2010 installation media and run PrerequisiteInstaller.exe.
  2. Review the prerequisites that will be installed and click Next
    spprereq1
  3. Accept the license terms and click Next.
  4. When the installation completes, click Finish.
  5. Restart the VM.

Step 4 – Install SharePoint Server 2010 Binaries

  1. Open the SharePoint 2010 installation media and run Setup.exe.
  2. Enter the license key and click Continue.
  3. Choose Server Farm install.
  4. On the Server Type screen, choose Complete and click Install Now.
  5. When installation is compl ete, on the Run Configuration Wizard screen, clear the Run the SharePoint Products Configuration Wizard now checkbox and then click Close
    sharepoint install 1

Step 5 – Run Sysprep to Prepare Machine for Cloning

  1. Navigate to C:\Windows\System32 and run Sysprep.exe
  2. In the System Preparation Tool dialog, in the System Cleanup Action section, choose Enter System Out-of-Box Experience(OOBE) and select the Generalize checkbox.
  3. In the Shutdown Options section, choose Shutdown and click OK
    sysprep dialog
  4. Sysprep will configure the machine and then automatically shut down.

The base virtual machine image is now ready to be cloned. I’ll detail the process of cloning the VM and scripting the configuration of SQL Server and SharePoint in the next post…

Creating a cloneable SharePoint 2010 Development Environment with PowerShell, Windows Sysprep, and SQL Server 2008 R2 Sysprep – Overview

by josh 26. March 2010 02:50

Goal: To be able to execute a single command and have a fresh, sysprepped, SharePoint 2010 development environment ready to go within minutes.

 

**Note I’m using VMware Workstation, but you should be able to use other virtualization software as long as it supports 64-bit operating systems)

 

With most SharePoint development being done in VMs, it is useful to have a set of sysprepped base VMs that can be cloned at any time to create a new environment, whether it be for a new project, to test some tool or software, or whatever. Sysprep is a great tool for virtual domains because it will generate a new security ID (SID) for every server created from the sysprepped image to prevent conflicts between servers on the same domain.

 

Most of the software needed for development can be installed prior to running sysprep on the virtual machine,but SQL Server and SharePoint (at least the configuration of SharePoint) are generally exceptions. These apps typically must be installed post-sysprep in order for them to operate correctly, and this is an involved and tedious process. Fortunately with SQL Server 2008 R2, we now have SQL Server Sysprep, which will allow SQL Server to be installed and imaged, but not configured until a new VM is created from the base image. Woohoo!

 

So, the general steps are as follows:

  1. Install a virtual machine with Windows Server 2008 R2, PowerShell 2.0 and all other desired client software (Visual Studio, Office, SharePoint Designer, etc…)
  2. Install SQL Server 2008 R2 as a Prepared Image
  3. Install SharePoint 2010 Prerequisites (http://technet.microsoft.com/en-us/library/cc262485(office.14).aspx) either manually or using the Prerequisites Installer tool
  4. Install SharePoint 2010 bits, but DO NOT run configuration wizard
  5. Run Sysprep.exe to prepare server
  6. Clone server
  7. Boot clone and execute PowerShell script to perform the following actions:
    • Complete configuration of sysprepped SQL Server 2008 R2 database server
    • Configure SharePoint 2010 farm

 

The PowerShell script can perform as much (or as little) configuration of the SharePoint environment as you wish. There are a whole slew of new cmdlets (Gary LaPointe lists all 535 of them here) that come with SharePoint 2010, so most actions are as easy as finding the appropriate cmdlet to run.

 

I’m going to split the detailed steps into two separate posts to make them a little more manageable. The first will cover steps 1-5 above, and the second will cover the rest of the steps.