So here is the first script in fully configuring a SharePoint Farm using PowerShell. I have included the script and following the script, I will discuss some of the commands that I used within the function.
1 function Install-SPFarm
2 {
3 <#
4 .DESCRIPTION
5 Install a SharePoint Farm Instance
6
7 .SYNOPSIS
8 Install a SharePoint 2010 Farm
9
10 .EXAMPLE
11 Install-SPFarm
12 This will install a SharePoint farm using the default parameters set in the script.
13
14 .EXAMPLE
15 Install-SPFarm -farmAcctName "domain\user" -farmAcctPwd "password" -configdb "SharePoint_Config" -adminContentDb "SharePoint_Content_Admin" -server "DBServer" -passphrase "$#@reP01nt" -WSSUSageappName "SharePoint_Usage_Application" -WSSUsageDBName "SharePoint_Usage_Application" -port 5000 -Authentication "NTLM"
16 This will install a SharePoint farm using the specified parameters.
17
18 .LINK
19 http://woodssharepoint.wordpress.com/
20 #>
21 param(
22 [string]$farmAcctName = "domain\svc_sp_farm",
23 [string]$farmAcctPwd = "P@ssword",
24 [string]$configDb = "SharePoint_Config",
25 [string]$adminContentDb = "SharePoint_Content_Admin",
26 [string]$server = "DBSERVER",
27 [string]$passphrase = "$#@reP01nt",
28 [string]$WSSUsageAppName = "SharePoint_Usage_Application",
29 [string]$WSSUsageDBName = "SharePoint_Usage_Application",
30 [int]$port = 5000,
31 [string]$Authentication = "NTLM"
32 )
33
34 # Create PSCredential Object from parameters
35 $secPassword = ConvertTo-SecureString $farmAcctPwd -AsPlainText -Force
36 $farmCreds = New-Object System.Management.Automation.PSCredential($farmAcctName,$secPassword)
37
38 # Create SecureString of Pass Phrase
39 $secPassPhrase = ConvertTo-SecureString $passphrase -AsPlainText -Force
40
41 # Create a new SharePoint Configuration and Administration database, use $passphrase as the passphrase.
42 New-SPConfigurationDatabase `
43 -DatabaseName $configDb `
44 -DatabaseServer $server `
45 -AdministrationContentDatabaseName $adminContentDb `
46 -Passphrase $secPassPhrase `
47 -FarmCredentials $farmCreds
48
49 # Check for Farm Configuration
50 $spFarm = Get-SPFarm -ErrorAction SilentlyContinue -ErrorVariable err
51 if ($spFarm -eq $null -or $err)
52 {
53 throw "unable to verify farm creation"
54 }
55
56 #Enforces resource security on the local server
57 Initialize-SPResourceSecurity
58
59 #Installs services on a farm
60 Install-SPService
61
62 #The file system is scanned and any new features are installed
63 Install-SPFeature -AllExistingFeatures
64
65 #Creates a new Central Administration Web application and starts the central administration service on the local machine
66 New-SPCentralAdministration -Port $port -WindowsAuthProvider $Authentication
67
68 #Installs the Help site collection files for SharePoint 2010 Products in the current farm
69 Install-SPHelpCollection -All
70
71 #Copies shared application data to existing Web application folders
72 Install-SPApplicationContent
73
74 #Install WSS_Usage Application
75 New-SPUsageApplication -Name $WSSUsageAppName -DatabaseServer $server -DatabaseName $WSSUsageDBName
76 }
So Lines 3 – 20 gives you the Help information of the script. This is found when you type Get-Help after executing the initial PS script. So when you type Get-Help Install-SPFarm, you will get the following information:
Lines 21-32 create the parameters for the rest of the script. As best practice, I will add default values for all of the parameters. Keep in mind that if you don’t specify actual values when running the function, you will get errors (unless your Active Directory domain is “domain”, etc). I request the Farm Account Name and Password during the initiation of the script, this could cause an security issue if you are running this script remotely.
Lines 35 and 36 convert the password that was entered into a Secure String and then creates a new credential object with the username and secured password. Line 39 changes the Pass Phrase to a Secure String as well.
Lines 42-54 starts the actual configuration of your SharePoint 2010 environment. The first step when configuring a SharePoint Environment is to get the Configuration Database and Farm credentials setup. This is done using the New-SPConfigurationDatabase PowerShell script. I passed the parameters from my initial script into this command, plus the two secured parameters. When the script runs, it will create a SharePoint Configuration database specified in the $configDb parameter on the server specified in the $server parameter. It will also create the Central Administration Content Database specified in the $adminContentDb. When you use the Configuration Wizard after installing the bits for SharePoint, the Administrator’s Content Database is automatically created with a GUID at the end of the name. The final two parameters that are passed are the Pass Phrase and the Farm Credentials, which require a Secure String and PSCredentials (the ones that we set after receiving the initial parameters, remember lines 35-39). Finally, lines 50-54 get the SharePoint Farm information that was just created and checks that it was successfully created.
1 # Create a new SharePoint Configuration and Administration database, use $passphrase as the passphrase.
2 New-SPConfigurationDatabase `
3 -DatabaseName $configDb `
4 -DatabaseServer $server `
5 -AdministrationContentDatabaseName $adminContentDb `
6 -Passphrase $secPassPhrase `
7 -FarmCredentials $farmCreds
8
9 # Check for Farm Configuration
10 $spFarm = Get-SPFarm -ErrorAction SilentlyContinue -ErrorVariable err
11 if ($spFarm -eq $null -or $err)
12 {
13 throw "unable to verify farm creation"
14 }
15
The final lines (56-75) finish the configuration. The Initialize-SPResourceSecurity cmdlet enforces resource security on the local server, including files, folders, and registry keys. Install-SPService installs and optionally provisions services on a farm. This cmdlet installs all services, service instances, and service proxies specified in the registry on the local server. The Install-SPFeature –AllExistingFeatures will scan the file system and install any new features. The New-SPCentralAdministration creates the Central Administration Web Application and starts the service on the local server. The two options I use is to set the Port, Authorization Provider. The Install-SPHelpCollection –All installs the Help site collection files for the current SharePoint farm. The Install-SPApplicationContent copies shared application data to existing Web application folders. The final cmdlet used is the New-SPUsageApplication cmdlet. This cmdlet creates a new usage application for the farm. Running this script allows you to choose the Name of the application, plus the Database Server and Database Name. If this cmdlet is not run, it will auto-provision in the future with default names.
1 #Enforces resource security on the local server
2 Initialize-SPResourceSecurity
3
4 #Installs services on a farm
5 Install-SPService
6
7 #The file system is scanned and any new features are installed
8 Install-SPFeature -AllExistingFeatures
9
10 #Creates a new Central Administration Web application and starts the central administration service on the local machine
11 New-SPCentralAdministration -Port $port -WindowsAuthProvider $Authentication
12
13 #Installs the Help site collection files for SharePoint 2010 Products in the current farm
14 Install-SPHelpCollection -All
15
16 #Copies shared application data to existing Web application folders
17 Install-SPApplicationContent
18
19 #Install WSS_Usage Application
20 New-SPUsageApplication -Name $WSSUsageAppName -DatabaseServer $server -DatabaseName $WSSUsageDBName
After running this script, you will be able to launch Internet Explorer and go the Central Administration site/port that you specified during the configuration. You will see in the Services that the Usage and Health Data Collection Service Application.
My Next post will be to configure the State Service Application, which if you do not run the Configuration Wizard’s, cannot be created through the Central Administration site.