I created a script which provisions a host name based site collection. The issue is when it creates the site collection,
it gives it a url as ($webappurl +$sitecollection.url) ie if the web application is test.dev.com and site collection is test2.dev.com then it will create the site collection as test.dev.test2.dev.com . I tried to modify the script by removing the
$webappurl but it gives me all sorts of errors like invalid uri etc. Kinda stuck there . I am attaching the script and the config file which it looks up for. Desired result is creating the site collection with url test2.dev.com
Param( [Parameter(Mandatory=$true)] [string]$PathofConfigFile ) function DeployFeatures($featureList, $url) { Write-Host "Activating Features......" -ForegroundColor Green foreach ($feature in $featureList) { if ($feature.Name -and $feature.Id) { $featurePath = $feature.Name Write-Host "Activating Feature: $featurePath" $SPFeature = Get-SPFeature | Where-Object { $_.Id -eq $feature.Id } if(!$SPFeature.Id) { $status = Install-SPFeature $featurePath -force } $status = Enable-SPFeature -Identity $feature.Id -Url $url -force } else { Write-Host "Failed to enable feature, ensure you have specified Id and Name." -foregroundcolor Red } } } function ProvisionWeb($webList, $url, $rootUrl, [int]$level) { foreach($web in $webList) { $rootUrl = $rootUrl -replace "/$", "" $url = $url -replace "/$", "" $WebUrl = $url + $web.Url $WebUrl = $WebUrl -replace "/$", "" $WebName = $web.Name $WebTemplate = $web.Template $exists = (Get-SPWeb $WebUrl -ErrorAction SilentlyContinue) -ne $null if ($exists) { Write-Host "Existing the web at $WebUrl" -ForegroundColor Yellow } else { Write-Host "Creating new web at $WebUrl" -ForegroundColor Green if($level -eq 0) { #$NewWeb = New-SPWeb $WebUrl -Template $WebTemplate -Addtotopnav -Useparenttopnav -Name $WebName $NewWeb = New-SPWeb $WebUrl -Template $WebTemplate -Useparenttopnav -Name $WebName } else { $NewWeb = New-SPWeb $WebUrl -Template $WebTemplate -Useparenttopnav -Name $WebName } # Deploy features DeployFeatures $web.SelectNodes("Features/Feature") $WebUrl Write-Host "Title: " $NewWeb.Title -foregroundcolor Green Write-Host "URL: " $NewWeb.Url -foregroundcolor Green #Check if Enable Show Subsites if ($web.ShowSubsites -eq $true) { $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($NewWeb) $pubWeb.Navigation.GlobalIncludeSubSites = $true $pubWeb.Navigation.GlobalIncludePages = $false $pubWeb.Update() $NewWeb.Dispose() } } # Provision Lists Write-Host "Provisioning Lists..." -ForegroundColor Green ProvisionLists $web.SelectNodes("Lists/List") $WebUrl # Create child webs foreach($childWeb in $web.SelectNodes("Web")) { ProvisionWeb $childWeb $WebUrl $rootUrl ($level + 1) } } } function ProvisionLists($lists, $url) { $CurrentWeb = Get-SPWeb $url foreach($list in $lists){ $checkList = $CurrentWeb.Lists.TryGetList($list.Title) if ($checkList -eq $null) { if ($list.Template -eq "Asset Library") { $template = $CurrentWeb.ListTemplates[$list.Template] } elseif ($list.Template -eq "Content Library") { $template = $CurrentWeb.ListTemplates | Where-Object {$_.Type -eq 30000} } else { $template = [Microsoft.SharePoint.SPListTemplateType]::($list.Template) } $CurrentWeb.Lists.Add($list.Url,$list.Description, $template) $CurrentWeb.Update() $mylist = $CurrentWeb.Lists[$list.Url] $mylist.Title = $list.Title if (($list.Template -eq "Asset Library") -or ($list.Template -eq "DocumentLibrary") -or ($list.Template -eq "PictureLibrary")) { if ($list.EnableVersion -eq $true) { $mylist.EnableMinorVersions = $true } if ($list.RequireCheckOut -eq $true) { $mylist.ForceCheckout = $true } if ($list.RequireApproval -eq $true) { $mylist.EnableModeration = $true } } $mylist.Update(); Write-Host $mylist.Title successfully created -ForegroundColor Green #Create Folders try{ foreach($folder in $list.Folders.Folder){ $mylist.RootFolder.SubFolders.add($folder); } }catch{ # if it crashed here, the configuration did not include folders Write-Host Configuration did not include folders -ForegroundColor Yellow } } else { Write-Host $list.Title has already existed -ForegroundColor Yellow } } } function ProvisionSiteCollections($siteCollections, [string]$rootUrl) { if($siteCollections -ne $null) { if($siteCollections.Item(0).Deploy -eq $false) { return } Write-Host "Provisioning Site Collections" -ForegroundColor Green $siteCollectionList = $siteCollections.Item(0).SelectNodes("SiteCollection") $webApp = Get-SPWebApplication $rootUrl foreach($SiteCollection in $siteCollectionList) { $ContentDB = $SiteCollection.SelectNodes("ContentDatabase") if ($ContentDB -ne $null) { $databaseServer = $ContentDB.Server $databaseName = $ContentDB.Name if ($SiteCollection.Url -eq "/") { $SiteUrl = ($WebAppUrl + $SiteCollection.Url) } else { if (($SiteCollection.ManagedPath -eq $null) -or ($SiteCollection.ManagedPath.Trim() -eq "") ) { $SiteUrl = ($WebAppUrl + $SiteCollection.Url) } else { $SiteUrl = ($WebAppUrl + $SiteCollection.ManagedPath + $SiteCollection.Url) } } $SiteUrl = $SiteUrl -replace "/$", "" try { $ContentDB = Get-SPContentDatabase -site $SiteUrl -ErrorAction SilentlyContinue } catch{ $ContentDB = $null} #check databas exists? if ( $ContentDB -ne $null) { $exists = $true } else { $exists = $false } if ($exists) { Write-Host "[Warning] Database with name $databaseName already exists on SQL Server $databaseServer" -ForegroundColor Yellow } else { try { Write-Host "Creating Content database $databaseName on $databaseServer" -ForegroundColor Green $db = New-SPContentDatabase -Name $databaseName -WebApplication $webApp -DatabaseServer $databaseServer } catch { Write-Host "Failed to create content database...." -ForegroundColor Red return } } } $SiteCollectionName = $SiteCollection.Name $SiteCollectionOwner = $SiteCollection.OwnerAlias $SiteCollectionTemplate = $SiteCollection.Template try { $targetUrl = Get-SPSite -Identity $SiteUrl -ErrorAction SilentlyContinue } catch{ $targetUrl="" } if ($targetUrl.Url.Length -gt 0) { Write-Host "Existing site at" $SiteUrl -ForegroundColor Yellow $CurrentSite = Get-SPSite $SiteUrl $RootWeb = $CurrentSite.RootWeb } else { Write-Host "Creating new site collection at" $SiteUrl -ForegroundColor Green if ($ContentDB -ne $null) { $NewSite = New-SPSite -URL $SiteUrl -OwnerAlias $SiteCollectionOwner -ContentDatabase $db -Template $SiteCollectionTemplate -Name $SiteCollectionName -HostHeaderWebApplication $WebAppUrl } else { $NewSite = New-SPSite -URL $SiteUrl -OwnerAlias $SiteCollectionOwner -Template $SiteCollectionTemplate -Name $SiteCollectionName -HostHeaderWebApplication $WebAppUrl } $RootWeb = $NewSite.RootWeb #Check if Enable Show Subsites if ($SiteCollection.ShowSubsites -eq $true) { $pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($RootWeb) $pubWeb.Navigation.GlobalIncludeSubSites = $true $pubWeb.Navigation.GlobalIncludePages = $false $pubWeb.Update() } Write-Host "Site collection created successfully" -ForegroundColor Green Write-Host "Title:" $RootWeb.Title -foregroundcolor Green Write-Host "URL:" $RootWeb.Url -foregroundcolor Green } DeployFeatures $SiteCollection.SelectNodes("Features/Feature") $RootWeb.Url # Provision Lists Write-Host "Provisioning Lists..." -ForegroundColor Green ProvisionLists $SiteCollection.SelectNodes("Lists/List") $RootWeb.Url # Provision Sub-Sites Write-Host "Provisioning Sub-Sites..." -ForegroundColor Green ProvisionWeb $SiteCollection.SelectNodes("Web") $RootWeb.Url $RootWeb.Url "0" } } } function InitApplication() { Write-Host "Initializing ..." -ForegroundColor Green $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" } } try { [xml]$ConfigFile = Get-Content $PathofConfigFile } catch { Write-Host "Failed to load the configuration file (Config.xml), ensure it's in the same directory as the script" -ForegroundColor Red return } $ServiceAccountId = $ConfigFile.Setup.ServiceAccount.UserId $ServiceAccountPwd = $ConfigFile.Setup.ServiceAccount.Password Write-Host $ServiceAccountId " , password is " $ServiceAccountPwd $WebAppUrl = $ConfigFile.Setup.WebAppUrl -replace "/$", "" Write-Host $WebAppUrl # initialize the script InitApplication # deploy site collections ProvisionSiteCollections $ConfigFile.SelectNodes("/Setup/SiteCollections") $WebAppUrl Write-Host "Site Structure Provisioning Process has been completed ..." -ForegroundColor Green
<?xml version="1.0" encoding="UTF-8"?> -<Setup WebAppUrl="http://test.dev.com"> -<SiteCollections> -<SiteCollection ShowSubsites="true" Template="BDR#0" OwnerAlias="dev\spdevfarmsetup" Url="test2.dev.com" Name="Unit Share"><ContentDatabase Name="SP2013_test_ContentDB" WarningSiteCount="80" MaxSiteCount="100" Server="enadev2.dev.com"/> -<Features><Feature Name="PremiumSite" Id="8581a8a7-cf16-4770-ac54-260265ddb0b2"/><Feature Name="PublishingSite" Id="f6924d36-2fa8-4f0b-b16d-06b7250180fa"/><Feature Name="BaseSite" Id="b21b090c-c796-4b0f-ac0f-7ef1659c20ae"/><Feature Name="PremiumWeb" Id="0806d127-06e6-447a-980e-2e90b03101b8"/><Feature Name="BaseWeb" Id="99fe402e-89a0-45aa-9163-85342e865dc8"/><Feature Name="PublishingWeb" Id="94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"/></Features> -<Lists><List Template="DocumentLibrary" Url="UnitShare" RequireApproval="false" RequireCheckOut="false" EnableVersion="false" Description="Unit Share Library" Title="Unit Share"/></Lists><Web ShowSubsites="true" Template="SRCHCEN#0" Url="/search" Name="Search Center"/></SiteCollection></SiteCollections></Setup>