Quick and Dirty Scripting

A blog that focuses on automating system administration tasks for Linux, Windows, and VMware ESX

Wednesday, April 9, 2008

 

Powershell Script to Create Sharepoint Web Application

Microsoft Sharepoint 2007 has a very nice web-based UI to create Web Applications, but there is one catch. The UI creates the backend database on the fly. In a lot of companies, this is the role of a DBA, and only they are allowed to create a new database. In a way this makes sense due to the fact that the DBAs must maintain the intergity of the database server. Microsoft has provided a switch within the stsadm command to create a new Web Application with an existing database, but I thought I could come up with a way to do it using Powersell.

What I wanted to do was to supply a script with an XML configuration that would create 1-N Web Applications with 1-N Sites. The XML looks like the following:
<Sharepoint>
<WebApplication name="Test Site #1" hostheader="http://portal.example.net">
<AppPoolName>WebApp-Portal</AppPoolName>
<AppPoolUser>Domain\myServiceAccount</AppPoolUser>
<AppPoolPass>test1235</AppPoolPass>
<Port>80</Port>
<DatabaseServer>DBS-SERVER-NAME\MOSS</DatabaseServer>
<DatabaseName>WSS_Content_Potal</DatabaseName>
<RootDirectory>d:\inetpub\wwwroot\wss\portal</RootDirectory>
<Sites>
<Site Path="/">
<Title>Root Site</Title>
<Description>Root Site</Description>
<Type>STS#1</Type>
<AdminAccount>DOMAIN\Administrator</AdminAccount>
<AdminName>Administrator1</AdminName>
<AdminEmail>root@jexample.net</AdminEmail>
</Site>
</Sites>
</WebApplication>
<WebApplication></WebApplication>
</Sharepoint>


The first thing that needs to be done is load the Sharepoint .NET assembly and attach to the local farm


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$farm = [microsoft.sharepoint.administration.spfarm]::local


Then load the XML configuration file and loop through all Web Applications.


function main()
{

$moss = "Sharepoint"
$cfg = [xml](gc $cfgFile)

if( $? -eq $false ) {
Write-Host "Could not cleanly parase XML file. Exiting . . ."
return $false
}

Write-Host "Found Sharepoint Farm on Local Host . . "

Write-Host "Using $cfgFile file . . ."

$cfg.$moss.WebApplication | % {

createWebApp( $_ )

}
}

main


Surprisingly to create new Web Application, all you need to do is create a Powershell object of type SPWebApplicationBuilder, assign the proper variables, create and then provision. The command can take up to 10 minutes to complete so be patient.


function createWebApp( [object] $cfg )
{
$webAppBuilder = $nul
$webAppBuilder = new-object _
microsoft.sharepoint.administration.SPWebApplicationBuilder($farm)

$secureString = ConvertTo-SecureString $cfg.AppPoolPass -asPlainText -force

$webAppBuilder.Port = $cfg.port
$webAppBuilder.ApplicationPoolId = $cfg.AppPoolName
$webAppBuilder.ApplicationPoolUsername = $cfg.AppPoolUser
$webAppBuilder.ApplicationPoolPassword = $secureString

$webAppBuilder.HostHeader = $cfg.hostheader
$webAppBuilder.ServerComment = $cfg.name
$webAppBuilder.DatabaseServer = $cfg.DatabaseServer
$webAppBuilder.DatabaseName = $cfg.DatabaseName
$webAppBuilder.RootDirectory = $cfg.RootDirectory

if( $cfg.AllowAnonymous.ToString().ToLower() -eq "true" ) {
$webAppBuilder.AllowAnonymousAccess = $true
}

Write-Host "Will now Provision this Web Application."
Write-Host "This may take up to 10 minutes. . ."
$webApp = $webAppBuilder.Create()
$webApp.Provision()


Finally, loop through all Sites that are listed in the XML and add them to the Web Application.

$cfg.Sites.Site | % {
$title = $_.SiteTitle.ToString()
$path = $_.Path.ToString()

rite-Host "Will now Provision Site - $title ($path). "
Write-Host "This may take up to 10 minutes. . ."

$webApp.Sites.Add( $_.Path,
$_.Title,
$_.Description,
1033,
$_.Type,
$_.AdminAccount,
$_.AdminName,
$_.AdminEmail)
}

See easy as 1, 2, 3 . . .

Archives

October 2007   November 2007   December 2007   January 2008   February 2008   April 2008   July 2008  

View Brian Denicola's profile on LinkedIn

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]