In this PowerShell and SharePoint series I'd thought I'd spin through, in order of use, the commands used to create the demo Portal site in the SUGUK user group presentation.
First step: Creating a Web Application using the API.
The new SPWebApplicationBuilder object makes this easy. This object creates a Web Application using a default port and the default settings, you can override any settings you like in your script but for simplicity I'm just changing the default timezone.
SPWebApplicationBuilder also creates an AppPool to go along with the new IIS Website which has Network Service as the user. In Beta 2TR that account doesn't have the correct permissions so you need to change it to a local administrator. I tend to dev on a Virtual machine DC so I'd use the domain admin.
So we have three routines
new-SpWebApplication - Create the IIS Website and SharePoint settings
set-apidentity - set the Application Pool credentials
get-defaulttimezoneid - returns the Greenwich Meantime TimeZone ID (adjust this one to suit)
We also then need to create the set of webs that will live inside the Web Application, this is done by calling the Add Method on the Sites method of the Web Application passing it various parameters the most important of which is the template ID, in this case SPSPORTAL to give us the default SharePoint Intranet portal look.
The Powershell commands to tie all this together are
$webapp=new-SPWebApplication
set-apIdentity "http://sps:yourportnumber" "contoso\administrator" "password"
#Create Portal Site Collection
$webapp.Sites.Add("/", "SUGUK Intranet","SUGUK Intranet",1033, "SPSPORTAL", "contoso\administrator", "administrator", "administrator@contoso.com")
Here's new-SPWebApplication
# Function: new-SPWebApplication
# Description: Create and return a new SPWebApplication object
# Use the default values which will create the web site at a random port
# Parameters: none
#
function new-SPWebApplication()
{
# Get our local SPFarm object
$spfarm = [Microsoft.SharePoint.Administration.SPfarm]::Local
# Use the new SPWebApplicationBuilder
$appbuilder= new-object Microsoft.SharePoint.Administration.SPWebApplicationBuilder $spfarm
# Create Web Application with the default settings
$webapplication = $appbuilder.Create()
# Set the timezone to Greenwich Mean Time
$timezone=get-defaulttimezoneid
$webapplication.DefaultTimeZone = $timezone.ID
$webapplication.Update()
# Actually queue the application for creation
$webapplication.Provision()
# return the SPWebApplication object
$webapplication
}
# Return the Default TimeZone as used for the majority of our sites
#
function get-defaulttimezoneid
{
[Microsoft.SharePoint.SPregionalSettings]::Globaltimezones | where-object { $_.Description -like "*greenwich*" }
}
Now to change the Credentials for the AppPool is also really simple in V3. The SPWebApplication object exposes a ApplicationPool property which you can use to set its credentials
# Function: set-apidentity
# Description: Set the credentials for the application pool for the given Web Application
# Parameters: Url Site Collection URL
# UserName UserName
# Password Password
function set-apidentity([string]$SiteCollectionURL, [string]$UserName, [string]$Password)
{
$webapp=get-spwebapplication $SiteCollectionURL
$webapp.ApplicationPool.CurrentIdentityType= [Microsoft.SharePoint.Administration.IdentityType]::SpecificUser
$webapp.ApplicationPool.UserName= $Username
$webapp.ApplicationPool.Password= $Password
# Save the settings
$webapp.ApplicationPool.Update()
# Roll the settings out via a Admin Job
$webapp.ApplicationPool.Provision()
}
To me this is way easier than using the UI.