Summary
The following script will create and populate data in a SharePoint Farm for testing.
What does the Script do?
- Create a new web application, with a new web application pool.
- Create new database and associate it with the new web application.
- Update the local HOSTS file to make the new site accessible from the server.
- Create a Root Site in the new web application.
- Create a Test document and store it in the local temp folder.
- Populate the default “Document Library” in the root site with the number of documents specified.
- Create new lists in the root site based on the number of lists specified.
- Populate each list in the root site with the number of items specified.
- Create new sub sites based on the amount specified.
- Populate the default “Document Library” in all sub sites with the number of documents specified.
- Create new lists in each sub site based on the number of lists specified.
-
Populate each list in all sub sites with the number of items specified.
Important Notes:
- Since this script will update your local HOSTS file you will need to run PowerShell as Administrator.
- DisableLoopBackCheck may be required if you access the site locally from the server.
reg.exe add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa /v DisableLoopBackCheck /t REG_DWORD /d 1 /f
Example:
How many Sites should be created?: 1
How many Lists should be created?: 2
How many Items in each list?: 5
How many Documents should be created?: 5
Specify the full URL of the new SharePoint Site, without http://. Example: contoso, contoso.local or www.contoso.com: sptest2
Enter your SQL Server Name: michlee-sql
Name of the new Content DB: sptest2_content_db
Enter the SP Managed Account. Example: contoso\spadmin: mylab\spservice
Enter a name for the new Web App: SPTest2 Web App
Enter a name for the new AppPool: sptest2apppool
The new Web Application sptest2 is being created. Please stand by…
The new Content Database sptest2_content_db is being created. Please stand by…
Id : eb6f2c05-e7d8-491a-9b6f-0a17b2624c65
Name : sptest2_content_db
WebApplication : SPWebApplication Name=SPTest2 Web App
Server : michlee-sql
CurrentSiteCount : 0
127.0.0.1 sptest2
The new Root Site http://sptest2 is being created. Please stand by…
Creating document: /Shared Documents/TestDocument1.csv …
Creating document: /Shared Documents/TestDocument2.csv …
Creating document: /Shared Documents/TestDocument3.csv …
Creating document: /Shared Documents/TestDocument4.csv …
Creating document: /Shared Documents/TestDocument5.csv …
Guid : 5dd9162c-08bd-4fd1-8079-6c3e106386c8
##################################
List 1 created in http://sptest2/
#################################
Item: 1 created in list: 1
################################
Item: 2 created in list: 1
################################
Item: 3 created in list: 1
################################
Item: 4 created in list: 1
################################
Item: 5 created in list: 1
################################
Guid : c81edcd1-71d4-427a-8cd6-a0015a712149
##################################
List 2 created in http://sptest2/
##################################
Item: 1 created in list: 2
################################
Item: 2 created in list: 2
################################
Item: 3 created in list: 2
################################
Item: 4 created in list: 2
################################
Item: 5 created in list: 2
################################
####################
Creating Sub Site 1
####################
Creating document: /1/Shared Documents/TestDocument1.csv …
Creating document: /1/Shared Documents/TestDocument2.csv …
Creating document: /1/Shared Documents/TestDocument3.csv …
Creating document: /1/Shared Documents/TestDocument4.csv …
Creating document: /1/Shared Documents/TestDocument5.csv …
Guid : 585c9288-d72a-472d-8532-5a3139537777
##################################
List 1 created in http://sptest2/1
##################################
Item: 1 created in list: 1
################################
Item: 2 created in list: 1
################################
Item: 3 created in list: 1
################################
Item: 4 created in list: 1
################################
Item: 5 created in list: 1
################################
Guid : a49c9124-de8f-4174-bd12-3958ec1fd62b
###################################
List 2 created in http://sptest2/1
###################################
Item: 1 created in list: 2
################################
Item: 2 created in list: 2
################################
Item: 3 created in list: 2
################################
Item: 4 created in list: 2
################################
Item: 5 created in list: 2
################################
##########################################
Your Site population script has completed!
##########################################
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# Load Microsoft.SharePoint.PowerShell Add-PSSnapin Microsoft.SharePoint.Powershell -ea 0 #Script Parameters $sitecount = (Read-Host "How many Sites should be created?") $listcount = (Read-Host "How many Lists should be created?") $itemcount = (Read-Host "How many Items in each list?") $doccount = (Read-Host "How many Documents should be created?") $url = (Read-Host "Specify the full URL of the new SharePoint Site, without http://. Example: contoso, contoso.local or www.contoso.com") $sqlsvr = (Read-Host "Enter your SQL Server Name") $sqldb = (Read-Host "Name of the new Content DB") $siteowner = (Read-Host "Enter the SP Managed Account. Example: contoso\spadmin") $sitename = (Read-Host "Enter a name for the new Web App") $apppool = (Read-Host "Enter a name for the new AppPool") $is2010 = (Read-Host "Is this a 2010 Farm Y/N?") #Adding 2010 capability if ($is2010 -eq "Y"){ $docLibraryName = "Shared Documents" } if ($is2010 -eq "N"){ $docLibraryName = "Documents" } #Static Settings $sourceDocumentPath = "$env:TEMP\temp\TestDocument.csv" $newFilenamePrefix = "TestDocument" $newFilenameExtension = ".csv" $hostsfile = "$env:windir\System32\drivers\etc\hosts" $spaccount = (Get-SPManagedAccount $siteowner) $user2 = whoami #Create the new web app Write-Host "The new Web Application $url is being created. Please stand by..." -ForegroundColor Green $site = "http://" + $url $ap = New-SPAuthenticationProvider $webapp = New-SPWebApplication -Name $sitename -URL $site -HostHeader $url -Port 80 -ApplicationPool $apppool -ApplicationPoolAccount $spaccount -AuthenticationProvider $ap #Create a new Content Database Write-Host "The new Content Database $sqldb is being created. Please stand by..." -ForegroundColor Yellow New-SPContentDatabase $sqldb -DatabaseServer $sqlsvr -WebApplication $webapp #This will update your HOSTS file with the named used in the URL so the site is accessible from the server #Note: This will require that PowerShell runs in elevated mode "127.0.0.1 $url" | Add-Content -PassThru $hostsfile #Create Root Site Write-Host "The new Root Site $site is being created. Please stand by..." -ForegroundColor Yellow $SiteTemplate = "STS#0" $Language = 1033 New-SPSite -Url $site -OwnerAlias $siteowner -SecondaryOwnerAlias $user2 -Template $SiteTemplate -Language $Language -ContentDatabase $sqldb #Test file creation #make a new folder if it does not exist $TARGETDIR = "$env:TEMP\temp" if(!(Test-Path -Path $env:TEMP\temp)){ New-Item -ItemType directory -Path $TARGETDIR } dir | export-csv "$env:TEMP\temp\TestDocument.csv" ################################## #Root WEB Creation and Population# ################################## #defining WEB $web = Get-SPWeb $site #Create Documents $docLibrary = $web.Lists[$docLibraryName] $docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl $folderPathWithinDocLibrary = "" $uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary) #Open file $file = get-item $sourceDocumentPath $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead() #Starting loop create docs in Root Library for($d=1; $d -le $doccount; $d++) { $newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$d+$newFilenameExtension write-host "Creating document: $newFilePath ..." -ForegroundColor Yellow $spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true) } #Start Loop for List creation based on $listcount in ROOT Library for($l=1; $l -le $listcount; $l++) #Create the Lists { $ListTemplate = $web.ListTemplates["Custom List"] $web.Lists.Add("List $l","List $l",$listTemplate) Write-Host "####################################" -ForegroundColor Green write-host "List $l created in $site" -ForegroundColor Green Write-Host "####################################" -ForegroundColor Green # While creating lists put new items in it based on the $itemcount if ($l -le $listcount ) { #Start Loop for List Item creation in ROOT Library for ($i=1; $i -le $itemcount; $i++) { #Create List Item $list = $web.Lists["List $l"] $newItem = $list.AddItem() $newItem["Title"] = "Item $i" $newItem.Update() write-host "Item: $i created in list: $l" -ForegroundColor Yellow Write-Host "##############################" -ForegroundColor Yellow }}} ################################# #Sub WEB Creation and Population# ################################# #Start Loop for site creation for($s=1; $s -le $sitecount ; $s++) { $SiteUrl = "" $SiteUrl = $Site + "/" $SiteUrl = $SiteUrl += $s #Create Webs based on $sitecount Write-Host "####################" -ForegroundColor Yellow Write-Host "Creating Sub Site $s" -ForegroundColor Yellow Write-Host "####################" -ForegroundColor Yellow New-SPWeb $SiteUrl -Template $SiteTemplate -Name $s -UseParentTopNav -Language $Language $web = Get-SPWeb $SiteUrl #Create Documents $docLibrary = $web.Lists[$docLibraryName] $docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl $folderPathWithinDocLibrary = "" $uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary) #Open file $file = get-item $sourceDocumentPath $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead() #Starting loop to create docs in Library for($d=1; $d -le $doccount; $d++) { $newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$d+$newFilenameExtension write-host "Creating document: $newFilePath ..." -ForegroundColor Yellow $spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true) } #Start Loop for List creation based on $listcount for($l=1; $l -le $listcount; $l++) #Create the Lists { $ListTemplate = $web.ListTemplates["Custom List"] $web.Lists.Add("List $l","List $l",$listTemplate) Write-Host "########################################" -ForegroundColor Green write-host "List $l created in $SiteUrl" -ForegroundColor Green Write-Host "########################################" -ForegroundColor Green #Populate list items based on the $itemcount if ($l -le $listcount ) { #Start Loop for List Item creation for ($i=1; $i -le $itemcount; $i++) { #Create List Item $list = $web.Lists["List $l"] $newItem = $list.AddItem() $newItem["Title"] = "Item $i" $newItem.Update() write-host "Item: $i created in list: $l" -ForegroundColor Yellow Write-Host "################################" -ForegroundColor Yellow }}}} #Close file stream $fileStream.Close() #Dispose web $web.Dispose() Write-Host "##########################################" -ForegroundColor Green write-host "Your Site population script has completed!" -ForegroundColor Green Write-Host "##########################################" -ForegroundColor Green |