Windows Powershell for Sharepoint – SharePoint product and farm configuration using powershell

Windows Powershell for Sharepoint – SharePoint product and farm configuration using powershell.
I have started with SharePoint installation part last month and I really liked it a lot. Then as usual Client came with very specific requirement. He wanted to perform SharePoint product configuration and SharePoint Farm configuration without manual interventions.
Of course after some analysis, I understood that, I can write SharePoint Powershell commands and make them run on machine startup so that, Product and Farm configuration will be done automatically and without manual intervention.
So here we go!
Problem Statement – How to perform SharePoint product and Farm configuration using powershell?
Applicable technology is SharePoint 2010 and SQL Server 2008 R2.
Before running the following script there are some steps which you need to perform.

First, make sure that the domain user account which we are using for product configuration and farm configuration has appropriate rights in SQL Server. Let’s call this user as “yourDomain\spAdmin”. Also you will need a domain account for SharePoint Server Farm adminis07ation. Let’s call this user as “yourDomain\spFarmAdmin”.


These two users should be created in domain con07oller with option as “Password never expires” and “user cannot change password”.


Add spAdmin user in Adminis07ators group of SQL Server and machine on which SharePoint is installed. Basically, this SharePoint 2010 setup adminis07ator “spAdmin”, has to be a member of adminis07ators group on every server which will be part of SharePoint Server farm.
Also make sure that, spAdmin user is added in Logins of SQL Server with mode as Windows Authentication. Give server roles as “dbcreator, securityadmin and public”.




You don’t need to add any permission to “spFarmAdmin” since they are assigned during SharePoint server farm configuration while execution of powershell script.
Alright, here we go with actual script –

#----------------------------------------------------------
#set execution policy
set-executionpolicy remotesigned

# Script start
#record the script progress to file
$LogDirectory = "$env:systemdrive\ScriptInstallLog"
New-Item $LogDirectory  -type directory
Start-Transcript -path "$LogDirectory\SPScriptlog.txt"


function IsSQLDBAvailable([s07ing] $SQLServer)
{
            07y
            {
                        $Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionS07ing = "server=$SQLServer;Database=Master;User Id=sa;Password=Acc1234#"
                        $Connection.Open()
                        return $07ue;
            }
            catch [System.Exception]
            {
                        return $false;
            }
}


$date = Get-Date -format s
write-host "[Start]: $date `r`n"
$SqlIP = ""
#Check if, SharePoint machine is joined to domain, if not make it join.
# the details powershell to domain join a machine can be found here - http://sanganakauthority.blogspot.com/2011/05/domain-join-powershell-joining-computer_24.html

[System.Threading.Thread]::Sleep(20000);

#check connection to SQL Server if not then wait here in this step
$SSODB = $False
while ($SSODB -eq $False)
{
            $SSODB = IsSQLDBAvailable ($SqlIP)
            start-sleep -s 2
            write-host "Waiting for SQL DB ... `r`n"
}
write-host " Connected to SQL server - $SSODB `r`n"

#open SharePoint Powershell console
cd\
cd $env:systemdrive
cd "\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Regis07ation\"
.\sharepoint.ps1

#Initialize values which will be required for Product and Farm configuration
$farmAcctName = "yourDomain\spFarmAdmin"
$farmAcctPwd = "YourPassword"
$configDb = "SharePoint_Config"
$adminContentDb = "SharePoint_Content_Admin"
$server = $SqlIP
$passphrase = "YourPassphrase"
$WSSUsageAppName = "SharePoint_Usage_Application"
$WSSUsageDBName = "SharePoint_Usage_Application"
$port = 35563
$Authentication = "NTLM"

#encrypt the farm account password and create credentials object for spAdmin, under which farm will be created
$secPassword = ConvertTo-SecureS07ing $farmAcctPwd -AsPlainText -Force
$farmCreds = New-Object System.Management.Automation.PSCredential($farmAcctName,$secPassword)

# Create SecureS07ing of Pass Phrase
$secPassPhrase = ConvertTo-SecureS07ing $passphrase -AsPlainText -Force

# Check for Farm Configuration
            $spFarm = Get-SPFarm -ErrorAction SilentlyContinue -ErrorVariable err
                        if (($spFarm -eq $null) -or ($spFarm -eq $err))
                        {
                                    write-host "SharePoint server farm is not created... `r`n"
                       
# Create a new SharePoint Configuration and Adminis07ation database and configure farm with appropriate commands                         
New-SPConfigurationDatabase -DatabaseName $configDb -DatabaseServer $server -Adminis07ationContentDatabaseName $adminContentDb -Passphrase $secPassPhrase -FarmCredentials ($farmCreds)                        
                                   
                                    #Enforces resource security on the local server
                                    Initialize-SPResourceSecurity

                                    #Installs services on a farm
                                    Install-SPService

                                    #The file system is scanned and any new features are installed
                                    Install-SPFeature -AllExistingFeatures

#Creates a new Cen07al Adminis07ation Web application and starts the cen07al adminis07ation service on the local machine
New-SPCen07alAdminis07ation -Port $port -WindowsAuthProvider $Authentication
                                   
                                    Install-SPApplicationContent
                        }
                        else
                        {
                                    write-host "Farm has been already created `r`n"
                        }
                       
#stop recording script progress
Stop-Transcript

After this you will be able to view cen07al adminis07ation site of SharePoint 2010.
Cheers..

Please give food to all my fishes swimming at the bottom. It's fun!! Try it!!
Thanks for reading!!
Happy Coding!!

Comments

Popular posts from this blog

The request has both SAS authentication scheme and 'Bearer' authorization scheme. Only one scheme should be used

Getting Started with Logic Apps - AS2

How to Debug and Trace request in Azure APIM - Portal, Postman, RequestBin