SharePoint 2010 Pre-Population powershell script

I was trying to do some bulk upload Test using Powershell and found the below link to pre-populate SharePoint with some Test Data.  I tried to make it work but there were few bugs. The actual script writtne by ArianNevjestic is still available at http://blogs.technet.com/b/anevjes/archive/2010/03/01/sharepoint-2010-pre-population-powershell-script.aspx

Note that this is not my Script.  I have made some changes and putting the updated one here.

Set-ExecutionPolicy Unrestricted

#USER Defined Variables

$siteCollTemplate = “STS#0″
$webAppProtocol =”http://”
$WebAppsToCreate = @(“extranet.contoso.com:80″,”www.contoso.com:80“)
$numSiteCollectionsPerWebApp =5
$OwnerAlias = “contoso\sp_admin”
$docLib = “AnalyticsReports”
$fileSource = “C:\Users\sp_admin\Desktop\UploadFiles”
$appPoolExt = “App_Pool”
# DO not edit anything beyond this line

function SPContentUpload {

#my document upload function to a sharepoint doc library.

param ([string]$urlDocLib,[string]$urlSPWeb,[string]$strDirPath)

$hshDocProps=@{“ContentType”=”Document“}
[void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
$SPsite=new-object Microsoft.SharePoint.SPSite($urlDocLib)
$SPweb=$SPsite.openweb($urlSPWeb)

#loop through all the files in the source folder and add each file to the defined SharePoint doc library

dir $strDirPath | foreach-object {
               $bytes=get-content $_.fullname -encoding byte
               $arrbytes=[byte[]]$bytes
               $SPWeb.files.Add($($urlDocLib +$docLib) + “/” + $_.Name + [guid]::newGuid().tostring(),$arrbytes,$hshDocProps, $true)
 

}

$SPSite.Dispose()

}

#[guid]::newGuid().tostring()
$b=1

foreach ($webApp in $WebAppsToCreate) {
$Split=$webApp.split(“:”)
$webAppUrl=$Split[0]
$webAppPort =$Split[1]

Write-Host Web App Url $webAppUrl $webAppPort
$webAppUrlFull=”$webAppProtocol$webAppUrl”

$OwnerMA = Get-SPManagedAccount -Identity “Contoso\sp_admin”
New-SPWebApplication -Name $($webAppUrl + ” Web Application” + $b) –URL $webAppUrlFull -port $webAppPort -ApplicationPool “$($webAppUrl + “_app_Pool” + $b)” -ApplicationPoolAccount $OwnerAlias -DatabaseName $($webAppUrl + “_” + $webAppPort) -HostHeader $webAppUrl

Write-Host Web App Url $webAppUrlFull

#Create new content db

#New-SPContentDatabase $webAppUrl -WebApplication $webAppUrlFull
New-SPContentDatabase $($webAppUrl + “_” + $webAppPort + “_2”) -WebApplication $($webAppUrlFull + “:” + $webAppPort)

$b++

$fileSource = “C:\Users\sp_admin\Desktop\UploadFiles”
$i=1

do {
$siteCollPath = $($webAppUrlFull + “:” + $webAppPort + “/sites/” + $i)
$siteCollPath2 = $($webAppUrlFull + “:” + $webAppPort + “/sites/” + $i + “_”)

Write-Host Creating site collection $siteCollPath

#create site collection to go to unique content database
New-SPSite -url $siteCollPath -OwnerAlias $OwnerAlias -Name “Test Wiki content db_1_ $i” -Template $siteCollTemplate -ContentDatabase $($webAppUrl + “_” + $webAppPort)
#populate Doc lib for above site collection
SPContentUpload “$($webAppUrlFull + “:” + $webAppPort + “/sites/” + $i + “/”)” “” “$fileSource”
#create site collection to go to sep content database
New-SPSite -url $siteCollPath2 -OwnerAlias $OwnerAlias -Name “Test Wiki content db_2_$i” -Template $siteCollTemplate -ContentDatabase $($webAppUrl + “_” + $webAppPort + “_2”)

#populate Doc lib for above site collection
SPContentUpload “$($webAppUrlFull + “:” + $webAppPort + “/sites/” + $i + “_/”)” “” “$fileSource”

$i++
}
while ($i -le $numSiteCollectionsPerWebApp)
}

Couple of Tips: Make sure that you have access to your Source file Folder.   If UAC is on you will not have access to C Drive.

Do not copy paste the script from you PC to HyperV VM using write clipboard text.  Seems Like some words were replaced by special characters.

Advertisements

One thought on “SharePoint 2010 Pre-Population powershell script

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