forked from pnp/custom-learning-office-365
-
Notifications
You must be signed in to change notification settings - Fork 0
/
M365lpConfiguration.ps1
258 lines (249 loc) · 13.7 KB
/
M365lpConfiguration.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
param([PSCredential]$Credentials,
[string]$TenantName,
[string]$SiteCollectionName,
[switch]$AppCatalogAdminOnly,
[switch]$SiteAdminOnly)
# Default to using PnP.PowerShell where possible - the script checks if legacy PnP PowerShell installed or prompts to install
$moduleUsed = "PnP.PowerShell"
if ($AppCatalogAdminOnly -and $SiteAdminOnly) {
Write-Host "Select either -AppCatalogAdminOnly or -SiteAdminOnly"
Write-Host "If you want to run both tenant and site admin parts, don't pass either parameter"
return
}
$AppCatalogAdmin = $AppCatalogAdminOnly
$SiteAdmin = $SiteAdminOnly
if (!($AppCatalogAdminOnly) -and !($SiteAdminOnly)) {
$AppCatalogAdmin = $true
$SiteAdmin = $true
}
#region Legal stuff for Telemetry
Write-Host "Microsoft collects active usage data from your organization’s use of Microsoft 365 learning pathways and the use of Microsoft’s online content. Microsoft will use this data to help improve the future Microsoft 365 learning pathways solutions. To learn more about Microsoft privacy policies see https://go.microsoft.com/fwlink/?LinkId=521839. If you would like to opt out of this data collection, please type Ctrl-C to stop this script and see Readme file (`"Disabling Telemetry Collection section`") for instructions on how to opt out.`n"
Read-Host "Press Enter to Continue"
$optInTelemetry = $true
#endregion
# verify the SharePointPnPPowerShellOnline module we need is installed
if (-not (Get-Module -ListAvailable -Name PnP.PowerShell )) {
if (-not (Get-Module -ListAvailable -Name SharePointPnPPowerShellOnline )) {
Write-Warning "Could not find the PnP.PowerShell or SharePointPnPPowerShellOnline module"
Write-Warning "Please install it and run this script again"
Write-Warning "You should use PnP.PowerShell with PowerShell 7 if possible and only use SharePointPnPPowerShellOnline if you are unable to register an application in Azure AD"
Write-Warning "You can install them it the following lines (use one or the other, not both):"
Write-Warning "`nInstall-Module PnP.PowerShell`n"
Write-Warning "`nInstall-Module SharePointPnPPowerShellOnline`n"
return
}
else {
Import-Module -Name SharePointPnPPowerShellOnline -DisableNameChecking
$moduleUsed = "SharePointPnPPowerShellOnline"
}
}
else {
Import-Module -Name PnP.PowerShell -DisableNameChecking
$moduleUsed = "PnP.PowerShell"
}
# Check if tenant name was passed in
while ([string]::IsNullOrWhitespace($TenantName)) {
# No TenantName was passed, prompt the user
$TenantName = Read-Host "Please enter your tenant name: (contoso) "
$TenantName = $TenantName.Trim()
$TestTenantURL = "https://$TenantName.sharepoint.com"
# Test that it's a mostly valid URL
# This doesn't catch everything
if (!([system.uri]::IsWellFormedUriString($TestTenantURL, [System.UriKind]::Absolute))) {
Write-Host "$TestTenantURL is not a valid URL." -BackgroundColor Black -ForegroundColor Red
Clear-Variable TenantName
}
}
# Check if $SiteCollectionName was passed in
if ([string]::IsNullOrWhitespace($SiteCollectionName) ) {
# No Site Collection was passed, prompt the user
$SiteCollectionName = Read-Host "Please enter your site collection name: (Press Enter for `'MicrosoftTraining`') "
if ([string]::IsNullOrWhitespace($SiteCollectionName)) {
$SiteCollectionName = "MicrosoftTraining"
}
}
$clSite = "https://$TenantName.sharepoint.com/sites/$SiteCollectionName"
try {
# If Credentials were passed, try them
if (-not [string]::IsNullOrWhitespace($Credentials)) {
if ($moduleUsed -eq "SharePointPnPPowerShellOnline") {
SharePointPnPPowerShellOnline\Connect-PnPOnline -Url $clSite -Credentials $Credentials -ErrorAction Stop
}
else {
PnP.PowerShell\Connect-PnPOnline -Url $clSite -Credentials $Credentials -ErrorAction Stop
}
}
else {
# If not, prompt for authentication. This supports MFA
if ($moduleUsed -eq "SharePointPnPPowerShellOnline") {
SharePointPnPPowerShellOnline\Connect-PnPOnline -Url $clSite -UseWebLogin -ErrorAction Stop
}
else {
PnP.PowerShell\Connect-PnPOnline -Url $clSite -Interactive -ErrorAction Stop
}
}
}
catch {
Write-Host "Failed to authenticate to $clSite"
Write-Host $_
return
}
#region Connect to Admin site.
if ($AppCatalogAdmin) {
# Need an App Catalog site collection defined for Set-PnPStorageEntity to work
if (!(Get-PnPTenantAppCatalogUrl)) {
Write-Warning "Tenant $TenantName must have an App Catalog site defined" -BackgroundColor Black -ForegroundColor Red
Write-Warning "Please visit https://social.technet.microsoft.com/wiki/contents/articles/36933.create-app-catalog-in-sharepoint-online.aspx to learn how, then run this setup script again"
Write-Host "`n"
Disconnect-PnPOnline
return
}
$appcatalog = Get-PnPTenantAppCatalogUrl
try {
# Test that user can write values to the App Catalog
Set-PnPStorageEntity -Key MicrosoftCustomLearningCdn -Value "https://pnp.github.io/custom-learning-office-365/learningpathways/" -Description "Microsoft 365 learning pathways CDN source" -ErrorAction Stop
}
catch {
# Get the username and
$user = ((Get-PnPConnection).PSCredential).username
Write-Warning "$user cannot write to App Catalog site" -BackgroundColor Black -ForegroundColor Red
Write-Warning "Please make sure they are a Site Collection Admin for $appcatalog"
Write-Warning $_
Disconnect-PnPOnline
return
}
Get-PnPStorageEntity -Key MicrosoftCustomLearningCdn
Set-PnPStorageEntity -Key MicrosoftCustomLearningSite -Value $clSite -Description "Microsoft 365 learning pathways Site Collection"
Get-PnPStorageEntity -Key MicrosoftCustomLearningSite
Set-PnPStorageEntity -Key MicrosoftCustomLearningTelemetryOn -Value $optInTelemetry -Description "Microsoft 365 learning pathways Telemetry Setting"
Get-PnPStorageEntity -Key MicrosoftCustomLearningTelemetryOn
if ($AppCatalogAdminOnly) {
Write-Host "`nTenant is configured. Run this script with the -SiteAdminOnly parameter to configure the site collection"
}
}
#endregion
#region Content stuff
if ($SiteAdmin) {
# Get the app
# Check for it at the tenant level first
$id = (Get-PnPApp | Where-Object -Property title -Like -Value "Microsoft 365 learning pathways").id
if ($null -ne $id) {
# Found the app in the tenant app catalog
# Install it to the site collection if it's not already there
Install-PnPApp -Identity $id -ErrorAction SilentlyContinue
}
else {
Write-Warning "Could not find `"Microsoft 365 learning pathways`" app. Please install in it your app catalog and run this script again."
break
}
$sitePagesList = Get-PnPList -Identity "Site Pages"
if ($null -ne $sitePagesList) {
# Delete pages if they exist. Alert user.
$clv = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningViewer.aspx</Value></Eq></Where></Query></View>"
if ($null -ne $clv) {
Write-Host "Found an existing CustomLearningViewer.aspx page. Deleting it."
# Renaming and moving to Recycle Bin to prevent potential naming overlap
Set-PnPListItem -List $sitePagesList -Identity $clv.Id -Values @{"FileLeafRef" = "CustomLearningViewer$((Get-Date).Minute)$((Get-date).second).aspx" }
Move-PnPListItemToRecycleBin -List $sitePagesList -Identity $clv.Id -Force
}
if ($moduleUsed -eq "SharePointPnPPowerShellOnline") {
# Now create the page whether it was there before or not
$clvPage = Add-PnPClientSidePage "CustomLearningViewer" # Will fail if user can't write to site collection
$clvSection = Add-PnPClientSidePageSection -Page $clvPage -SectionTemplate OneColumn -Order 1
# Before I try to add the Microsoft 365 learning pathways web parts verify they have been deployed to the site collection
$timeout = New-TimeSpan -Minutes 1 # wait for a minute then time out
$stopwatch = [diagnostics.stopwatch]::StartNew()
Write-Host "." -NoNewline
$WebPartsFound = $false
while ($stopwatch.elapsed -lt $timeout) {
if (Get-PnPAvailableClientSideComponents -page CustomLearningViewer.aspx -Component "Microsoft 365 learning pathways administration") {
Write-Host "Microsoft 365 learning pathways web parts found"
$WebPartsFound = $true
break
}
Write-Host "." -NoNewline
Start-Sleep -Seconds 10
}
# loop either timed out or web parts were found. Let's see which it was
if ($WebPartsFound -eq $false) {
Write-Warning "Could not find Microsoft 365 learning pathways Web Parts."
Write-Warning "Please verify the Microsoft 365 learning pathways Package is installed and run this installation script again."
break
}
Add-PnPClientSideWebPart -Page $clvPage -Component "Microsoft 365 learning pathways"
Set-PnPClientSidePage -Identity $clvPage -Publish
$clv = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningViewer.aspx</Value></Eq></Where></Query></View>"
$clv["PageLayoutType"] = "SingleWebPartAppPage"
$clv.Update()
Invoke-PnPQuery # Done with the viewer page
$cla = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningAdmin.aspx</Value></Eq></Where></Query></View>"
if ($null -ne $cla) {
Write-Host "Found an existing CustomLearningAdmin.aspx page. Deleting it."
# Renaming and moving to Recycle Bin to prevent potential naming overlap
Set-PnPListItem -List $sitePagesList -Identity $cla.Id -Values @{"FileLeafRef" = "CustomLearningAdmin$((Get-Date).Minute)$((Get-date).second).aspx" }
Move-PnPListItemToRecycleBin -List $sitePagesList -Identity $cla.Id -Force
}
$claPage = Add-PnPClientSidePage "CustomLearningAdmin" -Publish
$claSection = Add-PnPClientSidePageSection -Page $claPage -SectionTemplate OneColumn -Order 1
Add-PnPClientSideWebPart -Page $claPage -Component "Microsoft 365 learning pathways administration"
Set-PnPClientSidePage -Identity $claPage -Publish
$cla = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningAdmin.aspx</Value></Eq></Where></Query></View>"
$cla["PageLayoutType"] = "SingleWebPartAppPage"
$cla.Update()
Invoke-PnPQuery # Done with the Admin page
}
else {
# Now create the page whether it was there before or not
$clvPage = Add-PnPPage "CustomLearningViewer" # Will fail if user can't write to site collection
$clvSection = Add-PnPPageSection -Page $clvPage -SectionTemplate OneColumn -Order 1
# Before I try to add the Microsoft 365 learning pathways web parts verify they have been deployed to the site collection
$timeout = New-TimeSpan -Minutes 1 # wait for a minute then time out
$stopwatch = [diagnostics.stopwatch]::StartNew()
Write-Host "." -NoNewline
$WebPartsFound = $false
while ($stopwatch.elapsed -lt $timeout) {
if (Get-PnPPageComponent -page CustomLearningViewer.aspx -ListAvailable | Where-Object { $_.Name -eq "Microsoft 365 learning pathways administration" }) {
Write-Host "Microsoft 365 learning pathways web parts found"
$WebPartsFound = $true
break
}
Write-Host "." -NoNewline
Start-Sleep -Seconds 10
}
# loop either timed out or web parts were found. Let's see which it was
if ($WebPartsFound -eq $false) {
Write-Warning "Could not find Microsoft 365 learning pathways Web Parts."
Write-Warning "Please verify the Microsoft 365 learning pathways Package is installed and run this installation script again."
break
}
Add-PnPPageWebPart -Page $clvPage -Component "Microsoft 365 learning pathways"
Set-PnPPage -Identity $clvPage -Publish
$clv = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningViewer.aspx</Value></Eq></Where></Query></View>"
$clv["PageLayoutType"] = "SingleWebPartAppPage"
$clv.Update()
Invoke-PnPQuery # Done with the viewer page
$cla = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningAdmin.aspx</Value></Eq></Where></Query></View>"
if ($null -ne $cla) {
Write-Host "Found an existing CustomLearningAdmin.aspx page. Deleting it."
# Renaming and moving to Recycle Bin to prevent potential naming overlap
Set-PnPListItem -List $sitePagesList -Identity $cla.Id -Values @{"FileLeafRef" = "CustomLearningAdmin$((Get-Date).Minute)$((Get-date).second).aspx" }
Move-PnPListItemToRecycleBin -List $sitePagesList -Identity $cla.Id -Force
}
$claPage = Add-PnPPage "CustomLearningAdmin" -Publish
$claSection = Add-PnPPageSection -Page $claPage -SectionTemplate OneColumn -Order 1
Add-PnPPageWebPart -Page $claPage -Component "Microsoft 365 learning pathways administration"
Set-PnPPage -Identity $claPage -Publish
$cla = Get-PnPListItem -List $sitePagesList -Query "<View><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>CustomLearningAdmin.aspx</Value></Eq></Where></Query></View>"
$cla["PageLayoutType"] = "SingleWebPartAppPage"
$cla.Update()
Invoke-PnPQuery # Done with the Admin page
}
}
else {
Write-Warning "Could not find `"Site Pages`" library. Please make sure you are running this on a Modern SharePoint site"
return
}
}
Disconnect-PnPOnline # Disconnect from SharePoint Admin
Write-Host "Microsoft 365 learning pathways Pages created at $clSite"
#endregion