/
Fix-WebParts.ps1
214 lines (193 loc) · 9.77 KB
/
Fix-WebParts.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
#Requires -Version 3.0
<#
Copyright (c) Alya Consulting, 2019-2024
This file is part of the Alya Base Configuration.
https://alyaconsulting.ch/Loesungen/BasisKonfiguration
The Alya Base Configuration is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Alya Base Configuration is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details: https://www.gnu.org/licenses/gpl-3.0.txt
Diese Datei ist Teil der Alya Basis Konfiguration.
https://alyaconsulting.ch/Loesungen/BasisKonfiguration
Die Alya Basis Konfiguration ist eine Freie Software: Sie können sie unter den
Bedingungen der GNU General Public License, wie von der Free Software
Foundation, Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
veröffentlichten Version, weiter verteilen und/oder modifizieren.
Die Alya Basis Konfiguration wird in der Hoffnung, dass sie nützlich sein wird,
aber OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
Siehe die GNU General Public License fuer weitere Details:
https://www.gnu.org/licenses/gpl-3.0.txt
#>
Start-Transcript -Path "$PSScriptRoot\..\logs\Fix-WebParts-$(get-date -Format 'yyyyMMddhhmmss').txt"
Write-Output "Enviroment setup"
$webApplication = "webAppName1"
$migrateAll = $false
$sharepointUrl = "https://alyaconsulting.sharepoint.com"
$sharepointAdminUrl = "https://alyaconsulting-admin.sharepoint.com"
$sharepointSitesUrl = "$($sharepointUrl)/sites"
$sharePointEnvSuffix = ""
$sharepointAdmins = ("konrad.brunner@alyaconsulting.ch", "admin@alyaconsulting.onmicrosoft.com", "AlyaSpAdmin@alyaconsulting.ch")
# Getting csom if not already present
function DownloadAndInstallCSOM($dir, $nuget, $nuvrs)
{
$fileName = "$PSScriptRoot\$nuget_" + $nuvrs + ".nupkg"
Invoke-WebRequest -Uri $nusrc.href -OutFile $fileName
if (-not (Test-Path $fileName))
{
Write-Error "Was not able to download $nuget which is a prerequisite for this script"
break
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($fileName, "$PSScriptRoot\$dir")
Remove-Item $fileName
}
function PrepareCSOM($dir, $nuget)
{
$resp = Invoke-WebRequest –Uri "https://www.nuget.org/packages/$nuget"
$nusrc = ($resp).Links | Where-Object { $_.outerText -eq "Manual download" -or $_."data-track" -eq "outbound-manual-download"}
$nuvrs = $nusrc.href.Substring($nusrc.href.LastIndexOf("/") + 1, $nusrc.href.Length - $nusrc.href.LastIndexOf("/") - 1)
if (-not (Test-Path "$PSScriptRoot\$dir\lib\net45"))
{
DownloadAndInstallCSOM -dir $dir -nuget $nuget -nuvrs $nuvrs
}
else
{
# Checking CSOM version, updating if required
$nuspec = [xml](Get-Content "$PSScriptRoot\$dir\$nuget.nuspec")
if ($nuspec.package.metadata.version -ne $nuvrs)
{
Write-Host "There is a newer CSOM package available. Downloading and installing it."
Remove-Item -Recurse -Force "$PSScriptRoot\$dir"
DownloadAndInstallCSOM -dir $dir -nuget $nuget -nuvrs $nuvrs
}
}
}
if (-not $global:credLS4D) { $global:credLS4D = Get-Credential -Message "Enter Sharepoint password:" }
PrepareCSOM -dir "_csomOnline" -nuget "Microsoft.SharePointOnline.CSOM"
Add-Type -Path "$PSScriptRoot\_csomOnline\lib\net45\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$PSScriptRoot\_csomOnline\lib\net45\Microsoft.SharePoint.Client.Runtime.dll"
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($global:credLS4D.UserName, $global:credLS4D.Password)
#Reading input data
if (-Not (Test-Path "$PSScriptRoot\OnPremWebParts$($webApplication)$($webEnv).csv"))
{
Write-Host "Reading onprem WebParts"
& "$PSScriptRoot\Get-WebParts.ps1" -onprem $true
}
if (-Not (Test-Path "$PSScriptRoot\OnlineWebParts$($webApplication)$($webEnv).csv"))
{
Write-Host "Reading online WebParts"
& "$PSScriptRoot\getWebParts$($webApplication)$($webEnv).ps1" -onprem $false
}
Write-Host "Reading csv files"
$onpremWPs = Import-Csv -Path "$PSScriptRoot\OnPremWebParts$($webApplication)$($webEnv).csv" -Encoding UTF8
$onlineWPs = Import-Csv -Path "$PSScriptRoot\OnlineWebParts$($webApplication)$($webEnv).csv" -Encoding UTF8
#Calcluating changes
Write-Host "Compairing"
$migSites = Import-Csv -Delimiter "," -encoding UTF8 $PSScriptRoot\..\setupSites.csv
if ([string]::IsNullOrEmpty($migSites[0].DstCol))
{
$migSites = Import-Csv -Delimiter ";" -encoding UTF8 $PSScriptRoot\..\setupSites.csv
}
if ([string]::IsNullOrEmpty($migSites[0].DstCol))
{
Write-Error "Wrong delimiter found."
exit
}
$migSites | Where-Object { ( $migrateAll -or $_.Command.ToLower() -eq "copy" ) -and $_.WebApplication -eq $webApplication } | Foreach-Object {
if ([string]::IsNullOrEmpty($_.DstUrl))
{
$fullUrl = "$($sharepointSitesUrl)/$($_.DstCol)$($sharePointEnvSuffix)"
$alias = "$($_.DstCol)$($sharePointEnvSuffix)"
}
else
{
$fullUrl = "$($sharepointSitesUrl)/$($_.DstCol)$($sharePointEnvSuffix)-$($_.DstUrl)"
$alias = "$($_.DstCol)$($sharePointEnvSuffix)-$($_.DstUrl)"
}
$fullSrcUrl = "https://$($webApplication).alyaconsulting.ch$($_.SrcUrl)"
Write-Host " Site $($fullUrl)"
Write-Host " with data from $($fullSrcUrl)"
$onpremWebs = $onpremWPs | Where-Object { $_.Site -eq $fullSrcUrl } | Select-Object -ExpandProperty Web -Unique
foreach ($onpremWeb in $onpremWebs)
{
$onpremPages = $onpremWPs | Where-Object { $_.Site -eq $fullSrcUrl -and $_.Web -eq $onpremWeb } | Select-Object -ExpandProperty Page -Unique
foreach ($onpremPage in $onpremPages)
{
$onlineWeb = $onpremWeb
if ($_.SrcUrl.length -gt 0) {
$onlineWeb = $onlineWeb.Replace($_.SrcUrl,"")
}
if ($onlineWeb -eq "/") {
$onlineWeb = ""
}
$onlineWeb = "/sites/" + $alias + $onlineWeb
Write-Host " Web $($onlineWeb)"
$onlinePage = $onpremPage.Replace($onpremWeb,$onlineWeb)
Write-Host " Page $($onlinePage)"
$onpremPageWPs = $onpremWPs | Where-Object { $_.Site -eq $fullSrcUrl -and $_.Page -eq $onpremPage }
foreach ($onpremPageWP in $onpremPageWPs)
{
Write-Host " WP $($onpremPageWP.WPTitle)"
$onlinePageWPFnd = $onlineWPs | Where-Object { $_.Site -eq $fullUrl -and $_.Page -eq $onlinePage -and $_.WPTitle -eq $onpremPageWP.WPTitle -and $_.WPHidden -eq $onpremPageWP.WPHidden -and $_.WPDisplayName -eq $onpremPageWP.WPDisplayName }
if ($onlinePageWPFnd.Count -gt 1)
{
Write-Host " *** $($onlinePageWPFnd.Count) duplicates found" -ForegroundColor Red
Write-Host " *** Onprem: $($onpremPage)"
Write-Host " *** Online $($onlinePage)"
}
if ($onlinePageWPFnd.Count -eq 0)
{
Write-Host " *** not found in export!" -ForegroundColor Red
Write-Host " *** Onprem: $($onpremPage)"
Write-Host " *** Online $($onlinePage)"
}
foreach ($onlinePageWP in $onlinePageWPFnd)
{
$toChange = $false
if ($onlinePageWP.WPChromeState -ne $onpremPageWP.WPChromeState) { $toChange = $true }
if ($onlinePageWP.WPChromeType -ne $onpremPageWP.WPChromeType) { $toChange = $true }
if ($toChange)
{
Write-Host " *** Changing"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($sharepointUrl+$onlineWeb)
$ctx.credentials = $creds
$ctx.load($ctx.Web)
$ctx.executeQuery()
$page = $ctx.Web.GetFileByServerRelativeUrl($onlinePage)
$ctx.load($page)
$ctx.executeQuery()
$webPartManager = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$ctx.Load($webPartManager)
$ctx.Load($webPartManager.WebParts)
$ctx.ExecuteQuery()
$fnd = $false
foreach($webPart in $webPartManager.WebParts)
{
$ctx.Load($webPart.WebPart.Properties)
$ctx.ExecuteQuery()
if ($webPart.WebPart.Properties["Title"] -eq $onpremPageWP.WPTitle)
{
$webPart.WebPart.Properties["ChromeState"] = [int]$onpremPageWP.WPChromeState
$webPart.WebPart.Properties["ChromeType"] = [int]$onpremPageWP.WPChromeType
$webPart.SaveWebPartChanges()
$ctx.ExecuteQuery()
$fnd = $true
break
}
}
if (-Not $fnd)
{
Write-Host " *** webpart not found on page!" -ForegroundColor Red
}
}
}
}
}
}
}
Stop-Transcript