martes, 3 de diciembre de 2013

Day 4: Scripts - A useful script to create site collections (Part II).

Last entry we talk about a script to create site collections, but it has one problem: the script can´t create one site collection at root level... This happends because the part of the code that creates the site collection is static: the URL to create the site allways includes "/sites/" sufix. So, if we try to create one site collection at root level, Sharepoint tries to create the URL: "http://webapplication/sites/" and crash...

Then we must provide a mechanism to differentiate the creation under "sites" level and the creation at root level.

In order to do this, first we must talk about conditional in powershell. The reference to this aspect will be short beacuse the target of this blog is Sharepoint. It´s enough to know the following structures:

BASIC CONDITIONAL: 

 if (<CONDITION>) {<ACTION>}
else {<ALTERNATIVE ACTION>}


CONDITION: The situation to evaluate.
ACTION: The task to do if the condition is true.
ALTERNATE ACTION: The task to do if the condition is false.

LOGICAL OPERATORS: 

-eq     Equal to
-lt     Less than
-gt     Greater than
-ge     Greater than or Eqaul to
-le     Less than or equal to
-ne     Not equal to 


 So  the final code of the script is:

Write-Host "This script creates one site collection after you provide the information needed for it." -ForegroundColor Blue

#This part presents the templates and languages for Site Collections

Write-Host "Codes for templates and languages available"

Get-SPWebTemplate 

#This part recollects the information for Site Collection creation 

$WebApp = read-host "Please, introduce the Web Application URL: "
$ContentDatabase = read-host "
Please, introduce the Content Database name: "
 
#The variable $Sufix is the part of URL after "/sites/
#Example http://webapplication/sites/sufix

$Sufix = read-host "Please, introduce the site collection sufix: "
$Template= read-host "
Please, introduce the Template ID:"
$LocaleID = read-host "
Por favor, introduce the Locale ID:"
$Title = read-host "
Por favor, introduce the Title:"
$OwnerAlias=read-host "
Por favor, introduce the primary administrator(DOMAIN\user):"
$SecondaryOwnerAlias=read-host "
Por favor, introduce the secondary administrator(DOMAIN\USER):"

#This part has two steps. 
#First Content Database creation 

try
{
Write-Host "
Trying to create Content Database... please wait..."

New-SPContentDatabase -Name $ContentDatabase -WebApplication $WebApp -MaxSiteCount 1 -WarningSiteCount 0

#Second Site Collection creation 

Write-Host "Trying to create the Site Collection... please wait..."




#If the variable $sufix is empty create at root level
If ($sufix -eq "") {New-SPSite $webApp -OwnerAlias $OwnerAlias –Language $LocaleID -Template $Template -Name "$Title" -SecondaryOwnerAlias
$SecondaryOwnerAlias -ContentDatabase $ContentDatabase}

#If the variable $sufix has a character string create under /sites/

else {New-SPSite $webApp/sites/$Sufix -OwnerAlias $OwnerAlias –Language $LocaleID -Template $Template -Name "$Title" -SecondaryOwnerAlias
$SecondaryOwnerAlias -ContentDatabase $ContentDatabase}

}

catch
{

Write-Host "
There was a problem:" $_ -ForegroundColor Red

}

No hay comentarios:

Publicar un comentario