-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoneplaceSolutionsSite-Config-v2-onPrem-classic.ps1
308 lines (265 loc) · 14.4 KB
/
oneplaceSolutionsSite-Config-v2-onPrem-classic.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
<#
This script applies the configuration changes for the OnePlace Solutions site.
All major actions are logged to 'OPSScriptLog.txt' in the user's or Administrators Documents folder, and it is uploaded to the Solutions Site at the end of provisioning.
#>
$ErrorActionPreference = 'Stop'
$script:logFile = "OPSScriptLog.txt"
$script:logPath = "$env:userprofile\Documents\$script:logFile"
function Write-Log {
<#
.NOTES
Created by: Jason Wasser @wasserja
Modified by: Ashley Gregory
.LINK (original)
https://gallery.technet.microsoft.com/scriptcenter/Write-Log-PowerShell-999c32d0
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("LogContent")]
[string]$Message,
[Parameter(Mandatory=$false)]
[Alias('LogPath')]
[string]$Path = $script:logPath,
[Parameter(Mandatory=$false)]
[ValidateSet("Error","Warn","Info")]
[string]$Level = "Info",
[Parameter(Mandatory=$false)]
[switch]$NoClobber
)
Begin {
$VerbosePreference = 'SilentlyContinue'
$ErrorActionPreference = 'Continue'
}
Process {
# If the file already exists and NoClobber was specified, do not write to the log.
If ((Test-Path $Path) -AND $NoClobber){
Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name."
Return
}
# If attempting to write to a log file in a folder/path that doesn't exist create the file including the path.
ElseIf (!(Test-Path $Path)){
Write-Verbose "Creating $Path."
$NewLogFile = New-Item $Path -Force -ItemType File
}
Else {
# Nothing to see here yet.
}
# Format Date for our Log File
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss K"
# Write message to error, warning, or verbose pipeline and specify $LevelText
Switch($Level) {
'Error' {
Write-Error $Message
$LevelText = 'ERROR:'
}
'Warn' {
Write-Warning $Message
$LevelText = 'WARNING:'
}
'Info' {
Write-Verbose $Message
$LevelText = 'INFO:'
}
}
# Write log entry to $Path
"$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append
}
End {
$ErrorActionPreference = 'Stop'
}
}
Write-Host "Beginning script. Logging script actions to $script:logPath" -ForegroundColor Cyan
Start-Sleep -Seconds 3
Try {
Set-ExecutionPolicy Bypass -Scope Process
function Send-OutlookEmail ($attachment,$body) {
Try {
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "[email protected]"
$Mail.CC = "[email protected]"
$from = $Outlook.Session.CurrentUser.Name
$Mail.Subject = "Solutions Site and License List Information generated by $from"
$Mail.Body = "Hello Customer Success Team`n`nPlease find our Solutions Site and License List details below:`n`n$body"
$mail.Attachments.Add($attachment) | Out-Null
Write-Host "Please open the new email being composed in Outlook, add information as necessary, and send it to the address indicated ([email protected])" -ForegroundColor Yellow
$mail.Display()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
}
Catch {
Write-Host "Failed to open a new email in Outlook." -ForegroundColor Red
Write-Log -Level Error -Message $_
}
}
Write-Host "`n--------------------------------------------------------------------------------`n" -ForegroundColor Red
Write-Host 'Welcome to the Solutions Site deployment script for OnePlace Solutions.' -ForegroundColor Green
Write-Host "`n--------------------------------------------------------------------------------`n" -ForegroundColor Red
$stage = "Stage 1/3 - Connect to Site Collection"
Write-Host "`n$stage`n" -ForegroundColor Yellow
Write-Progress -Activity "Solutions Site Deployment" -CurrentOperation $stage -PercentComplete (33)
Write-Host "Please ensure you have created a new Site Collection with the template 'Team Site' (classic if the option is given) and the URL '/oneplacesolutions'"
$SolutionsSiteUrl = Read-Host -Prompt "Enter your new Site Collection URL here, eg http://contoso.com/sites/oneplacesolutions"
$licenseListUrl = $SolutionsSiteUrl + "/Lists/Licenses"
Try {
$stage = "Stage 2/3 - Apply Solutions Site template"
Write-Host "`n$stage`n" -ForegroundColor Yellow
Write-Progress -Activity "Solutions Site Deployment" -CurrentOperation $stage -PercentComplete (66)
#Connecting to the site collection to apply the template
Connect-pnpOnline -url $SolutionsSiteUrl
#Download OnePlace Solutions Site provisioning template
$WebClient = New-Object System.Net.WebClient
$Url = "https://raw.githubusercontent.com/OnePlaceSolutions/OnePlaceLiveSitePnP/master/oneplaceSolutionsSite-template-v2.xml"
$Path = "$env:temp\oneplaceSolutionsSite-template-v2.xml"
If(-not (Test-Path $Path)) {
$filler = "Downloading provisioning xml template to: $Path"
Write-Host $filler -ForegroundColor Yellow
Write-Log -Level Info -Message $filler
$WebClient.DownloadFile( $Url, $Path )
}
#Download OnePlace Solutions Company logo to be used as Site logo
$UrlSiteImage = "https://raw.githubusercontent.com/OnePlaceSolutions/OnePlaceLiveSitePnP/master/oneplacesolutions-logo.png"
$PathImage = "$env:temp\oneplacesolutions-logo.png"
If(-not (Test-Path $PathImage )) {
$filler = "Downloading OPS logo for Solutions Site"
Write-Host $filler -ForegroundColor Yellow
Write-Log -Level Info -Message $filler
$WebClient.DownloadFile( $UrlSiteImage, $PathImage )
}
#Apply provisioning xml to new site collection
$filler = "Applying configuration changes..."
Write-Host $filler -ForegroundColor Yellow
Write-Log -Level Info -Message $filler
Apply-PnPProvisioningTemplate -path $Path -ExcludeHandlers SiteSecurity, Pages
$licenseList = Get-PnPList -Identity "Licenses"
$licenseListId = $licenseList.ID
$licenseListId = $licenseListId.ToString()
$filler = "Applying Site Security and Page changes separately..."
Write-Host $filler -ForegroundColor Yellow
Write-Log -Level Info -Message $filler
Start-Sleep -Seconds 2
Try {
Apply-PnPProvisioningTemplate -path $Path -Handlers SiteSecurity, Pages -Parameters @{"licenseListID"=$licenseListId;"site"=$SolutionsSiteUrl;"version"="16.0.0.0"}
}
Catch {
$filler = "Issue deploying SiteSecurity and Pages, likely running SharePoint 2013, retrying template deployment with fix..."
Write-Log -Level Warn -Message $filler
Apply-PnPProvisioningTemplate -path $Path -Handlers SiteSecurity, Pages -Parameters @{"licenseListID"=$licenseListId;"site"=$SolutionsSiteUrl;"version"="15.0.0.0"}
}
$filler = "Provisioning complete!"
Write-Host $filler -ForeGroundColor Green
Write-Log -Level Info -Message $filler
$stage = "Stage 3/3 - License Item creation"
Write-Host "`n$stage`n" -ForegroundColor Yellow
Write-Progress -Activity "Solutions Site Deployment" -CurrentOperation $stage -PercentComplete (100)
$filler = "Creating License Item..."
Write-Host $filler -ForegroundColor Yellow
Write-Log -Level Info -Message $filler
$licenseItemCount = ((Get-PnPListItem -List "Licenses" -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>License</Value></Eq></Where></Query></View>").Count)
If ($licenseItemCount -eq 0) {
Add-PnPListItem -List "Licenses" -Values @{"Title" = "License"} | Out-Null
$filler = "License Item created!"
Write-Host "`n$filler" -ForegroundColor Green
Write-Log -Level Info -Message $filler
}
Else {
$filler = "License Item not created or is duplicate!"
Write-Host "`n$filler" -ForegroundColor Red
Write-Log -Level Warn -Message $filler
}
Write-Log -Level Info -Message "Solutions Site URL = $SolutionsSiteUrl"
Write-Log -Level Info -Message "License List URL = $LicenseListUrl"
Write-Log -Level Info -Message "License List ID = $licenseListId"
Write-Log -Level Info -Message "Uploading log file to $SolutionsSiteUrl/Shared%20Documents"
#This sets up a Custom Column Mapping list ready for use if required
If($null -eq (Get-PnPList -Identity 'Custom Column Mapping')) {
Write-Log -Level Info -Message "Creating Custom Column Mapping list for later use if required."
Try {
New-PnPList -Title 'Custom Column Mapping' -Template GenericList | Out-Null
Add-PnPField -List 'Custom Column Mapping' -DisplayName 'From Column' -InternalName 'From Column' -Type Text -Required -AddToDefaultView | Out-Null
Set-PnPField -List 'Custom Column Mapping' -Identity 'From Column' -Values @{Description = "This is the field (by internal name) you want to map from to an existing field" }
Add-PnPField -List 'Custom Column Mapping' -DisplayName 'To Column' -InternalName 'To Column' -Type Text -Required -AddToDefaultView | Out-Null
Set-PnPField -List 'Custom Column Mapping' -Identity 'To Column' -Values @{Description = "This is the field (by internal name) you want to map to. This should already exist" }
Set-PnPField -List 'Custom Column Mapping' -Identity 'Title' -Values @{Title = "Scope"; DefaultValue = "Global" }
}
Catch {
Write-Log -Level Warn -Message "Issue creating Custom Column Mapping List. May already exist."
}
}
Else {
Write-Log -Level Info -Message "Custom Column Mapping list already exists by name, skipping creation."
}
Try {
#workaround for a PnP bug
$logToSharePoint = Add-PnPfile -Path $script:LogPath -Folder "Shared Documents"
}
Catch {
$exType = $($_.Exception.GetType().FullName)
$exMessage = $($_.Exception.Message)
Write-Host "Caught an exception:" -ForegroundColor Red
Write-Host "Exception Type: $exType" -ForegroundColor Red
Write-Host "Exception Message: $exMessage" -ForegroundColor Red
Write-Log -Level Error -Message "Caught an exception. Exception Type: $exType"
Write-Log -Level Error -Message $exMessage
}
Write-Progress -Activity "Completed" -Completed
Write-Host "`nPlease record the OnePlace Solutions Site URL and License Location / License List URL for usage in the OnePlaceMail Desktop and OnePlaceDocs clients, and the License List Id for the licensing process. " -ForegroundColor Yellow
Write-Host "`nThese have also been written to a log file at '$script:logPath', and '$SolutionsSiteUrl/Shared%20Documents/$script:logFile'." -ForegroundColor Yellow
Write-Host "`n-------------------`n" -ForegroundColor Red
$importants = "Solutions Site URL = $SolutionsSiteUrl`nLicense List URL = $LicenseListUrl`nLicense List ID = $licenseListId"
Write-Host $importants
Write-Host "`n-------------------`n" -ForegroundColor Red
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
If($false -eq $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$input = Read-Host "Would you like to email this information and Log file to OnePlace Solutions now? (yes or no)"
$input = $input[0]
If($input -eq 'y') {
$file = Get-ChildItem $script:logPath
Send-OutlookEmail -attachment $file.FullName -body $importants
}
}
Else {
Write-Host "Script is run as Administrator, cannot compose email details. Please email the above information and the log file generated to '[email protected]'." -ForegroundColor Yellow
}
Write-Host "Opening Solutions Site at $SolutionsSiteUrl..." -ForegroundColor Yellow
Pause
Start-Process $SolutionsSiteUrl | Out-Null
}
Catch [Microsoft.SharePoint.Client.ServerException] {
$exMessage = $($_.Exception.Message)
If($exMessage -match 'A site already exists at url') {
Write-Host $exMessage -ForegroundColor Red
Write-Log -Level Error -Message $exMessage
If($solutionsSite -ne 'oneplacesolutions'){
Write-Host "Please run the script again and choose a different Solutions Site suffix." -ForegroundColor Red
}
}
Else {
Throw $_
}
}
Catch {
Throw $_
}
}
Catch {
$exType = $($_.Exception.GetType().FullName)
$exMessage = $($_.Exception.Message)
write-host "Caught an exception:" -ForegroundColor Red
write-host "Exception Type: $exType" -ForegroundColor Red
write-host "Exception Message: $exMessage" -ForegroundColor Red
Write-Log -Level Error -Message "Caught an exception. Exception Type: $exType, Message: $exMessage"
Pause
}
Finally {
Try {
Disconnect-PnPOnline
}
Catch {}
Write-Log -Level Info -Message "End of script."
}