Source : http://sharepoint.microsoft.com

Earlier in the week I posted about SPModule, which greatly simplifies setting up a SharePoint farm.  But when you pair this functionality with PowerShell remoting—the real “power” shines.   I’m going to show you how, after a couple of initial setup concepts, you can setup a multi-machine farm with just a few lines of PowerShell!
Prereqs: RemotingThe only pre-requisite for this exercise is having PowerShell 2.0 RTM installed and enabled for remoting.  PowerShell bits are available for earlier versions of Windows Server here—but should already be ready on your Windows 2k8 R2 installs.
Then, to enable PowerShell Remoting for SharePoint you need to open up the SharePoint Console “As an Administrator” on EACH machine that will be receiving remote commands (i.e.: all machines for the farm) and run the following commands:
      Enable-PSRemoting -force
      Enable-WSManCredSSP –role Server -force
      Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1000
                Note – the above commands are doing the following in this order:
                                1. Enabling Remoting  (WSMan)
                                2. Enable CredSSP support in WSMan
                                3. Increases the local memory limit to 1000 MB (I recommend somewhere between 512 to 1000 MB)
Finally, from the machine that will be executing the remote commands, you need to run the following commands to enable client-remoting.  I’m going to use the “Admin Machine” (which is essentially the first machine in my farm) as the initiator of the script—but you can also use a machine that is not going to be part of the farm. 
Enable-PSRemoting -force
      Enable-WSManCredSSP –role Client –DelegateComputer ” -force
The DelegateComputer parameter needs to be updated with an expression or machine(s) that will be delegated to.  If you want to delegate across the domain, you could write “*.mydomain.com”. Additionally “*” can be used to easily pass this step, but this is very bad security-wise (it allows credential delegation to all machines).  

Share SPModuleThe last step to getting prepared—is sharing out the folder that contains SPModule (e.g.: \\servername\spmodule), which you can install according to my last post. 
The InstallNow for the fun! The script is really split into two pieces, installing SharePoint and creating the farm on the local machine (my “Admin Box”) and then joining the rest of the machines to the farm.   So, let’s begin.  I’ll go through all the steps and then present the whole script together—keep in mind I’ve kept his pretty simply so error handling and additional logic is “an exercise for the reader”:
1.       Define the additional machines we are using
$machinelist = (‘machine1’,‘machine2’)
2.       Get the credential to use for remoting
$cred = Get-Credential
3.       Load the Modules (from our share)
$env:PSModulePath = “\\ServerName\SPModule;” + $env:PSModulePath
Import-Module SPModule.misc
Import-Module SPModule.setup
4.       Install SharePoint on the local box:
Install-SharePoint -SetupExePath \\servername\SharePoint2010-Beta\setup.exe” -PIDKey“PKXTJ-DCM9D-6MM3V-G86P8-MJ8CY”
5.       Install SharePoint on the remote boxes, which means:
a.       Create our script to run remotely which will load the modules and call “install”
b.      Connect to the remote machines and execute the script:
$script = {$env:PSModulePath = \\ServerName\SPModule;” + $env:PSModulePath;
Import-Module SPModule.miscImport-Module SPModule.setup;
Install-SharePoint -SetupExePath \\servername\SharePoint2010-Beta\setup.exe -PIDKey “PKXTJ-DCM9D-6MM3V-G86P8-MJ8CY”
}
$machinelist |%Invoke-Command -ComputerName $_ -Credential $cred -Authentication Credssp-ScriptBlock $script -AsJob  }
                Helpful Hints: If your jobs are failing right away, remove the “AsJob” to see the error inline.  Also, if your jobs are failing after a long period of time, you may be needing a reboot during your install.  You can do this by sending a restart-computer to the script block above, then waiting for the remote machine to reboot and call install again. Alternatively you can make sure your initial Windows image has enough prereqs to avoid a reboot.
6.       We did the above commands as a job (so they could run in parallel) so now we need to wait for them to complete…
While((Get-Job -State Running-ne $null){ Write-Progress “Install Jobs are being completed” -Status “Please Wait…” -PercentComplete 0; sleep 30 }
7.       And now we can deploy the farm locally:
   New-SharePointFarm –DatabaseAccessAccount ($cred–DatabaseServer “SQL01” –FarmName“TestFarm”
8.       And then join the rest of the machines (we need to do this one at a time on this one to keep things “okay”!).  Unfortunately, this will prompt you for the passphrase (it’s the password of the user account you’re prompted for in step 2).  You can add “-PassPhrase ” to keep this from prompting, but it will put your password in plaintext; I’ll try to blog about a workaround for this in another post.
$script = {$env:PSModulePath = “\\ServerName\SPModule;” + $env:PSModulePath;
Import-Module SPModule.miscImport-Module SPModule.setup;
      Join-SharePointFarm -DatabaseServer “SQL01” -ConfigurationDatabaseName“TestFarm_SharePoint_Configuration_Database”
}
$machinelist |%Invoke-Command -ComputerName $_ -Credential $cred -Authentication Credssp-ScriptBlock $script }
9.       Done!
Here’s the full script—just change the machines, paths, sql machine name, and farm name (highlighted items)—then go grab some coffee!  When you return you’ll be a couple PassPhrases away from a multi-machine farm!
$machinelist = (‘machine2’,‘machine3’)  #machine1 is the local machine
$cred = Get-Credential
$env:PSModulePath = \\ServerName\SPModule;” + $env:PSModulePath
Import-Module SPModule.misc
Import-Module SPModule.setup
Install-SharePoint -SetupExePath \\servername\SharePoint2010-Beta\setup.exe” -PIDKey“PKXTJ-DCM9D-6MM3V-G86P8-MJ8CY”
$script = {
$env:PSModulePath = \\ServerName\SPModule;” + $env:PSModulePath;
Import-Module SPModule.miscImport-Module SPModule.setup;
Install-SharePoint -SetupExePath \\servername\SharePoint2010-Beta\setup.exe” -PIDKey “PKXTJ-DCM9D-6MM3V-G86P8-MJ8CY”
}
$machinelist |%Invoke-Command -ComputerName $_ -Credential $cred -Authentication Credssp-ScriptBlock $script -AsJob  }
While((Get-Job -State Running-ne $null){ Write-Progress “Install Jobs are being completed” -Status “Please Wait…” -PercentComplete 0; sleep 30 }

   New-SharePointFarm –DatabaseAccessAccount ($cred–DatabaseServer SQL01 –FarmNameTestFarm

$script = {$env:PSModulePath = \\ServerName\SPModule;” + $env:PSModulePath;
Import-Module SPModule.miscImport-Module SPModule.setup;
            Join-SharePointFarm -DatabaseServer SQL01 ConfigurationDatabaseNameTestFarm_SharePoint_Configuration_Database”
}
$machinelist |%Invoke-Command -ComputerName $_ -Credential $cred -Authentication Credssp-ScriptBlock $script }
Note: for the truly adventurous, you can enhance this script greatly by including VM management.  I’d recommend looking at this project for ideas…

By Indra

SharePoint Architect

Leave a Reply