Monday, September 8, 2014

HowTo Create SPFarm (SharePoint Farm) with Powershell

After binary installation of SharePoint do not run configuration wizard to build your farm.
Try instead this simple PowerShell script in order to create a clean Farm with Central Administration deployed an SQL Server database without GUID (replace italic information with your environment).
Run this script within SharePoint Management Shell with administrative permissions.

# Service accounts
$DOMAIN = "yourdomain"
$accounts = @{}
$accounts.Add("Farm", @{"username" = ($DOMAIN + "\yourfarmaccount"); "password" = "yourfarmaccountpassword"})

Foreach ($account in $accounts.keys) {
    $accounts.$account.Add(`
    "Credential", `
    (New-Object System.Management.Automation.PsCredential $accounts.$account.username, `
    (ConvertTo-SecureString -String $accounts.$account.password -AsPlainText -Force)))
}

# Farm configuration

$config_Passphrase = "yourpassphrase"
$s_config_Passphrase = (ConvertTo-SecureString -String $config_passphrase -AsPlainText -force)

$server_DB = "SQLSERVERALIAS"
$db_Config = "SP2013_ConfigDb"
$db_CentralAdmin = "SP2013_Content_CentralAdministration"

$ca_port = 2013
$ca_AuthProvider = "NTLM"

########################################
# Create the farm

Add-PSSnapin Microsoft.SharePoint.PowerShell

Write-Output "Creating the configuration database $db_Config"
New-SPConfigurationDatabase `
-DatabaseName $db_Config `
-DatabaseServer $server_DB `
-AdministrationContentDatabaseName $db_CentralAdmin `
-Passphrase  $s_config_Passphrase `
-FarmCredentials $accounts.farm.credential

# Check to make sure the farm exists and is running. if not, end the script
$farm = Get-SPFarm
if (!$farm -or $farm.Status -ne "Online") {
    Write-Output "Farm was not created or is not running"
    exit
}

Write-Output "Create the Central Administration site on port $ca_Port"
New-SPCentralAdministration `
-Port $ca_Port `
-WindowsAuthProvider $ca_AuthProvider


# Perform the config wizard tasks

Write-Output "Install Help Collections"
Install-SPHelpCollection -All

Write-Output "Initialize security"
Initialize-SPResourceSecurity

Write-Output "Install services"
Install-SPService

Write-Output "Register features"
Install-SPFeature -AllExistingFeatures

Write-Output "Install Application Content"
Install-SPApplicationContent