SharePoint Tech Blog

08/07/2009

Create SharePoint Web Application and Root Site Collection using Powershell

Filed under: SharePoint — Tags: , , — kctnpblog @ 10:58 am

PowerShell is a really powerful scripting tool and I’m quite amazed at how simple it is to automate several tasks in SharePoint using PowerShell. To start this blog off, here’s a simple script to create a web application using PowerShell:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null
# Return the Default TimeZone for Sydney
function get-defaulttimezoneid
{
[Microsoft.SharePoint.SPregionalSettings]::Globaltimezones | where-object { $_.Description -like "*sydney*" }
}
$port=$args[0]
$webAppBuilder=new-object Microsoft.SharePoint.Administration.SPWebApplicationBuilder( [Microsoft.SharePoint.Administration.SPFarm]::Local);
$webAppBuilder.Port=$port;
$webAppBuilder.RootDirectory="C:\Inetpub\wwwroot\wss\VirtualDirectories\$port";
$webAppBuilder.ServerComment="SharePoint - $port";
$webAppBuilder.ApplicationPoolID="SharePoint - $port";
$webAppBuilder.CreateNewDatabase=$true;
$webAppBuilder.DatabaseName="WSS_Content_$port";
$webAppBuilder.UseNTLMExclusively=$true;
$webAppBuilder.AllowAnonymousAccess=$false;
$webAppBuilder.UseSecureSocketsLayer=$false;
$webApp=$webAppBuilder.Create();
$webApp.ProvisionGlobally();
$timezone=get-defaulttimezoneid
$webApp.DefaultTimeZone=$timezone.ID;
$webApp.Update()
$webApp.ProvisionGlobally()
$webApp.ApplicationPool.CurrentIdentityType = [Microsoft.SharePoint.Administration.IdentityType]::SpecificUser;
$webApp.ApplicationPool.Username = "mossdemo\mossadmin";
$webApp.ApplicationPool.Password = "password";
$webApp.ApplicationPool.Update();
$webApp.ApplicationPool.Provision();
$title="Test Intranet" $description="Test Intranet"
$locale=1033
$sptemplate="SPSPORTAL#0"
$ownerlogin="mossdemo\mossadmin"
$owneremail="mossadmin@mossdemo.local"
$ownername="MOSS Administrator"
$webApp.Sites.Add("/", $title,$description,$locale,$sptemplate,$ownerlogin,$ownername,$owneremail)

All that’s needed is the port number to be passed to the code from the command line. If you’ve installed PowerShell then command prompt to the folder where this scipt file is and type in:
powershell .\<script file name>.ps1 8080

to create a web application on port 8080

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: