-
Notifications
You must be signed in to change notification settings - Fork 0
/
sharepoint-load-test-data-generator.ps1
193 lines (192 loc) · 8.07 KB
/
sharepoint-load-test-data-generator.ps1
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
192
193
Add-PSSnapin Microsoft.SharePoint.PowerShell
$sharepointHost = "http://win-od8k4mkm92n"
$siteNamePrefix = "test4site"
$subsiteNamePrefix = "test4subsite"
$siteGroupNamePrefix = "test4spsitegroup"
$documentLibraryNamePrefix = "test4doclib"
$docLibFileDumpFolder = "C:\outdir\document-library-dumps"
$numSites = 2
$numSubSites = 2
$numDocumentLibraies = 2
$numSharepointSiteGroupsToCreate = 5
$siteOwner = "win-od8k4mkm92n\administrator"
function New-SPList {
<#
.Synopsis
Use New-SPList to create a new SharePoint List or Library.
.Description
This advanced PowerShell function uses the Add method of a SPWeb object to create new lists and libraries in a SharePoint Web
specified in the -Web parameter.
.Example
C:\PS>New-SPList -Web http://intranet -ListTitle "My Documents" -ListUrl "MyDocuments" -Description "This is my library" -Template "Document Library"
This example creates a standard Document Library in the http://intranet site.
.Example
C:\PS>New-SPList -Web http://intranet -ListTitle "My Announcements" -ListUrl "MyAnnouncements" -Description "These are company-wide announcements." -Template "Announcements"
This example creates an Announcements list in the http://intranet site.
.Notes
You must use the 'friendly' name for the type of list or library. To retrieve the available Library Templates, use Get-SPListTemplates.
.Link
http://www.iccblogs.com/blogs/rdennis
http://twitter.com/SharePointRyan
.Inputs
None
.Outputs
None
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$Web,
[Parameter(Mandatory=$true)]
[string]$ListTitle,
[Parameter(Mandatory=$true)]
[string]$ListUrl,
[Parameter(Mandatory=$false)]
[string]$Description,
[Parameter(Mandatory=$true)]
[string]$Template
)
Start-SPAssignment -Global
$SPWeb = Get-SPWeb -Identity $Web
$listTemplate = $SPWeb.ListTemplates[$Template]
$SPWeb.Lists.Add($ListUrl,$Description,$listTemplate)
$list = $SPWeb.Lists[$ListUrl]
$list.Title = $ListTitle
$list.Update()
$newListRootFolder = $list.RootFolder.Name
Write-Host "Created new list $Web/$newListRootFolder" -foregroundcolor Green
$SPWeb.Dispose()
Stop-SPAssignment -Global
}
function Upload-Files-To-SPDocumentList {
<#
.Synopsis
Use to upload a bunch of files in a directory to a SPList
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]$Web,
[Parameter(Mandatory=$true)]
[string]$DocLibraryGuid,
[Parameter(Mandatory=$true)]
[string]$ListUrl,
[Parameter(Mandatory=$true)]
[string]$DocLibraryUrlName,
[Parameter(Mandatory=$true)]
[string]$LocalFolderPath,
[Parameter(Mandatory=$false)]
[string]$RelativeParentFileName
)
Start-SPAssignment -Global
$SPWeb = Get-SPWeb $Web
$docLibrary = $SPWeb.Lists[[GUID]($DocLibraryGuid)]
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
ForEach($file in $files)
{
$newFileUrl = $docLibrary.RootFolder.Name
if ($RelativeParentFileName)
{
$newFileUrl += $RelativeParentFileName
}
$folder = $SPWeb.GetFolder($newFileUrl)
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
$newRelativeFilePath = $newFileUrl + "/" + $file.Name
$spFile = $folder.Files.Add($newRelativeFilePath, [System.IO.Stream]$fileStream, $true)
write-host "Uploaded new file $Web/$newRelativeFilePath" -foregroundcolor Green
$fileStream.Close();
}
$dirs = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetDirectories()
ForEach($dir in $dirs)
{
$newFolderUrl = $docLibrary.RootFolder.Name
if ($RelativeParentFileName)
{
$newFolderUrl += $RelativeParentFileName
}
$parentFolder = $newFolderUrl
$newFolderUrl += "/" + $dir.Name
$folder = $SPWeb.GetFolder($newFolderUrl)
$parentFolder = $SPWeb.GetFolder($parentFolder).ServerRelativeUrl
if (-Not $folder.Exists)
{
$folder = $docLibrary.AddItem($parentFolder, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $dir.Name)
$folder["Title"] = $dir.Name
$folder.Update();
write-host "Created new folder $Web/$newFolderUrl" -foregroundcolor Green
$groupToAssign = $siteGroupNamePrefix + "1"
$group = $SPWeb.SiteGroups[$groupToAssign]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $SPWeb.RoleDefinitions["Read"];
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
$folder = $SPWeb.GetFolder($newFolderUrl)
$folder.Item.BreakRoleInheritance($true);
$folder.Item.RoleAssignments.Add($roleAssignment);
$folder.Item.Update();
write-host "Assigned group $siteGroupNamePrefix1 to folder" -foregroundcolor Green
}
$newRelUrl = $RelativeParentFileName + "/" + $dir.Name
Upload-Files-To-SPDocumentList -Web $Web -DocLibraryGuid $DocLibraryGuid -DocLibraryUrlName $DocLibraryUrlName -ListUrl $ListUrl -LocalFolderPath $dir.FullName -RelativeParentFileName $newRelUrl
}
$SPWeb.Dispose()
Stop-SPAssignment -Global
}
function Create-SPGroupInWeb
{
param ($Url, $GroupName, $PermissionLevel, $Description)
$web = Get-SPWeb -Identity $Url
if ($web.SiteGroups[$GroupName] -ne $null)
{
Write-Host "Group $GroupName already exists!" -foregroundcolor Red
}
else
{
$web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, $Description)
$group = $web.SiteGroups[$GroupName]
$roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
$roleDefinition = $web.Site.RootWeb.RoleDefinitions[$PermissionLevel]
$roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
$web.RoleAssignments.Add($roleAssignment)
$web.Update()
Write-Host "Group $GroupName created successfully" -foregroundcolor Green
}
$web.Dispose()
}
function ProcessSite {
<#
.Synopsis
Add doc libs to a site, then adds groups to the site, then adds files to the document libraries.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$siteUrl
)
for ($k=1; $k -le $numSharepointSiteGroupsToCreate; $k++)
{
Create-SPGroupInWeb -Url $siteUrl -GroupName "$siteGroupNamePrefix$k" -PermissionLevel "Read" -Description "My group description"
}
for ($k=1; $k -le $numDocumentLibraies; $k++)
{
$listUrl = "$documentLibraryNamePrefix$k"
$listTitle = "Document Library $docLibraryIdx"
$docLibraryUrlName = "$documentLibraryNamePrefix$j"
$spNewList = New-SPList -Web $siteUrl -ListTitle "Document Library $docLibraryIdx" -ListUrl $listUrl -Description "This is my library" -Template "Document Library"
Upload-Files-To-SPDocumentList -Web $siteUrl -DocLibraryGuid $spNewList.Guid -ListUrl $listUrl -localFolderPath $docLibFolders[$docLibFolderIndex].FullName -DocLibraryUrlName $docLibraryUrlName
$docLibFolderIndex += 1
}
}
$docLibFolderIndex = 0
$docLibFolders = ([System.IO.DirectoryInfo] (Get-Item $docLibFileDumpFolder)).GetDirectories()
for ($i=1; $i -le $numSites; $i++)
{
write-host "Creating site $sharepointHost/sites/$siteNamePrefix$i which is $i out of $numSites to create" -foregroundcolor Green
New-SPSite $sharepointHost/sites/$siteNamePrefix$i -OwnerAlias "$siteOwner" -Name "$siteNamePrefix-$i" -Template "STS#2"
ProcessSite $sharepointHost/sites/$siteNamePrefix$i
for ($j=1; $j -le $numSubSites; $j++)
{
write-host "Creating sub-site $sharepointHost/sites/$siteNamePrefix$i/$subsiteNamePrefix$j which is $j out of $numSubSites to create." -foregroundcolor Green
New-SPWeb $sharepointHost/sites/$siteNamePrefix$i/$subsiteNamePrefix$j -Template "STS#2"
ProcessSite $sharepointHost/sites/$siteNamePrefix$i/$subsiteNamePrefix$j
}
}