Some Nice PowerShell Scripts for Configuring a SharePoint 2010 Farm

Written by Denis Stadler on . Posted in IT Pro & PowerShell

The main idea behind this post is the wish of getting a configured SharePoint farm which has friendly and standardized names for the databases created during the configuration process.

Below I will post only the PowerShell scripts that I used to provision the services that don’t offer the possibility of configuring the database name(s).

If you are searching a fully automated SharePoint installation solution, please check AutoSPInstaller on CodePlex.

Central Administration Provisioning

The following script provisions the central administration. Run it on the first SharePoint server within your farm, on the server that is going to host the Central Administration web application.

# Add SharePoint PS Snapin- uncommend if you need it
# Add-PsSnapin Microsoft.SharePoint.PowerShell

# Disable loopback check - uncomment only if you are deploying a test machine
# New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -value "1" -PropertyType dword

# Parameters - replace with your own
$DatabasePrefix = "SPStaging"
$DatabaseServer = "SQL"
$Passphrase= "Farm-Passphrase"

# This is the port for central administration web application
$Port = 80
$AuthType = "NTLM"

# Creates configuration databases
# Here the script will prompt for the farm account credentials
$DatabaseName = $DatabasePrefix + "_Config"
$AdministrationContentDatabaseName = $DatabasePrefix + "_Admin_Content"
New-SPConfigurationDatabase –DatabaseName $DatabaseName –DatabaseServer $DatabaseServer –AdministrationContentDatabaseName $AdministrationContentDatabaseName –Passphrase (ConvertTo-SecureString $Passphrase –AsPlaintext –Force) –FarmCredentials (Get-Credential)
Install-SPHelpCollection –All
Install-SPFeature –AllExistingFeatures

# Creates central administration web application and site
New-SPCentralAdministration –Port $Port –WindowsAuthProvider $AuthType

Central Administration Extras

Below you can find some extra PowerShell command that I considered useful:

# Change central administration site's port
$Port = 2010
Set-SPCentralAdministration -Port $Port

# Add new managed account
New-SPManagedAccount -Credential (Get-Credential)

Add Additional Member Servers to the SharePoint Farm

After installing SharePoint 2010 on the new member server, use the following script:

$DatabasePrefix = "SPStaging"
$DatabaseServer = "SQL"
$Passphrase = Read-Host -assecurestring "Please type the passkey"
$DatabaseName = $DatabasePrefix + "_Config"

Connect-SPConfigurationDatabase -DatabaseServer $DatabaseServer -DatabaseName $DatabaseName -Passphrase $Passphrase
Install-SPHelpCollection -All
Install-SPFeature -AllExistingFeatures

WSS Usage Application Provisioning

The creation of a WSS usage application is required by the Search service.

$WSSUsageApplicationName = "WSS Usage Application"
$DatabasePrefix = "SPStaging"
#Use FQDN for the database server anme
$DatabaseServer = "sql.stadler.local"
$WSSUsageDataBaseName = $DatabasePrefix + "_WSSUsageApplication"

New-SPUsageApplication -Name $WSSUsageApplicationName -DatabaseServer $DatabaseServer -DatabaseName $WSSUsageDataBaseName   

# Provision Usage and Health Data Collection Proxy
$SAP = Get-SPServiceApplicationProxy | where-object {$_.TypeName -eq “Usage and Health Data Collection Proxy”}

User Profile Service Components Provisioning

Remeber to give the user profile application pool’s identity Replicating Directory Changes rights in Active Directory.

# Create MySite Host Web Application
$WebAppName = "spstaging-mysite.stadler.local"
$WebAppHostHeader = "spstaging-mysite.stadler.local"
$WebAppPort = 80
$WebAppAppPool = "SharePoint Default Content App Pool"
# This is a managed account
$WebAppAppPoolAccount = "STADLER\srv-SPStaging-user"
$WebAppDatabaseName = "SPStaging_Content_MySite"
$WebAppDatabaseServer = "SQL"

New-SPWebApplication -Name $WebAppName -Port $WebAppPort -HostHeader $WebAppHostHeader -URL ("http://" + $WebAppHostHeader) -ApplicationPool $WebAppAppPool -ApplicationPoolAccount (Get-SPManagedAccount $WebAppAppPoolAccount) -DatabaseName $WebAppDatabaseName -DatabaseServer $WebAppDatabaseServer

# Enumerate Template Types,  My Site Host is SPSMSITEHOST#0

# Create MySite Host Site Collection
$SiteCollectionName = "My Site Staging"
$SiteCollectionURL = ("http://" + $WebAppHostHeader)
$SiteCollectionTemplate = "SPSMSITEHOST#0"
$SiteCollectionLanguage = 1033
$SiteCollectionOwner = "STADLER\Administrator"

New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName

# Create Application Pool for the User Profile Service Application
# The user account used has to be a managed account and to have Replicating Directory Changes rights
$AppPoolName="SharePoint User Profile App Pool"
New-SPServiceApplicationPool -Name $AppPoolName -Account (Get-SPManagedAccount $WebAppAppPoolAccount)

Then you need to create the user profile service from the interface and start User Profile Service and User Profile Synchronization Service on you application servers.

State Service Provisioning

$serviceApp = New-SPStateServiceApplication -Name "State Service"
New-SPStateServiceDatabase -Name "SPStaging_StateService" -ServiceApplication $serviceApp
New-SPStateServiceApplicationProxy -Name "State Service Proxy" -ServiceApplication $serviceApp -DefaultProxyGroup

Search Application Provisioning

# 1.Setting up initial variables
$Search_Service_name = "Search Service Application SPStaging"

$ServiceAccount = "STADLER\srv-spstaging-user"
$Search_Service_account = Get-SPManagedAccount $ServiceAccount

$Search_Service_Instance = get-spenterprisesearchserviceinstance -local
$err = $null

# Start Services search services for Search_Service_Instance
Start-SPEnterpriseSearchServiceInstance -Identity $Search_Service_Instance

# 2.Create an Application Pool or use an existing one
$ExistingApplicationPool = "SharePoint Web Services Default"
$AppPool = Get-SPServiceApplicationPool $ExistingApplicationPool
# $AppPool = new-SPServiceApplicationPool -name $Search_Service_name"_AppPool" -account $Search_Service_account

# 3.Create the SearchApplicationlication and set it to a variable
$AdminDB = "SPStaging_Search"
$SearchApplication = New-SPEnterpriseSearchServiceApplication -Name $Search_Service_name -applicationpool $AppPool -databasename $AdminDB

# 4.Create search service application proxy
$Search_Service_Proxy = new-spenterprisesearchserviceapplicationproxy -name $Search_Service_name" Application Proxy" -Uri $SearchApplication.Uri.AbsoluteURI

# 5.Provision Search Admin Component
set-SPenterprisesearchadministrationcomponent -SearchApplication $SearchApplication -searchserviceinstance $Search_Service_Instance

# 6.Create a new Crawl Topology
$CrawlTopo = $SearchApplication | New-SPEnterpriseSearchCrawlTopology

# 7.Create a new Crawl Store
$CrawlStore = $SearchApplication | Get-SPEnterpriseSearchCrawlDatabase

# 8.Create a new Crawl Component
New-SPEnterpriseSearchCrawlComponent -CrawlTopology $CrawlTopo -CrawlDatabase $CrawlStore -SearchServiceInstance $Search_Service_Instance

# 9.Activate the Crawl Topology
$err = $null
$CrawlTopo | Set-SPEnterpriseSearchCrawlTopology -Active -ErrorVariable err
if ($CrawlTopo.State -eq "Active")
$err = $null
Start-Sleep -Seconds 10
until ($err -eq $null)

# 10.Create a new Query Topology
$QueryTopo = $SearchApplication | New-SPenterpriseSEarchQueryTopology -partitions 1

# 11.Create a variable for the Query Partition
$Partition1 = ($QueryTopo | Get-SPEnterpriseSearchIndexPartition)

# 12.Create a Query Component
New-SPEnterpriseSearchQueryComponent -indexpartition $Partition1 -QueryTopology $QueryTopo -SearchServiceInstance $Search_Service_Instance

# 13.Create a variable for the Property Store DB
$PropDB = $SearchApplication | Get-SPEnterpriseSearchPropertyDatabase

# 14.Set the Query Partition to use the Property Store DB
$Partition1 | Set-SPEnterpriseSearchIndexPartition -PropertyDatabase $PropDB

# 15.Activate the Query Topology
$err = $null
$QueryTopo | Set-SPEnterpriseSearchQueryTopology -Active -ErrorVariable err -ErrorAction SilentlyContinue
Start-Sleep -Seconds 10
if ($QueryTopo.State -eq "Active")
$err = $null
until ($err -eq $null)

Tags: , , , , , , , , , , , , ,

Trackback from your site.

Denis Stadler

I'm a technology enthusiast, with more than 10 years of experience in SharePoint and Dynamics CRM projects. To find more details about, please visit the about me page.

Leave a comment