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,$RsSvcPassword –username 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