Thursday, May 26, 2011

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 administration. Let’s call this user as “yourDomain\spFarmAdmin”.


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


Add spAdmin user in Administrators group of SQL Server and machine on which SharePoint is installed. Basically, this SharePoint 2010 setup administrator “spAdmin”, has to be a member of administrators 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([string] $SQLServer)
{
            try
            {
                        $Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server=$SQLServer;Database=Master;User Id=sa;Password=Acc1234#"
                        $Connection.Open()
                        return $true;
            }
            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\Registration\"
.\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-SecureString $farmAcctPwd -AsPlainText -Force
$farmCreds = New-Object System.Management.Automation.PSCredential($farmAcctName,$secPassword)

# Create SecureString of Pass Phrase
$secPassPhrase = ConvertTo-SecureString $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 Administration database and configure farm with appropriate commands                         
New-SPConfigurationDatabase -DatabaseName $configDb -DatabaseServer $server -AdministrationContentDatabaseName $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 Central Administration Web application and starts the central administration service on the local machine
New-SPCentralAdministration -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 central administration 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!!

No comments:

Post a Comment