Thursday, May 26, 2011

Windows Powershell for Sharepoint – Adding server to an existing SharePoint server farm using powershell

Windows Powershell for Sharepoint – Adding server to an existing SharePoint server farm using powershell

So… after post of  configuring SharePoint product and farm using power shell here I come with another post of Sharepoint and Powershell.
Today I will be discussing, how we can make a machine join to existing SharePoint server farm using powershell.
Problem Statement – I have a SharePoint server farm configured with me. I want to join another server to SharePoint Server Farm using powershell.
Before going to actual script, first I would recommend you to read above mentioned post which will help you to create necessary users and provide suitable access rights to them. As stated in this post, you will need to add spAdmin user in Administrators group of the machine which you want to join to the SharePoint server farm.
Alright, so here we go!

#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\SP2Scriptlog.txt"

function IsSQLDBAvailable([string] $SQLServer)
            $Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server=$SQLServer;Database=Master;trusted_connection=true;"
            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 -

#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
#$OpenSPPowerShell = 'powershell.exe "&'' $systemdrive ''"'
#invoke-expression $OpenSPPowerShell
cd $env:systemdrive
cd "\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\"

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

$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 if main SharePoint server farm creation is completed
$IsMainFarmCreated = $false
While($IsMainFarmCreated -eq $false)
$IsMainFarmCreated = [Microsoft.SharePoint.Administration.SPFarm]::Open("server=$SqlIP;Database=$configDb;trusted_connection=true;")
            start-sleep  -s 2
            write-host "waiting for Main SharePoint Farm Server... `r`n"
            write-host "Main SharePoint Farm Server created... `r`n"

# Check for Farm Configuration on this machine
            $spFarm = Get-SPFarm -ErrorAction SilentlyContinue -ErrorVariable err
                        if (($spFarm -eq $null) -or ($spFarm -eq $err))
                                    write-host "Server not joined to any Farm... `r`n"                               
                                    # Connect to existing farm                                                                 
Connect-SPConfigurationDatabase –DatabaseName $configDb –DatabaseServer $server –Passphrase $secPassPhrase
                                    #Enforces resource security on the local server

                                    #Installs services on a farm

                                    #The file system is scanned and any new features are installed
                                    Install-SPFeature -AllExistingFeatures                                   
                                    write-host "Already joined to the farm `r`n"
#stop recording script progress

After this script if you run the SharePoint Central Administration website on Main SharePoint Farm Server, you will be able to view the joined server.

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