Dog Food Conference 2012


This morning (and afternoon) I spoke in front of a great crowd at the Dog Food Conference in Columbus, Ohio.

I chose to re-use my Build your SharePoint Internet presence with PowerShell presentation, as I still think it’s a good presentation that gives a good introductory view into how you can automate site provisioning with PowerShell. I also try to keep it light from a technical standpoint and tend to focus more on the business case around using PowerShell as an automation tool.

Below is a description of the topic itself along with PowerPoint Slides and the PowerShell and XML code that was used in the demo:

Build your SharePoint Internet presence with PowerShell

The topic: Build your SharePoint Internet presence with PowerShell
The story: Everybody knows PowerShell is powerful, it’s in the name! But did you know that PowerShell can read and understand XML? By leveraging XML among other things, complete builds can be automated – making them efficient and predictable.

In this fun, interactive and demo-filled session – I will show you how you can leverage PowerShell to help you build your branded, company website from the ground up using PowerShell and XML. I will also pass along some tips and tricks that will help you become a PowerShell Rockstar!

Here are my slides, and below are the code snippets:

Demo Script – Setup-SPWebAppAndSite.ps1

Config XML Syntax:

<?xml version="1.0" encoding="utf-8"?>
<WebApplications>
	<WebApplication>
		<WebAppConfig>
			<Name>AdventureWorks.com</Name>
			<AppPool>SharePoint - AdventureWorksDemoWeb</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_AdventureWorks_Demo</DBName>
			<HostHeader>www.adventureworks.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http</Protocol>
			<ManagedPaths>
				<ManagedPath Type="explicit" Path="search"/>
			</ManagedPaths>
			<Solutions>
				<Solution GACDeployment="true">adventureworks_site_branding.wsp</Solution>
			</Solutions>
			<BlobCacheLocation>C:\SharePointBlobCache\AdventureWorks.com</BlobCacheLocation>
			<Authentication>
				<!--Auth Types - Claims,Classic-->
				<Type>Classic</Type>
				<!--Auth Methods - NTLM,Kerberos-->
				<AuthMethod>NTLM</AuthMethod>
				<AuthProvider Name="" 
				MembershipProviderName="" 
				RoleProviderName="" 
				/>
			</Authentication>
		</WebAppConfig>
		<SiteCollections>
			<SiteCollection>
				<Name>AdventureWorks.com</Name>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
				<Path>/</Path>
				<Features>
					<Feature>BaseSite</Feature>
					<Feature>BaseWeb</Feature>
					<Feature>PremiumSite</Feature>
					<Feature>PremiumWeb</Feature>
					<Feature>TeamCollab</Feature>
					<!--<Feature>a6ad433b-0bdf-40aa-aec8-6a091d7a3c26</Feature>-->
					<Feature>1caa8e30-f360-4ebc-a38e-e57708109e6f</Feature>
				</Features>
				<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
				<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
				<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
				<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
				<SearchSettings>
					<SearchCenterUrl>/search/pages</SearchCenterUrl>
					<!-- Possible dropdown values are-->
					<!--
					HideScopeDD_DefaultContextual
					HideScopeDD
					ShowDD
					ShowDD_DefaultURL
					ShowDD_DefaultContextual
					ShowDD_NoContextual
					ShowDD_NoContextual_DefaultURL
					-->
					<DropDownMode>ShowDD</DropDownMode>
					<TargetResultsPage>/search/pages/results.aspx</TargetResultsPage>
				</SearchSettings>
			</SiteCollection>
			<SiteCollection>
				<Name>Search Center</Name>
				<WebTemplate>SRCHCEN#0</WebTemplate>
				<Path>/search</Path>
				<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
				<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
				<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
				<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			</SiteCollection>
		</SiteCollections>
		</WebApplication>
	<WebApplication>
		<WebAppConfig>
			<Name>Contoso Extranet</Name>
			<AppPool>SharePoint - ContosoDemo</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_Contoso_Demo</DBName>
			<HostHeader>extranet.contoso.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http://</Protocol>
			<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
			<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
			<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
			<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			<RootSite>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
			</RootSite>
			<BlobCacheLocation>C:\SharePointBlobCache\Contoso.com</BlobCacheLocation>
		</WebAppConfig>
		<Sites>
			<!-- root site -->
			<Site Create="false">
				<SiteTitle>Home</SiteTitle>
				<SiteUrl>http://www.adventureworks.com/</SiteUrl>
				<Page>
				   <PageTitle>Home</PageTitle>
				   <PageUrl>Home.aspx</PageUrl>
				   <PageContent></PageContent>
				   <PageLayout>Body Only</PageLayout>
			   </Page>	
				<Page>
				   <PageTitle>Terms of Use</PageTitle>
				   <PageUrl>Terms-of-Use.aspx</PageUrl>
				   <PageContent></PageContent>
				   <PageLayout>Body Only</PageLayout>
			   </Page>
				<Page>
				   <PageTitle>Privacy Policy</PageTitle>
				   <PageUrl>Privacy-Policy.aspx</PageUrl>
				   <PageContent></PageContent>
				   <PageLayout>Body Only</PageLayout>
			   </Page>
				<Page>
				   <PageTitle>Linking Policy</PageTitle>
				   <PageUrl>Linking-Policy.aspx</PageUrl>
				   <PageContent></PageContent>
				   <PageLayout>Body Only</PageLayout>
			   </Page>
				<Page>
				   <PageTitle>Legal Disclaimer</PageTitle>
				   <PageUrl>Legal-Disclaimer.aspx</PageUrl>
				   <PageContent></PageContent>
				   <PageLayout>Body Only</PageLayout>
			   </Page>	
				<Page>
				   <PageTitle>404 - Page Not Found</PageTitle>
				   <PageUrl>Page-Not-Found.aspx</PageUrl>
				   <PageContent></PageContent>
				   <PageLayout>Body Only</PageLayout>
			   </Page>			   		   
			</Site>		
		</Sites>
	</WebApplication>
	<WebApplication>
		<WebAppConfig>
			<Name>Contoso Intranet</Name>
			<AppPool>SharePoint - ContosoDemo</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_Contoso_Demo</DBName>
			<HostHeader>intranet.contoso.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http://</Protocol>
			<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
			<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
			<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
			<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			<RootSite>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
			</RootSite>
			<BlobCacheLocation>C:\SharePointBlobCache\Contoso.com</BlobCacheLocation>
		</WebAppConfig>
	</WebApplication>
	<WebApplication>
		<WebAppConfig>
			<Name>Contoso Website</Name>
			<AppPool>SharePoint - ContosoDemo</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_Contoso_Demo</DBName>
			<HostHeader>www.contoso.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http://</Protocol>
			<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
			<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
			<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
			<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			<RootSite>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
			</RootSite>
			<BlobCacheLocation>C:\SharePointBlobCache\Contoso.com</BlobCacheLocation>
		</WebAppConfig>
	</WebApplication>
	<WebApplication>
		<WebAppConfig>
			<Name>AdventureWorks Intranet</Name>
			<AppPool>SharePoint - ContosoDemo</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_Contoso_Demo</DBName>
			<HostHeader>intranet.adventureworks.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http://</Protocol>
			<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
			<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
			<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
			<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			<RootSite>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
			</RootSite>
			<BlobCacheLocation>C:\SharePointBlobCache\Contoso.com</BlobCacheLocation>
		</WebAppConfig>
	</WebApplication>
	<WebApplication>
		<WebAppConfig>
			<Name>AdventureWorks Extranet</Name>
			<AppPool>SharePoint - ContosoDemo</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_Contoso_Demo</DBName>
			<HostHeader>extranet.adventureworks.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http://</Protocol>
			<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
			<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
			<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
			<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			<RootSite>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
			</RootSite>
			<BlobCacheLocation>C:\SharePointBlobCache\Contoso.com</BlobCacheLocation>
		</WebAppConfig>
	</WebApplication>
	<WebApplication>
		<WebAppConfig>
			<Name>My Sites</Name>
			<AppPool>SharePoint - ContosoDemo</AppPool>
			<AppPoolAccount>ISG1085\SpAppPool</AppPoolAccount>
			<DBServer>ISG1085\SharePoint</DBServer>
			<DBName>SP2010_Content_Contoso_Demo</DBName>
			<HostHeader>mysites.adventureworks.com</HostHeader>
			<InetpubPath>C:\inetpub\wwwroot\wss\VirtualDirectories</InetpubPath>
			<Port>80</Port>
			<Protocol>http://</Protocol>
			<OwnerAlias>ISG1085\SharePointRyan</OwnerAlias>
			<OwnerEmail>SharePointRyan@demowebsite.com</OwnerEmail>
			<SecondaryOwnerAlias>ISG1085\SPFarm</SecondaryOwnerAlias>
			<SecondaryOwnerEmail>SPFarm@demowebsite.com</SecondaryOwnerEmail>
			<RootSite>
				<WebTemplate>BLANKINTERNET#0</WebTemplate>
			</RootSite>
			<BlobCacheLocation>C:\SharePointBlobCache\Contoso.com</BlobCacheLocation>
		</WebAppConfig>
	</WebApplication>
</WebApplications>

Subsite XML Syntax:

<?xml version="1.0" encoding="utf-8"?>
<SubSites>
	<Sites SPSiteUrl="http://www.adventureworks.com">
		<!-- root site -->
		<Site Create="false">
			<SiteTitle>Home</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Home</PageTitle>
			   <PageUrl>Home.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>	
			<Page>
			   <PageTitle>Terms of Use</PageTitle>
			   <PageUrl>Terms-of-Use.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Privacy Policy</PageTitle>
			   <PageUrl>Privacy-Policy.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Linking Policy</PageTitle>
			   <PageUrl>Linking-Policy.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Legal Disclaimer</PageTitle>
			   <PageUrl>Legal-Disclaimer.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>	
			<Page>
			   <PageTitle>404 - Page Not Found</PageTitle>
			   <PageUrl>Page-Not-Found.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>			   		   
		</Site>		
		<!-- 2nd level sites -->
		<Site>
			<SiteTitle>Products</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/Products</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Awesome Widget</PageTitle>
			   <PageUrl>Awesome-Widget.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>The Spectacular Thingamajig</PageTitle>
			   <PageUrl>The-Spectacular-Thingamajig.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Store</PageTitle>
			   <PageUrl>Store.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Solutions</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/Solutions</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Customer Engagement</PageTitle>
			   <PageUrl>Customer-Engagement.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Custom Solutions</PageTitle>
			   <PageUrl>Custom-Solutions.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Shipping and Returns</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/Shipping-and-Returns</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Shipping Policies</PageTitle>
			   <PageUrl>Shipping-Policies.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Return Policies</PageTitle>
			   <PageUrl>Return-Policies.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>About Us</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/About-Us</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Company</PageTitle>
			   <PageUrl>Company.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>History</PageTitle>
			   <PageUrl>History.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Locations</PageTitle>
			   <PageUrl>Locations.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Blank Web Part page</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Employment Opportunities</PageTitle>
			   <PageUrl>Employment-Opportunities.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Contact Us</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/Contact-Us</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Social Media</PageTitle>
			   <PageUrl>Social-Media.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Locations</PageTitle>
			   <PageUrl>Locations.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Service Requests</PageTitle>
			   <PageUrl>Service-Requests.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Request a Catalog</PageTitle>
			   <PageUrl>Request-a-Catalog.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Our Company</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/Our-Company</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Forms</PageTitle>
			   <PageUrl>Forms.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Travel</PageTitle>
			   <PageUrl>Travel.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Our Customers</SiteTitle>
			<SiteUrl>http://www.adventureworks.com/Our-Customers</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
		</Site>
	</Sites>
</SubSites>

PowerShell code:

#Region New-SPWebApplicationFromXml
function New-SPWebApplicationFromXml {
<#
.Synopsis
	This advanced PowerShell Function uses XML and PowerShell code to provision
	one or more Web Applications, Site Colections, Subsites and Pages. It can 
	also deploy and activate solutions and features.
.Description
	This advanced script uses New-SPWebApplication, New-SPSite and 
	Enable-SPFeature to configure web applications and site collections using
	specified Site Templates from the included XML. Features, Managed Paths, 
	subsites and even Publishing pages can be created using XML as the input.
.Example
	C:\PS>New-SPWebApplicationFromXml -ConfigXmlInput .\WebApps.xml `
	-SubsiteXmlInput .\Subsites.xml
	
	This example runs with the default parameter values to configure a Web App
	and Site Collection based on the contents of an XML file called 
	WebApps.xml in the current file directory.
.Notes
	Name: New-SPWebApplicationFromXml
	Author: Ryan Dennis
	Last Edit: 9/7/2012
	Keywords: New-SPWebApplication, New-SPSite
.Link
	http://www.sharepointryan.com
 	http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][System.String]$ConfigXmlInput,
[Parameter(Mandatory=$true)][System.String]$SubsiteXmlInput
)

#Region Miscellaneous stuff at the beginning
# If SharePoint Snapin isn't loaded - load it #
if ((Get-PSSnapin Microsoft.SharePoint.PowerShell)-eq $null)
{
	Write-Warning "Adding SharePoint Snapin"
	try	{ Add-PSSnapin Microsoft.SharePoint.PowerShell }
	catch { throw "Unable to add SharePoint Snapin";return }
}
# Start SP Assignment for proper disposal of objects such as SPSite and SPWeb #
Start-SPAssignment -Global

# Read Web Apps from XML file #
[xml]$WebAppXml = Get-Content $($ConfigXmlInput)
if ($WebAppXml -eq $null) { return }

[xml]$SubsiteXml = Get-Content $($SubsiteXmlInput)
if ($SubsiteXml -eq $null) { return }


#EndRegion

#Region Enable Blob Cache function 
function Enable-SPBlobCache { 
param( 
	[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
	[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind] 
	$WebApplication,
	[Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=1)]
	$BlobCacheLocation="C:\BlobCache\14"
) 

process { 
    $WebApp = $WebApplication.Read() 
    # SPWebConfigModification to enable BlobCache 
    $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
    $configMod1.Path = "configuration/SharePoint/BlobCache" 
    $configMod1.Name = "enabled" 
    $configMod1.Sequence = 0 
    $configMod1.Owner = "BlobCacheMod" 
    ## SPWebConfigModificationType.EnsureChildNode -> 0 
    ## SPWebConfigModificationType.EnsureAttribute -> 1 
    ## SPWebConfigModificationType.EnsureSection -> 2 
    $configMod1.Type = 1 
    $configMod1.Value = "true"
		
    ######################################################################
	
	# SPWebConfigModification to enable client-side Blob caching (max-age) 
    $configMod2 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
    $configMod2.Path = "configuration/SharePoint/BlobCache" 
    $configMod2.Name = "max-age" 
    $configMod2.Sequence = 0 
    $configMod2.Owner = "BlobCacheMod" 
    ## SPWebConfigModificationType.EnsureChildNode -> 0 
    ## SPWebConfigModificationType.EnsureAttribute -> 1 
    ## SPWebConfigModificationType.EnsureSection -> 2 
    $configMod2.Type = 1 
    $configMod2.Value = "86400" 
	
	######################################################################
	
	# SPWebConfigModification to change the default location for the Blob Cache files
	$configMod3 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
	$configMod3.Path = "configuration/SharePoint/BlobCache"
	$configMod3.Name = "location"
	$configMod3.Sequence = "0"
	$configMod3.Owner = "BlobCacheMod"
	## SPWebConfigModificationType.EnsureChildNode -> 0 
    ## SPWebConfigModificationType.EnsureAttribute -> 1 
    ## SPWebConfigModificationType.EnsureSection -> 2 
	$configMod3.Type = 1
	$configMod3.Value = $BlobCacheLocation
    # Add mods, update, and apply 
    $WebApp.WebConfigModifications.Add( $configMod1 ) 
    $WebApp.WebConfigModifications.Add( $configMod2 )
	$WebApp.WebConfigModifications.Add( $configMod3 )
    $WebApp.Update() 
    $WebApp.Parent.ApplyWebConfigModifications() 
} 
}

#EndRegion Blob Cache Function

#Region New-BalloonTip Function
function New-BalloonTip {
[CmdletBinding()]
Param(
$TipText='This is the body text.',
$TipTitle='This is the title.',
$TipDuration='10000'
)
[system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
$balloon = New-Object System.Windows.Forms.NotifyIcon
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.Icon = $icon
$balloon.BalloonTipIcon = 'Info'
$balloon.BalloonTipText = $TipText
$balloon.BalloonTipTitle = $TipTitle
$balloon.Visible = $true
$balloon.ShowBalloonTip($TipDuration)
}
#EndRegion New-BalloonTipFunction

#Region Load the Form Dialog
# Load Assemblies for System.Windows.Forms and System.Drawing #
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

# Store the 14 Hive in Variable, create an Img variable for Icon #
$14 = 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14'
$Img = $14+"\Template\Images\Favicon.ico"

# Create the Form Object #
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "SharePoint Web Application Deployment Wizard"
$objForm.Size = New-Object System.Drawing.Size(500,350) 
$objForm.StartPosition = "CenterScreen"
$objForm.Icon = $Img
$objForm.FormBorderStyle = 'FixedDialog'
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {
		$objForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
		$SelectedApps = $null
		[System.String]$Items=$objListBox.SelectedItems;
		[System.Array]$SelectedApps=$Items.Split(' ');
		if($SelectedApps -eq $null){
		throw "You must select at least one web application!"}
		$objForm.Close()
	}
}
)
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$objForm.Close()}})

# OK Button #
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(330,280)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click(
{
	$objForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
	$SelectedApps = $null
	[System.String]$Items=$objListBox.SelectedItems;
	[System.Array]$SelectedApps=$Items.Split(' ');
	if($SelectedApps -eq $null)
	{
	throw "You must select at least one web application!"
	}
	$objForm.Close()
})

# Cancel Button #
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(405,280)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click(
{
	$objForm.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
	$objForm.Close()
}
)

# Listbox Object #
$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(470,20) 
$objListBox.Height = 230
$objListBox.SelectionMode = "MultiExtended"

# Listbox Label #
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(470,20) 
$objLabel.Text = "Please select the web application(s) you wish to deploy:"

$WebApps = $WebAppXml.WebApplications.WebApplication
$WebApps | ForEach-Object {
	[void] $objListBox.Items.Add($_.WebAppConfig.HostHeader)
}

# Add the controls to the Form Object #
$objForm.Controls.Add($objLabel)
$objForm.Controls.add($objListBox)
$objForm.Controls.Add($OKButton)
$objForm.Controls.Add($CancelButton)
$objForm.Controls.Add($SearchButton)

# Display the form on top of all other windows #
$objForm.Topmost = $True

# Add the dialog box to the screen #
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

#EndRegion Form Dialog

#Region Web App ForEach-Object Loop to create everything
# If the dialog result is OK, run the code to create navigation #
if ($objForm.DialogResult -eq "OK" -and $SelectedApps -ne $null)
{
$SelectedApps | ForEach-Object {
	
	#Region Create Web Application
	# Clear Host for a clean, empty shell #
	Clear-Host
	
	[System.String]$CurrentWA = $_
	$wa = $WebAppXml.WebApplications.WebApplication |
	Where-Object {$_.WebAppConfig.HostHeader -eq $CurrentWA}
	
	# Store start time in variable for calculation later #
	$StartTime = Get-Date

	# Read in list of sites from XML file and create variables for use later #
	$Name = $wa.WebAppConfig.Name
	$ApplicationPool = $wa.WebAppConfig.AppPool
	$ApplicationPoolAccount = $wa.WebAppConfig.AppPoolAccount
	$DatabaseServer = $wa.WebAppConfig.DBServer
	$DatabaseName = $wa.WebAppConfig.DBName
	$InetpubPath = $wa.WebAppConfig.InetpubPath
	$HostHeader = $wa.WebAppConfig.HostHeader
	$Port = $wa.WebAppConfig.Port
	$WebsitePath=$InetpubPath+"\"+$HostHeader+$Port
	$Protocol = $wa.WebAppConfig.Protocol
	$Url = $Protocol+"://"+$HostHeader+":"+$Port
	$ManagedPaths = $wa.WebAppConfig.ManagedPaths
	$Solutions = $wa.WebAppConfig.Solutions
	$BlobCacheLocation = $wa.WebAppConfig.BlobCacheLocation
	$AuthType = $wa.WebAppConfig.Authentication.Type
	$AuthMethod = $wa.WebAppConfig.Authentication.AuthMethod
	$AuthProviderName = $wa.WebAppConfig.Authentication.AuthProvider.Name
	$AuthProviderMember = $wa.WebAppConfig.Authentication.AuthProvider.MembershipProviderName
	$AuthProviderRole = $wa.WebAppConfig.Authentication.AuthProvider.RoleProviderName
	
	# Start SP Assignment for object disposal later #
	Start-SPAssignment -Global

	# Change Shell Title #
	$title = $Host.UI.RawUI.WindowTitle
	$Host.UI.RawUI.WindowTitle = "Provisioning Web Application..."

	$ErrorPref = $ErrorActionPreference
	$ErrorActionPreference = "SilentlyContinue"
	$WarningPref = $WarningPreference
	$WarningPreference = "SilentlyContinue"

	# Tell us the Web App is getting created #
	$iMax = 11 #Number of Steps#
	$i = 0 #Number to start on#
	
	$i++ #step 1
	Write-Progress -Activity "Step $i of $iMax : Creating Web Application at Url $($HostHeader)" `
	-PercentComplete ($i/$imax*100) -Status "Creating IIS Website, Content Database and Application Pool"
	# Create a Classic App #
	if($AuthType -eq "Classic"){
		try {
			# Create the Web App #
			New-SPWebApplication -Name $Name -ApplicationPool $ApplicationPool `
			-ApplicationPoolAccount $ApplicationPoolAccount -DatabaseName $DatabaseName `
			-DatabaseServer $DatabaseServer -HostHeader $HostHeader -Port $Port `
			-Path $WebsitePath -Url $HostHeader -WarningAction SilentlyContinue | Out-Null
			New-BalloonTip -TipText "The web application at Url: $($HostHeader) has been created." `
			-TipTitle "Web Application created successfully"
		} 
		catch {	throw "Unable to create the web application";return	}
	}
	# Create a Windows Claims App #
	if($AuthType -eq "Claims"){
		try {
			# Create the auth provider #
			$ap = New-SPAuthenticationProvider
			# Create the Web App #
			New-SPWebApplication -Name $Name -ApplicationPool $ApplicationPool `
			-ApplicationPoolAccount $ApplicationPoolAccount -DatabaseName $DatabaseName `
			-DatabaseServer $DatabaseServer -HostHeader $HostHeader -Port $Port `
			-Path $WebsitePath -Url $HostHeader -WarningAction SilentlyContinue `
			-AuthenticationProvider $ap -AuthenticationMethod $AuthMethod | Out-Null
		} 
		catch {	throw "Unable to create the web application";return	}
	}
	#EndRegion Create Web Application
	
	#Region Creating Managed Paths
	$i++ #Step2
	# Create Managed Paths #
	$wa.WebAppConfig.ManagedPaths.ManagedPath | ForEach-Object {
		$path = $_.path
		$type = $_.type
		if($type -eq "explicit"){
			New-SPManagedPath -Explicit -RelativeURL $path -WebApplication $Url `
			-ErrorAction SilentlyContinue | Out-Null
			New-BalloonTip -TipText "The managed path at path: $($path) has been created." `
			-TipTitle "Managed path created successfully"
		}
		elseif($type -eq "wildcard"){
			New-SPManagedPath -RelativeURL $path -WebApplication $Url `
			-ErrorAction SilentlyContinue | Out-Null
			New-BalloonTip -TipText "The managed path at path: $($path) has been created." `
			-TipTitle "Managed path created successfully"
		}
	}
	#EndRegion Creating Managed Paths
	
	#Region Installing Solutions
	$i++ #Step3
	# Change Shell Title #
	$Host.UI.RawUI.WindowTitle = "Installing Solutions..."
	# Enable solutions #
	try {
		$Solutions.Solution | ForEach-Object {
		$SolutionId = $_
			if($SolutionId.GACDeployment -eq "true"){
			Write-Progress -Activity "Step $i of $iMax : Installing Solutions..." `
			-PercentComplete ($i/$imax*100) -Status "Activating $($SolutionId) solution"
			Install-SPSolution $($SolutionId) -WebApplication $Url `
			-GACDeployment -ErrorVariable +err -Confirm:$false
			Start-Sleep -Seconds 5
			}
			else{
			Write-Progress -Activity "Step $i of $iMax : Installing Solutions..." `
			-PercentComplete ($i/$imax*100) -Status "Activating $($SolutionId) solution"
			Install-SPSolution $($SolutionId) -WebApplication $Url `
			-ErrorVariable +err -Confirm:$false
			Start-Sleep -Seconds 5
			}
		}
	}
	catch { Write-Error "Error enabling solution $($SolutionId)" }
	#EndRegion Installing Solutions	
		
	#Region Create Site Collections
	# Change Shell Title #
	$Host.UI.RawUI.WindowTitle = "Provisioning Site Collections..."
	$i++ #step4
	$siteCollections = $wa.SiteCollections
	# Create Site Collections using ForEach-Object loop #
	try {
	$SiteCollections.SiteCollection | ForEach-Object {
		$NewSiteUrl = $Url+$_.Path
		$sName = $_.Name
		$sOAlias = $_.OwnerAlias
		$sOEmail = $_.OwnerEmail
		$sSOAlias = $_.SecondaryOwnerAlias
		$sSOEmail = $_.SecondaryOwnerEmail
		$sTemplate = $_.WebTemplate
		$subSites = $_.Sites
		$srchUrl = $_.SearchSettings.SearchCenterUrl
		$srchDD = $_.SearchSettings.DropDownMode
		$srchPage = $_.SearchSettings.TargetResultsPage
		# Tell us the Site Collection is getting created #
		Write-Progress -Activity "Step $i of $iMax : Creating Site Collection at Url $($HostHeader)..." `
		-PercentComplete ($i/$imax*100) -Status "Provisioning $($sTemplate) site"
		$SPSite = New-SPSite -Name $sName -OwnerAlias $sOAlias -OwnerEmail $sOEmail `
		-SecondaryOwnerAlias $sSOAlias -SecondaryEmail $sSOEmail `
		-Template $sTemplate -Url $NewSiteUrl | Out-Null
		New-BalloonTip -TipText "The site collection at Url: $($NewSiteUrl) has been created." `
		-TipTitle "Site collection created successfully"
		Start-Sleep -Seconds 5
		
		$_.Features.Feature | ForEach-Object {
		# Enable Features #
		try {
			$FeatureId = $_
			Write-Progress -Activity "Step $i of $iMax : Configuring Site Features..." `
			-PercentComplete ($i/$imax*100) -Status "Activating $($FeatureId) feature"
			Enable-SPFeature -Identity $($FeatureId) -Url $NewSiteUrl -ErrorVariable +err `
			-ErrorAction SilentlyContinue -Confirm:$false
			}
		catch { Write-Error "Error activating feature $err" }
		} #end Feature ForEach
		New-BalloonTip -TipText "The specified features have been enabled." `
		-TipTitle "Features activated successfully"
		Start-Sleep -Seconds 5
		
		
		#Region Search Settings
		.\Set-SPSiteSearchSettings.ps1 -SiteUrl $NewSiteUrl -SearchCenterUrl $srchUrl `
		-SearchDropDownMode $srchDD -SearchResultsUrl $srchPage
		#EndRegion Search Settings
		
	} #End Site Collection ForEach
	} #End Site Collection Try
	catch { throw "Unable to create site collection at path: $NewSiteUrl";return }
	
	#EndRegion Create Site Collections

	#Region Configure Blob Caching
	$i++ #step 7
	Write-Progress -Activity "Step $i of $iMax : Configuring Cache Settings..." `
	-PercentComplete ($i/$imax*100) -Status "Configuring BLOB Caching"
	try {
	Enable-SPBlobCache -WebApplication $Url -BlobCacheLocation $BlobCacheLocation
	}
	catch { Write-Error "Unable to configure BLOB caching" }
	#EndRegion Configure Blob Caching
	
	#Region Run External Scripts
		$location = Get-Location
		$modPath = $PSHOME+"\Modules\PSSAT002\"
		Set-Location $modPath
	
		#Region Create Web App Policy
		$i++ #step8
		Write-Progress -Activity "Step $i of $iMax : Creating Web Application Policy..." `
		-PercentComplete ($i/$imax*100) -Status "Configuring Full Control Policy"
		
		.\Configure-SPWebApplicationUserPolicy.ps1 -WebApplicationUrl $Url
		#EndRegion Create Web App Policy	
	
		#Region Meta Fields
		# Create Meta Fields #
		$i++ #step 9
		Write-Progress -Activity "Step $i of $iMax : Configuring SEO Fields..." `
		-PercentComplete ($i/$imax*100) -Status "Provisioning SEO Meta Fields"
		
		#Write-Host "Configuring SEO fields"
		.\Create-MaventionMetaFields.ps1 -Url $Url
		#EndRegion MetaFields

		Set-Location $location
	#EndRegion Run External Scripts
	
	#Region Create Subsites
	
		# Create subwebs from XML input #
		$i++ #step 11
		Write-Progress -Activity "Step $i of $iMax : Creating Subsites from XML..." `
		-PercentComplete ($i/$imax*100) -Status "Retrieving sites from XML file"
		Start-SPAssignment -Global
		
		$SubsiteXml.SubSites.Sites.Site | ForEach-Object {
		Write-Progress -Activity "Step $i of $iMax : Creating Subsites from XML..." `
		-PercentComplete ($i/$imax*100) -Status "Creating an SPWeb at $($SiteUrl)"
		$SPSiteUrl = $_.SPSiteUrl
		$SiteTitle = [string]$_.SiteTitle
		$SiteUrl = [string]$_.SiteUrl
		$SiteOwner = [string]$_.Owner
		$SiteUser = [string]$_.User
		$WebTemplate = [string]$_.WebTemplate
		$LangId = [string]$_.LangId
		$SiteUrl = $SiteUrl
		# Create the SPWeb #
		if ($_.create -ne "false")
		{
		$Web = New-SPWeb -Url $SiteUrl -Language $LangId -Template $WebTemplate `
		-Name $SiteTitle -ErrorAction Inquire
		}
		else {
		$Web = Get-SPWeb -Identity $SiteUrl -ErrorAction Inquire
		}
		# Get publishing site and web objects
		$site = $Web.Site
		$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

		# Create all Pages using ForEach-Object #
		if ($_.Page -ne $null){
		$_.Page | ForEach-Object {
		
		# Create page content variables from XML file
		$PageTitle = [string]$_.PageTitle
		$PageUrl = [string]$_.PageUrl
		$PageLayout = [string]$_.PageLayout
		#$PageContent = [xml]$_.PageContent
		$PageImage = [string]$_.PageImage
		$layout = $pWeb.GetAvailablePageLayouts() | Where-Object {$_.Title -match $PageLayout}
		
	    # Create blank page using Add method
		Write-Progress -Activity "Step $i of $iMax : Creating Subsites from XML..." `
		-PercentComplete ($i/$imax*100) -Status "Creating $($PageTitle).aspx page in $($SiteTitle) site..."
	    $pages = $pWeb.GetPublishingPages($pWeb)
		$page = $pages.Add($PageUrl, $layout)
		$page.Update()
		
	    # Update the filename to the one specified in the XML, add HTML content to Page Content zone
	    $item = $page.ListItem
		$item["Title"] = $PageTitle;
		$item["Page Content"] = $PageContent;
		$item.Update()
		
	    # Check-in and publish page
	    $item.File.CheckIn("")
	    $item.File.Publish("")
		$file = $item.File
		$pWeb.Update()
		
		} #end Page Foreach
		
		} # End if block 
		Start-Sleep -Seconds 2
		} #End Site ForEach
		New-BalloonTip -TipText "The specified subsites have been created." `
		-TipTitle "Sites created successfully"
	#EndRegion Create Subsites
	
	#Region Clean up and launch site
	# Dispose all objects #
	Stop-SPAssignment -Global

	#Region Start SharePoint Warmup
	.\Start-SharePointWarmup.ps1 -WebApp $Url
	#EndRegion
	
	# Set Error and Warning Preferences back #
	$ErrorActionPreference = $ErrorPref
	$WarningPreference = $WarningPref

	# Open the site in IE #
	$ie = New-Object -ComObject InternetExplorer.Application
	$ie.Navigate($Url)
	$ie.Visible = $true;

	Write-Progress -Activity "Launching the site in Internet Explorer" `
	-Status "Opening IExplore.exe"
	#EndRegion Clean up and launch site
} #end Web App ForEach

#Region Calculate running time and finish up
$EndTime = Get-Date
$TimeSpan = New-TimeSpan $StartTime $EndTime
$Mins = $TimeSpan.Minutes
$Secs = $TimeSpan.Seconds
$Time = $EndTime.ToShortTimeString()
# Tell us it's done #
Write-Host "------------------------------------------------------------------"
Write-Host " Portal Configuration Complete at $($Time)."
Write-Host " Process took $($Mins) minutes and $($Secs) seconds to complete."
Write-Host "------------------------------------------------------------------"
Stop-SPAssignment -Global

New-BalloonTip -TipText "Portal configuration complete at $($Time)." `
-TipTitle "Portal configuration complete!"

#EndRegion Calculate running time and finish up
} #end If DialogResult = OK

#EndRegion Web App ForEach-Object Loop to create everything

#Region Else blocks
# If the Cancel button is clicked, exit #
elseif($objForm.DialogResult -eq "Cancel"){return} #end Else

# If attempting to run without selecting at least one site, output message #
else{Write-Warning "You must select at least one web application!"}
#EndRegion Else blocks

}#endFunction

#EndRegion New-SPWebApplicationFromXml
Advertisements

SharePoint Saturday Cincinnati 2012


This morning I spoke in front of a great crowd at the second annual SharePoint Saturday Cincinnati, also dubbed ScarePoint Saturday Spookinnati!

I chose to re-use my Build your SharePoint Internet presence with PowerShell presentation, as I still think it’s a good presentation that gives a good introductory view into how you can automate site provisioning with PowerShell. I also try to keep it light from a technical standpoint and tend to focus more on the business case around using PowerShell as an automation tool.

Below is a description of the topic itself along with PowerPoint Slides and the PowerShell and XML code that was used in the demo:

Build your SharePoint Internet presence with PowerShell

The topic: Build your SharePoint Internet presence with PowerShell
The story: Everybody knows PowerShell is powerful, it’s in the name! But did you know that PowerShell can read and understand XML? By leveraging XML among other things, complete builds can be automated – making them efficient and predictable.

In this fun, interactive and demo-filled session – I will show you how you can leverage PowerShell to help you build your branded, company website from the ground up using PowerShell and XML. I will also pass along some tips and tricks that will help you become a PowerShell Rockstar!

Here are my slides, and below are the code snippets:

Demo Script – Setup-SPWebAppAndSite.ps1

XML Syntax:

<?xml version="1.0" encoding="utf-8"?>
<SubSites>
	<Sites SPSiteUrl="http://www.vandelay.com">
		<!-- root site -->
		<Site Create="false">
			<SiteTitle>Home</SiteTitle>
			<SiteUrl>http://www.vandelay.com/</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Home</PageTitle>
			   <PageUrl>Home.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Terms of Use</PageTitle>
			   <PageUrl>Terms-of-Use.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Privacy Policy</PageTitle>
			   <PageUrl>Privacy-Policy.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Linking Policy</PageTitle>
			   <PageUrl>Linking-Policy.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>Legal Disclaimer</PageTitle>
			   <PageUrl>Legal-Disclaimer.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
			<Page>
			   <PageTitle>404 - Page Not Found</PageTitle>
			   <PageUrl>Page-Not-Found.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
		   </Page>
		</Site>
		<!-- 2nd level sites -->
		<Site>
			<SiteTitle>Products</SiteTitle>
			<SiteUrl>http://www.vandelay.com/Products</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Awesome Widget</PageTitle>
			   <PageUrl>Awesome-Widget.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>The Spectacular Thingamajig</PageTitle>
			   <PageUrl>The-Spectacular-Thingamajig.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Store</PageTitle>
			   <PageUrl>Store.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Solutions</SiteTitle>
			<SiteUrl>http://www.vandelay.com/Solutions</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Customer Engagement</PageTitle>
			   <PageUrl>Customer-Engagement.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Custom Solutions</PageTitle>
			   <PageUrl>Custom-Solutions.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Shipping and Returns</SiteTitle>
			<SiteUrl>http://www.vandelay.com/Shipping-and-Returns</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Shipping Policies</PageTitle>
			   <PageUrl>Shipping-Policies.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Return Policies</PageTitle>
			   <PageUrl>Return-Policies.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>About Us</SiteTitle>
			<SiteUrl>http://www.vandelay.com/About-Us</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Company</PageTitle>
			   <PageUrl>Company.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>History</PageTitle>
			   <PageUrl>History.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Locations</PageTitle>
			   <PageUrl>Locations.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Blank Web Part page</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Employment Opportunities</PageTitle>
			   <PageUrl>Employment-Opportunities.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Contact Us</SiteTitle>
			<SiteUrl>http://www.vandelay.com/Contact-Us</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Social Media</PageTitle>
			   <PageUrl>Social-Media.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Locations</PageTitle>
			   <PageUrl>Locations.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Service Requests</PageTitle>
			   <PageUrl>Service-Requests.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Request a Catalog</PageTitle>
			   <PageUrl>Request-a-Catalog.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>What We Do</SiteTitle>
			<SiteUrl>http://www.vandelay.com/What-We-Do</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Engagement</PageTitle>
			   <PageUrl>Engagement.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Ambassadors</PageTitle>
			   <PageUrl>Ambassadors.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Our Company</SiteTitle>
			<SiteUrl>http://www.vandelay.com/Our-Company</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
			<Page>
			   <PageTitle>Forms</PageTitle>
			   <PageUrl>Forms.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
			<Page>
			   <PageTitle>Travel</PageTitle>
			   <PageUrl>Travel.aspx</PageUrl>
			   <PageContent></PageContent>
			   <PageLayout>Body Only</PageLayout>
			</Page>
		</Site>
		<Site>
			<SiteTitle>Our Customers</SiteTitle>
			<SiteUrl>http://www.vandelay.com/Our-Customers</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
		</Site>
		<Site>
			<SiteTitle>News</SiteTitle>
			<SiteUrl>http://www.vandelay.com/News</SiteUrl>
			<WebTemplate>CMSPUBLISHING#0</WebTemplate>
			<LangId>1033</LangId>
		</Site>
	</Sites>
</SubSites>

PowerShell code:

#Region New-SPWebApplicationFromXml
function New-SPWebApplicationFromXml {
<#
.Synopsis
	This advanced PowerShell Function uses XML and PowerShell code to provision
	one or more Web Applications, Site Colections, Subsites and Pages. It can
	also deploy and activate solutions and features.
.Description
	This advanced script uses New-SPWebApplication, New-SPSite and
	Enable-SPFeature to configure web applications and site collections using
	specified Site Templates from the included XML. Features, Managed Paths,
	subsites and even Publishing pages can be created using XML as the input.
.Example
	C:\PS>New-SPWebApplicationFromXml -ConfigXmlInput .\WebApps.xml `
	-SubsiteXmlInput .\Subsites.xml

	This example runs with the default parameter values to configure a Web App
	and Site Collection based on the contents of an XML file called
	WebApps.xml in the current file directory.
.Notes
	Name: New-SPWebApplicationFromXml
	Author: Ryan Dennis
	Last Edit: 9/7/2012
	Keywords: New-SPWebApplication, New-SPSite
.Link
	http://www.sharepointryan.com
 	http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][System.String]$ConfigXmlInput,
[Parameter(Mandatory=$true)][System.String]$SubsiteXmlInput
)

#Region Miscellaneous stuff at the beginning
# If SharePoint Snapin isn't loaded - load it #
if ((Get-PSSnapin Microsoft.SharePoint.PowerShell)-eq $null)
{
	Write-Warning "Adding SharePoint Snapin"
	try	{ Add-PSSnapin Microsoft.SharePoint.PowerShell }
	catch { throw "Unable to add SharePoint Snapin";return }
}
# Start SP Assignment for proper disposal of objects such as SPSite and SPWeb #
Start-SPAssignment -Global

# Read Web Apps from XML file #
[xml]$WebAppXml = Get-Content $($ConfigXmlInput)
if ($WebAppXml -eq $null) { return }

[xml]$SubsiteXml = Get-Content $($SubsiteXmlInput)
if ($SubsiteXml -eq $null) { return }

#EndRegion

#Region Enable Blob Cache function
function Enable-SPBlobCache {
param(
	[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
	[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]
	$WebApplication,
	[Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=1)]
	$BlobCacheLocation="C:\BlobCache\14"
)

process {
    $WebApp = $WebApplication.Read()
    # SPWebConfigModification to enable BlobCache
    $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
    $configMod1.Path = "configuration/SharePoint/BlobCache"
    $configMod1.Name = "enabled"
    $configMod1.Sequence = 0
    $configMod1.Owner = "BlobCacheMod"
    ## SPWebConfigModificationType.EnsureChildNode -> 0
    ## SPWebConfigModificationType.EnsureAttribute -> 1
    ## SPWebConfigModificationType.EnsureSection -> 2
    $configMod1.Type = 1
    $configMod1.Value = "true"

    ######################################################################

	# SPWebConfigModification to enable client-side Blob caching (max-age)
    $configMod2 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
    $configMod2.Path = "configuration/SharePoint/BlobCache"
    $configMod2.Name = "max-age"
    $configMod2.Sequence = 0
    $configMod2.Owner = "BlobCacheMod"
    ## SPWebConfigModificationType.EnsureChildNode -> 0
    ## SPWebConfigModificationType.EnsureAttribute -> 1
    ## SPWebConfigModificationType.EnsureSection -> 2
    $configMod2.Type = 1
    $configMod2.Value = "86400"

	######################################################################

	# SPWebConfigModification to change the default location for the Blob Cache files
	$configMod3 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
	$configMod3.Path = "configuration/SharePoint/BlobCache"
	$configMod3.Name = "location"
	$configMod3.Sequence = "0"
	$configMod3.Owner = "BlobCacheMod"
	## SPWebConfigModificationType.EnsureChildNode -> 0
    ## SPWebConfigModificationType.EnsureAttribute -> 1
    ## SPWebConfigModificationType.EnsureSection -> 2
	$configMod3.Type = 1
	$configMod3.Value = $BlobCacheLocation
    # Add mods, update, and apply
    $WebApp.WebConfigModifications.Add( $configMod1 )
    $WebApp.WebConfigModifications.Add( $configMod2 )
	$WebApp.WebConfigModifications.Add( $configMod3 )
    $WebApp.Update()
    $WebApp.Parent.ApplyWebConfigModifications()
}
}

#EndRegion Blob Cache Function

#Region Load the Form Dialog
# Load Assemblies for System.Windows.Forms and System.Drawing #
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Store the 14 Hive in Variable, create an Img variable for Icon #
$14 = 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14'
$Img = $14+"\Template\Images\Favicon.ico"

# Create the Form Object #
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "SharePoint Web Application Deployment Wizard"
$objForm.Size = New-Object System.Drawing.Size(500,350)
$objForm.StartPosition = "CenterScreen"
$objForm.Icon = $Img
$objForm.FormBorderStyle = 'FixedDialog'
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {
		$objForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
		$SelectedApps = $null
		[System.String]$Items=$objListBox.SelectedItems;
		[System.Array]$SelectedApps=$Items.Split(' ');
		if($SelectedApps -eq $null){
		throw "You must select at least one web application!"}
		$objForm.Close()
	}
}
)
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape"){$objForm.Close()}})

# OK Button #
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(330,280)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click(
{
	$objForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
	$SelectedApps = $null
	[System.String]$Items=$objListBox.SelectedItems;
	[System.Array]$SelectedApps=$Items.Split(' ');
	if($SelectedApps -eq $null)
	{
	throw "You must select at least one web application!"
	}
	$objForm.Close()
})

# Cancel Button #
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(405,280)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click(
{
	$objForm.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
	$objForm.Close()
}
)

# Listbox Object #
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(470,20)
$objListBox.Height = 230
$objListBox.SelectionMode = "MultiExtended"

# Listbox Label #
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(470,20)
$objLabel.Text = "Please select the web application(s) you wish to deploy:"

$WebApps = $WebAppXml.WebApplications.WebApplication
$WebApps | ForEach-Object {
	[void] $objListBox.Items.Add($_.WebAppConfig.HostHeader)
}

# Add the controls to the Form Object #
$objForm.Controls.Add($objLabel)
$objForm.Controls.add($objListBox)
$objForm.Controls.Add($OKButton)
$objForm.Controls.Add($CancelButton)
$objForm.Controls.Add($SearchButton)

# Display the form on top of all other windows #
$objForm.Topmost = $True

# Add the dialog box to the screen #
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

#EndRegion Form Dialog

#Region Web App ForEach-Object Loop to create everything
# If the dialog result is OK, run the code to create navigation #
if ($objForm.DialogResult -eq "OK" -and $SelectedApps -ne $null)
{
$SelectedApps | ForEach-Object {

	#Region Create Web Application
	# Clear Host for a clean, empty shell #
	Clear-Host

	[System.String]$CurrentWA = $_
	$wa = $WebAppXml.WebApplications.WebApplication |
	Where-Object {$_.WebAppConfig.HostHeader -eq $CurrentWA}

	# Store start time in variable for calculation later #
	$StartTime = Get-Date

	# Read in list of sites from XML file and create variables for use later #
	$Name = $wa.WebAppConfig.Name
	$ApplicationPool = $wa.WebAppConfig.AppPool
	$ApplicationPoolAccount = $wa.WebAppConfig.AppPoolAccount
	$DatabaseServer = $wa.WebAppConfig.DBServer
	$DatabaseName = $wa.WebAppConfig.DBName
	$InetpubPath = $wa.WebAppConfig.InetpubPath
	$HostHeader = $wa.WebAppConfig.HostHeader
	$Port = $wa.WebAppConfig.Port
	$WebsitePath=$InetpubPath+"\"+$HostHeader+$Port
	$Protocol = $wa.WebAppConfig.Protocol
	$Url = $Protocol+"://"+$HostHeader+":"+$Port
	$ManagedPaths = $wa.WebAppConfig.ManagedPaths
	$Solutions = $wa.WebAppConfig.Solutions
	$BlobCacheLocation = $wa.WebAppConfig.BlobCacheLocation
	$AuthType = $wa.WebAppConfig.Authentication.Type
	$AuthMethod = $wa.WebAppConfig.Authentication.AuthMethod
	$AuthProviderName = $wa.WebAppConfig.Authentication.AuthProvider.Name
	$AuthProviderMember = $wa.WebAppConfig.Authentication.AuthProvider.MembershipProviderName
	$AuthProviderRole = $wa.WebAppConfig.Authentication.AuthProvider.RoleProviderName

	# Start SP Assignment for object disposal later #
	Start-SPAssignment -Global

	# Change Shell Title #
	$title = $Host.UI.RawUI.WindowTitle
	$Host.UI.RawUI.WindowTitle = "Provisioning Web Application..."

	$ErrorPref = $ErrorActionPreference
	$ErrorActionPreference = "SilentlyContinue"
	$WarningPref = $WarningPreference
	$WarningPreference = "SilentlyContinue"

	# Tell us the Web App is getting created #
	$iMax = 11 #Number of Steps#
	$i = 0 #Number to start on#

	$i++ #step 1
	Write-Progress -Activity "Step $i of $iMax : Creating Web Application at Url $($HostHeader)" `
	-PercentComplete ($i/$imax*100) -Status "Creating IIS Website, Content Database and Application Pool"
	# Create a Classic App #
	if($AuthType -eq "Classic"){
		try {
			# Create the Web App #
			New-SPWebApplication -Name $Name -ApplicationPool $ApplicationPool `
			-ApplicationPoolAccount $ApplicationPoolAccount -DatabaseName $DatabaseName `
			-DatabaseServer $DatabaseServer -HostHeader $HostHeader -Port $Port `
			-Path $WebsitePath -Url $HostHeader -WarningAction SilentlyContinue | Out-Null
		}
		catch {	throw "Unable to create the web application";return	}
	}
	# Create a Windows Claims App #
	if($AuthType -eq "Claims"){
		try {
			# Create the auth provider #
			$ap = New-SPAuthenticationProvider
			# Create the Web App #
			New-SPWebApplication -Name $Name -ApplicationPool $ApplicationPool `
			-ApplicationPoolAccount $ApplicationPoolAccount -DatabaseName $DatabaseName `
			-DatabaseServer $DatabaseServer -HostHeader $HostHeader -Port $Port `
			-Path $WebsitePath -Url $HostHeader -WarningAction SilentlyContinue `
			-AuthenticationProvider $ap -AuthenticationMethod $AuthMethod | Out-Null
		}
		catch {	throw "Unable to create the web application";return	}
	}
	#EndRegion Create Web Application

	#Region Creating Managed Paths
	$i++ #Step2
	# Create Managed Paths #
	$wa.WebAppConfig.ManagedPaths.ManagedPath | ForEach-Object {
		$path = $_.path
		$type = $_.type
		if($type -eq "explicit"){
			New-SPManagedPath -Explicit -RelativeURL $path -WebApplication $Url `
			-ErrorAction SilentlyContinue | Out-Null
		}
		elseif($type -eq "wildcard"){
			New-SPManagedPath -RelativeURL $path -WebApplication $Url `
			-ErrorAction SilentlyContinue | Out-Null
		}
	}
	#EndRegion Creating Managed Paths

	#Region Installing Solutions
	$i++ #Step3
	# Change Shell Title #
	$Host.UI.RawUI.WindowTitle = "Installing Solutions..."
	# Enable solutions #
	try {
		$Solutions.Solution | ForEach-Object {
		$SolutionId = $_
			if($SolutionId.GACDeployment -eq "true"){
			Write-Progress -Activity "Step $i of $iMax : Installing Solutions..." `
			-PercentComplete ($i/$imax*100) -Status "Activating $($SolutionId) solution"
			Install-SPSolution $($SolutionId) -WebApplication $Url `
			-GACDeployment -ErrorVariable +err -Confirm:$false
			Start-Sleep -Seconds 5
			}
			else{
			Write-Progress -Activity "Step $i of $iMax : Installing Solutions..." `
			-PercentComplete ($i/$imax*100) -Status "Activating $($SolutionId) solution"
			Install-SPSolution $($SolutionId) -WebApplication $Url `
			-ErrorVariable +err -Confirm:$false
			Start-Sleep -Seconds 5
			}
		}
	}
	catch { Write-Error "Error enabling solution $($SolutionId)" }
	#EndRegion Installing Solutions

	#Region Create Site Collections
	# Change Shell Title #
	$Host.UI.RawUI.WindowTitle = "Provisioning Site Collections..."
	$i++ #step4
	$siteCollections = $wa.SiteCollections
	# Create Site Collections using ForEach-Object loop #
	try {
	$SiteCollections.SiteCollection | ForEach-Object {
		$NewSiteUrl = $Url+$_.Path
		$sName = $_.Name
		$sOAlias = $_.OwnerAlias
		$sOEmail = $_.OwnerEmail
		$sSOAlias = $_.SecondaryOwnerAlias
		$sSOEmail = $_.SecondaryOwnerEmail
		$sTemplate = $_.WebTemplate
		$subSites = $_.Sites
		$srchUrl = $_.SearchSettings.SearchCenterUrl
		$srchDD = $_.SearchSettings.DropDownMode
		$srchPage = $_.SearchSettings.TargetResultsPage
		# Tell us the Site Collection is getting created #
		Write-Progress -Activity "Step $i of $iMax : Creating Site Collection at Url $($HostHeader)..." `
		-PercentComplete ($i/$imax*100) -Status "Provisioning $($sTemplate) site"
		$SPSite = New-SPSite -Name $sName -OwnerAlias $sOAlias -OwnerEmail $sOEmail `
		-SecondaryOwnerAlias $sSOAlias -SecondaryEmail $sSOEmail `
		-Template $sTemplate -Url $NewSiteUrl | Out-Null
		Start-Sleep -Seconds 5

		$_.Features.Feature | ForEach-Object {
		# Enable Features #
		try {
			$FeatureId = $_
			Write-Progress -Activity "Step $i of $iMax : Configuring Site Features..." `
			-PercentComplete ($i/$imax*100) -Status "Activating $($FeatureId) feature"
			Enable-SPFeature -Identity $($FeatureId) -Url $NewSiteUrl -ErrorVariable +err `
			-ErrorAction SilentlyContinue -Confirm:$false
			}
		catch { Write-Error "Error activating feature $err" }
		} #end Feature ForEach

		Start-Sleep -Seconds 5

		#Region Search Settings
		.\Set-SPSiteSearchSettings.ps1 -SiteUrl $NewSiteUrl -SearchCenterUrl $srchUrl `
		-SearchDropDownMode $srchDD -SearchResultsUrl $srchPage
		#EndRegion Search Settings

	} #End Site Collection ForEach
	} #End Site Collection Try
	catch { throw "Unable to create site collection at path: $NewSiteUrl";return }

	#EndRegion Create Site Collections

	#Region Configure Blob Caching
	$i++ #step 7
	Write-Progress -Activity "Step $i of $iMax : Configuring Cache Settings..." `
	-PercentComplete ($i/$imax*100) -Status "Configuring BLOB Caching"
	try {
	Enable-SPBlobCache -WebApplication $Url -BlobCacheLocation $BlobCacheLocation
	}
	catch { Write-Error "Unable to configure BLOB caching" }
	#EndRegion Configure Blob Caching

	#Region Run External Scripts
		$location = Get-Location
		$modPath = $PSHOME+"\Modules\PSSAT002\"
		Set-Location $modPath

		#Region Create Web App Policy
		$i++ #step8
		Write-Progress -Activity "Step $i of $iMax : Creating Web Application Policy..." `
		-PercentComplete ($i/$imax*100) -Status "Configuring Full Control Policy"

		.\Configure-SPWebApplicationUserPolicy.ps1 -WebApplicationUrl $Url
		#EndRegion Create Web App Policy

		#Region Meta Fields
		# Create Meta Fields #
		$i++ #step 9
		Write-Progress -Activity "Step $i of $iMax : Configuring SEO Fields..." `
		-PercentComplete ($i/$imax*100) -Status "Provisioning SEO Meta Fields"

		#Write-Host "Configuring SEO fields"
		.\Create-MaventionMetaFields.ps1 -Url $Url
		#EndRegion MetaFields

		Set-Location $location
	#EndRegion Run External Scripts

	#Region Create Subsites

		# Create subwebs from XML input #
		$i++ #step 11
		Write-Progress -Activity "Step $i of $iMax : Creating Subsites from XML..." `
		-PercentComplete ($i/$imax*100) -Status "Retrieving sites from XML file"
		Start-SPAssignment -Global

		$SubsiteXml.SubSites.Sites.Site | ForEach-Object {
		Write-Progress -Activity "Step $i of $iMax : Creating Subsites from XML..." `
		-PercentComplete ($i/$imax*100) -Status "Creating an SPWeb at $($SiteUrl)"
		$SPSiteUrl = $_.SPSiteUrl
		$SiteTitle = [string]$_.SiteTitle
		$SiteUrl = [string]$_.SiteUrl
		$SiteOwner = [string]$_.Owner
		$SiteUser = [string]$_.User
		$WebTemplate = [string]$_.WebTemplate
		$LangId = [string]$_.LangId
		$SiteUrl = $SiteUrl
		# Create the SPWeb #
		if ($_.create -ne "false")
		{
		$Web = New-SPWeb -Url $SiteUrl -Language $LangId -Template $WebTemplate `
		-Name $SiteTitle -ErrorAction Inquire
		}
		else {
		$Web = Get-SPWeb -Identity $SiteUrl -ErrorAction Inquire
		}
		# Get publishing site and web objects
		$site = $Web.Site
		$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

		# Create all Pages using ForEach-Object #
		if ($_.Page -ne $null){
		$_.Page | ForEach-Object {

		# Create page content variables from XML file
		$PageTitle = [string]$_.PageTitle
		$PageUrl = [string]$_.PageUrl
		$PageLayout = [string]$_.PageLayout
		#$PageContent = [xml]$_.PageContent
		$PageImage = [string]$_.PageImage
		$layout = $pWeb.GetAvailablePageLayouts() | Where-Object {$_.Title -match $PageLayout}

	    # Create blank page using Add method
		Write-Progress -Activity "Step $i of $iMax : Creating Subsites from XML..." `
		-PercentComplete ($i/$imax*100) -Status "Creating $($PageTitle).aspx page in $($SiteTitle) site..."
	    $pages = $pWeb.GetPublishingPages($pWeb)
		$page = $pages.Add($PageUrl, $layout)
		$page.Update()

	    # Update the filename to the one specified in the XML, add HTML content to Page Content zone
	    $item = $page.ListItem
		$item["Title"] = $PageTitle;
		$item["Page Content"] = $PageContent;
		$item.Update()

	    # Check-in and publish page
	    $item.File.CheckIn("")
	    $item.File.Publish("")
		$file = $item.File
		$pWeb.Update()

		} #end Page Foreach

		} # End if block
		Start-Sleep -Seconds 2
		} #End Site ForEach

	#EndRegion Create Subsites

	#Region Clean up and launch site
	# Dispose all objects #
	Stop-SPAssignment -Global

	#Region Start SharePoint Warmup
	.\Start-SharePointWarmup.ps1 -WebApp $Url
	#EndRegion

	# Set Error and Warning Preferences back #
	$ErrorActionPreference = $ErrorPref
	$WarningPreference = $WarningPref

	# Open the site in IE #
	$ie = New-Object -ComObject InternetExplorer.Application
	$ie.Navigate($Url)
	$ie.Visible = $true;

	Write-Progress -Activity "Launching the site in Internet Explorer" `
	-Status "Opening IExplore.exe"
	#EndRegion Clean up and launch site
} #end Web App ForEach

#Region Calculate running time and finish up
$EndTime = Get-Date
$TimeSpan = New-TimeSpan $StartTime $EndTime
$Mins = $TimeSpan.Minutes
$Secs = $TimeSpan.Seconds
$Time = $EndTime.ToShortTimeString()
# Tell us it's done #
Write-Host "------------------------------------------------------------------"
Write-Host " Portal Configuration Complete at $($Time)."
Write-Host " Process took $($Mins) minutes and $($Secs) seconds to complete."
Write-Host "------------------------------------------------------------------"
Stop-SPAssignment -Global
#EndRegion Calculate running time and finish up
} #end If DialogResult = OK

#EndRegion Web App ForEach-Object Loop to create everything

#Region Else blocks
# If the Cancel button is clicked, exit #
elseif($objForm.DialogResult -eq "Cancel"){return} #end Else

# If attempting to run without selecting at least one site, output message #
else{Write-Warning "You must select at least one web application!"}
#EndRegion Else blocks

}#endFunction

#EndRegion New-SPWebApplicationFromXml

SharePoint Saturday Cincinnati 2011


This past weekend I broke new ground in my career – I completed my first public speaking engagement! While I have spoken on various topics in front of my SharePoint Team at ICC – this was my first public speaking engagement and overall I think it went very well.

The topic: The Power is in the Shell, use it wisely!
The story: What is PowerShell – how does it fit into the SharePoint 2010 puzzle? How can we take commands run in the Shell and easily convert them to scripts or functions? How can we use the Get-Content cmdlet and some XML to automate content loading or migrations? What are some best practices around PowerShell as it relates to SharePoint 2010?

This was the first SharePoint Saturday in Cincinnati, and I was very proud to be a part of a great event.

My session went pretty flawlessly, as I lucked out and had zero issues with the presentation or the three demos. I had a small group of attendees, but all of them were very engaged in dialog throughout. For those of you who attended, thank you for being a part of it – and if you have any feedback or questions feel free to contact me either on Twitter or via e-mail (rdennis at iccohio dot com).

I promised to upload my slides and demo PowerShell code, so the slides are on Slideshare and the XML and PowerShell code is below:

First demo function – Set-SPWebTitle

function Set-SPWebTitle {
Param(
[string]$SiteUrl,
[string]$SiteTitle
)
$site = Get-SPSite $SiteUrl
$web = $site.RootWeb                                                                                                   
$web.Title = $SiteTitle
Write-Host "Changing title to:" $SiteTitle
$web.Update()                                                                                                          
$web.Dispose()                                                                                                        
$site.Dispose()           
Write-Host "Finished!"
}

Second demo function – New-SPWebFromXml
XML Syntax:

<?xml version="1.0" encoding="utf-8"?>
<Sites>
  	<Site>
    		<SiteTitle>Ryan Dennis</SiteTitle>
	    	<SiteUrl>http://sps.adventureworks.com/rdennis</SiteUrl>
	    	<Page>
			<PageTitle>Home</PageTitle>
			<PageUrl>Home.aspx</PageUrl>
			<PageContent>HTML Content</PageContent>
			<PageLayout>Body Only</PageLayout>
		</Page>
  	</Site>
</Sites>

PowerShell code:

function New-SPWebFromXml {
# Xml Input parameter - accepts string to filename
[CmdletBinding()]
	Param(
    [Parameter(Mandatory=$true)]
	[string]$XmlInput
	)

# Read in list of sites from XML file
[xml]$SitesXml = Get-Content $($XmlInput)
if ($SitesXml -eq $null) {return}
Start-SPAssignment -Global #Stores all objects in the global store for proper disposal
$StartTime = Get-Date #Grab the date & time for further calculation later

# Upload speaker images to the PublishingImages library
Write-Host "Uploading speaker images to the PublishingImages library..."
Add-SPFilesToLibrary -WebUrl http://sps.adventureworks.com -LibraryName PublishingImages -FileExtension "*.jpg" -FolderLocation "C:\SPSDemo\Speakers"

	# Loop through each site node to extract data
	$SitesXml.Sites.Site | ForEach-Object {
	$SiteTitle = [string]$_.SiteTitle
	$SiteUrl = [string]$_.SiteUrl
	$WebTemplate = "CMSPUBLISHING#0"
	$LangId = "1033"
	
	# Get publishing site and web objects
	$site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
	$psite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($site)
	$web = $site.OpenWeb()
	$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
	
	# Create the SPWeb
	Write-Host "Creating an SPWeb at $($SiteUrl)"
	New-SPWeb -Url $SiteUrl	-Language $LangId -Template $WebTemplate -Name $SiteTitle
	
	# Create page content variables from XML file
	$PageTitle = [string]$_.Page.PageTitle
	$PageUrl = [string]$_.Page.PageUrl
	$PageLayout = [string]$_.Page.PageLayout
	$PageContent = [string]$_.Page.PageContent
	$PageImage = [string]$_.Page.PageImage
	$layout = $pWeb.GetAvailablePageLayouts() | Where-Object {$_.Title -match $PageLayout}
	
    # Create blank page using Add method
	Write-Host "Creating $($PageTitle).aspx page"
    $pages = $pWeb.GetPublishingPages($pWeb)
	$page = $pages.Add($PageUrl, $layout)
	$page.Update()
	
    # Update the filename to the one specified in the XML, add HTML content to Page Content zone
    $item = $page.ListItem
	$item["Title"] = $PageTitle;
	$item["Page Content"] = $PageContent;
	$item.Update()
	
    # Check-in and publish page
    $item.File.CheckIn("")
    $item.File.Publish("")
	$file = $item.File
	$pWeb.DefaultPage = $file
	$pWeb.Update()
	
	# Get and delete original default.aspx page
	Write-Host "Deleting default.aspx page"
	$list = $web.lists["Pages"]
	$oldPage = $list.items | Where-Object {$_.Name -match "default"}
	$oldPage.Delete();
	$web.Update()
	} #End ForEach-Object loop
	
$EndTime = Get-Date #Grab the ending date & time to get the timespan
$TimeSpan = New-TimeSpan $StartTime $EndTime #Calculate total time taken
Write-Host "$($SitesXml.Sites.Site.Count) sites created in $timespan"
Stop-SPAssignment -Global #Stop assignment, safely disposing all site and web objects
} #End Function