-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.ps1
505 lines (454 loc) · 20.1 KB
/
Script.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Specify tenant admin and site URL
$User = Read-Host -Prompt "Please enter your username"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$SiteURL = "Your SharePoint Site Url"
#Get Credentials to connect
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Context.Credentials = $Creds
Function Get-ListIdByName
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!")
)
Try {
$List = $Context.Web.Lists.GetByTitle($ListName)
return $List.Id;
}
Catch {
write-host -f Red "Error Creating List!" $_.Exception.Message
}
}
Function New-SPOList
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$ListTitle = $(throw "Please Enter the Title for the List!")
)
Try {
#Retrieve lists
$Lists = $Context.Web.Lists
$Context.Load($Lists)
$Context.ExecuteQuery()
#Create list with "custom" list template
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $ListName
$ListInfo.TemplateType = "100"
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = $ListName
$List.Update()
$Context.ExecuteQuery()
#Get the List
$List=$Context.Web.Lists.GetByTitle($ListName)
$List.Title = $ListTitle
$List.Update()
$Context.ExecuteQuery()
Write-Host "Success, List: $ListName" -f Green
}
Catch {
write-host -f Red "Error Creating List!" $_.Exception.Message
}
}
Function Add-SPOListColumn-Text
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='Text' DisplayName='$DisplayName' Name='$FieldName' Description='$Description' Required='$IsRequired' ></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: Text" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-MultiText
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='Note' DisplayName='$DisplayName' Name='$FieldName' Description='$Description' NumLines='5' RichText='FALSE' Sortable='FALSE'></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: MultiText" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-Boolean
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!"),
[Parameter(Mandatory=$true)] [Int32]$DefaultValue = $(throw "Please Enter the DefaultValue!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='Boolean' DisplayName='$DisplayName' Name='$FieldName' Description='$Description'><Default>$DefaultValue</Default></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: Boolean" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-DateTime
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='DateTime' DisplayName='$DisplayName' Name='$FieldName' Description='$Description'></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: DateTime" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-Number
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='Number' DisplayName='$DisplayName' Name='$FieldName' Description='$Description'></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: Number" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-Lookup
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!"),
[Parameter(Mandatory=$true)] [string]$LookupList = $(throw "Please Enter the LookupList!"),
[Parameter(Mandatory=$true)] [string]$ShowField = $(throw "Please Enter the ShowField!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='Lookup' DisplayName='$DisplayName' Name='$FieldName' Description='$Description' List='$LookupList' ShowField='$ShowField'></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: Lookup, List: $LookupList, ShowField: $ShowField" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-Url
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='URL' DisplayName='$DisplayName' Name='$FieldName' Description='$Description' Format='Hyperlink'></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: URL" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-User
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$FieldSchema = "<Field Type='User' DisplayName='$DisplayName' Name='$FieldName' Description='$Description' Format='Hyperlink'></Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: User" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function Add-SPOListColumn-Choice
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the List Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the FieldName!"),
[Parameter(Mandatory=$true)] [string]$DisplayName = $(throw "Please Enter the DisplayName!"),
[Parameter(Mandatory=$true)] [string]$Description = $(throw "Please Enter the Description!"),
[Parameter(Mandatory=$true)] [boolean]$IsRequired = $(throw "Please Enter the IsRequired!"),
[Parameter(Mandatory=$true)] [string[]]$Choices = $(throw "Please Enter the Choices!"),
[Parameter(Mandatory=$true)] [string]$DefaultChoice = $(throw "Please Enter the Default Choice!")
)
Try {
#Get the List
$List = $Context.Web.Lists.GetByTitle($ListName)
$Fields = $List.Fields
$Context.Load($List)
$Context.Load($Fields)
$Context.ExecuteQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Field '$FieldName' already exists in the List!" -f Yellow
}
else
{
$start1 = "<CHOICES>"
$end1 = "</CHOICES>"
$start2 = "<CHOICE>"
$end2 = "</CHOICE>"
$default1 = "<Default>"
$default2 = "</Default>"
$choiseString = $default1 + $DefaultChoice + $default2 + $start1
Foreach($choice in $Choices)
{
$choiseString = $choiseString + $start2 + $choice+ $end2
}
$choiseString = $choiseString + $end1
#Write-Host $choiseString
$FieldSchema = "<Field Type='Choice' DisplayName='$DisplayName' Name='$FieldName' Description='$Description' Format='Dropdown'>'$choiseString'</Field>"
$NewField = $List.Fields.AddFieldAsXml($FieldSchema, $true, [Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$List.Update()
$Context.ExecuteQuery()
Write-host "Success, List: $ListName, Field: $FieldName, Type: Choice" -f Green
}
}
Catch {
write-host -f Red "Error Adding Column!" $_.Exception.Message
}
}
Function New-SPOGroup
{
param
(
[Parameter(Mandatory=$true)] [string]$GroupName = $(throw "Please Enter the Group Name!"),
[Parameter(Mandatory=$true)] [string]$GroupDescription = $(throw "Please Enter the Group Description!"),
[Parameter(Mandatory=$true)] [string]$PermissionLevel = $(throw "Please Enter the Permission Level!")
)
Try {
#Get all existing groups of the site
$Groups = $Context.Web.SiteGroups
$Context.load($Groups)
$Context.ExecuteQuery()
#Get Group Names
$GroupNames = $Groups | Select -ExpandProperty Title
$NewGroup = $Groups | where { ($_.Title -eq $GroupName) }
if($NewGroup -ne $NULL)
{
Write-host "Group '$GroupName' already exists in the site!" -f Yellow
}
else
{
#sharepoint online powershell create group
$GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
$GroupInfo.Title = $GroupName
$GroupInfo.Description = $GroupDescription
$Group = $Context.web.SiteGroups.Add($GroupInfo)
$Context.ExecuteQuery()
#Get Group Title and ID
$Groups | Select Title, ID
#Assign permission to the group
$RoleDef = $Context.web.RoleDefinitions.GetByName($PermissionLevel)
$RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Context)
$RoleDefBind.Add($RoleDef)
$Context.Load($Context.Web.RoleAssignments.Add($Group,$RoleDefBind))
$Context.ExecuteQuery()
write-Host -f Green "Success, Group: $GroupName"
}
}
Catch {
write-host -f Red "Error Creating Group!" $_.Exception.Message
}
}
Function Rename-SPOListColumn
{
param
(
[Parameter(Mandatory=$true)] [string]$ListName = $(throw "Please Enter the Column Name!"),
[Parameter(Mandatory=$true)] [string]$FieldName = $(throw "Please Enter the New Name!"),
[Parameter(Mandatory=$true)] [string]$Title = $(throw "Please Enter the List Name!")
)
Try
{
$List = $Context.Web.Lists.GetByTitle($ListName)
$Field = $List.Fields.GetByInternalNameOrTitle($FieldName)
$Field.Title = $Title
$Field.Update()
$Context.ExecuteQuery()
Write-Host "Field Name $FieldName Changed To: $Title" -f Green
}
Catch
{
write-host -f Red "Something Went Wrong!" $_.Exception.Message
}
}