-
Notifications
You must be signed in to change notification settings - Fork 2
/
scriptUpgraded.ps1
122 lines (83 loc) · 4.11 KB
/
scriptUpgraded.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
Write-Output "started"
$saveDir = "D:\blog\provisioning\powershell"
$siteTitle ="";
$siteURL = "URL_OF_YOUR_SITE_SOURCE"
$destination = "URL_OF_YOUR_SITE_TARGET"
function ProcessFolder($folderUrl, $destinationFolder) {
Write-Output "Folder URL " $folderUrl " destinationFolder " $destinationFolder
$folder = Get-PnPFolder -RelativeUrl $folderUrl
$tempfiles = Get-PnPProperty -ClientObject $folder -Property Files
if (!(Test-Path -path $destinationfolder )) {
$dest = New-Item $destinationfolder -type directory
}
$total = $folder.Files.Count
$ctx = Get-PnPContext
For ($i = 0; $i -lt $total; $i++) {
$file = $folder.Files[$i]
$ctx.load($file.Versions)
$ctx.ExecuteQuery()
foreach($version in $file.Versions)
{
$filesplit = $file.Name.split(".")
$fullname = $filesplit[0]
$fileext = $filesplit[1]
$FullFileName = $fullname+"\"+$version.VersionLabel+"\"+$file.Name
$fileURL = $destination+"/"+$version.Url
$DownloadPath = $FullFileName
if (!(Test-Path ($destinationfolder + "\" + $fullname + "\" + $version.VersionLabel)))
{
New-Item ($destinationfolder + "\" + $fullname + "\" + $version.VersionLabel) -type directory -Force
}
HTTPDownloadFile "$fileURL" ($destinationfolder + "\" + $fullname + "\" + $version.VersionLabel + "\" + $file.Name)
$versionSourceFolder = "./" + $siteTitle + "/" + $folder.Name + "/" + $fullname + "/" + $version.VersionLabel + "/" + $file.Name
Add-PnPFileToProvisioningTemplate -Path ($saveDir + "Template.xml") -Source $versionSourceFolder -Folder $folderUrl -FileLevel Published
}
Get-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $destinationfolder -FileName $file.Name -AsFile -Force
Add-PnPFileToProvisioningTemplate -Path ($saveDir + "Template.xml") -Source ($destinationfolder + "\" + $file.Name) -Folder $folderUrl -FileLevel Published
}
}
function ProcessSubFolders($folders, $currentPath) {
foreach ($folder in $folders) {
$tempurls = Get-PnPProperty -ClientObject $folder -Property ServerRelativeUrl
#Avoid Forms folders
if ($folder.Name -ne "Forms") {
$targetFolder = $currentPath +"\"+ $folder.Name;
ProcessFolder $folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length) $targetFolder
$tempfolders = Get-PnPProperty -ClientObject $folder -Property Folders
ProcessSubFolders $tempfolders $targetFolder
}
}
}
function HTTPDownloadFile($ServerFileLocation, $DownloadPath)
{
$userName = "LOGIN_NAME"
$password = "PASSWORD"
#create secure password
$sPassword = $password | ConvertTo-SecureString -AsPlainText -Force
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $sPassword)
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$webclient.DownloadFile($ServerFileLocation,$DownloadPath)
}
Write-Output "source: $siteURL"
Write-Output "destination: $destination"
Write-Output "Connecting to: $siteURL"
Connect-PnPOnline -Url $siteURL
Write-Output "Connected!"
$web = Get-PnPWeb
$siteTitle = $web.Title
$saveDir = $saveDir + "\" + $siteTitle + "\"
Get-PnPProvisioningTemplate -Out $($saveDir + $pageTemplate) -Force -PersistBrandingFiles -PersistPublishingFiles -IncludeNativePublishingFiles -Handlers Navigation, Lists,PageContents, Pages, Files
$docLibs = Get-PNPList | Where-Object{$_.BaseTemplate -eq 101}
Write-Output "getting doc list"
foreach( $doc in $docLibs ){
if( $doc.Title -ne "Site Assets"){
#Download root files
ProcessFolder $doc.Title ($saveDir + $doc.Title)
#Download files in folders
$tempfolders = Get-PnPProperty -ClientObject $doc.RootFolder -Property Folders
ProcessSubFolders $tempfolders $($saveDir + $doc.Title) + "\"
}
}
Apply-PnPProvisioningTemplate -path ($saveDir + "Template.xml") -Handlers Navigation, Lists, Pages, Files -ClearNavigation
Write-Output "done"