Skip to content

Commit 497b3ec

Browse files
committed
Seperated original from improved version
1 parent 9428e5e commit 497b3ec

File tree

1 file changed

+88
-1
lines changed
  • scripts/spo-move-files-library-sites

1 file changed

+88
-1
lines changed

scripts/spo-move-files-library-sites/README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Large enterprises frequently need to migrate or replicate subsets of files betwe
1515
- **Auditable Execution:** Generates daily log files for compliance and troubleshooting.
1616
- **Environment Segregation:** Supports controlled movement of sample or test files between environments.
1717

18-
# [PnP PowerShell](#tab/pnpps)
18+
# [PnP PowerShell Updated](#tab/pnppsv2)
1919

2020
```PowerShell
2121
param (
@@ -177,6 +177,93 @@ Write-Log "==== Script completed ====" Cyan
177177
178178
179179
180+
```
181+
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
182+
183+
# [PnP PowerShell](#tab/pnpps)
184+
185+
```PowerShell
186+
187+
param (
188+
[Parameter(Mandatory=$false)]
189+
[string]$SourceSiteUrl = "https://contoso.sharepoint.com/teams/app",
190+
[Parameter(Mandatory=$false)]
191+
[string]$SourceFolderPath= "https://contoso.sharepoint.com/teams/app/Temp Library/test",
192+
[Parameter(Mandatory=$false)]
193+
[string]$DestinationSiteUrl = "https://contoso.sharepoint.com/teams/t-app",
194+
[Parameter(Mandatory=$false)]
195+
[string]$DestinationFolderPath = "https://contoso.sharepoint.com/teams/t-app/TempLibrary/test"
196+
)
197+
198+
# Generate a unique log file name using today's date
199+
$todayDate = Get-Date -Format "yyyy-MM-dd"
200+
$logFileName = "CopyFilesToSharePoint_$todayDate.log"
201+
$logFilePath = Join-Path -Path $PSScriptRoot -ChildPath $logFileName
202+
203+
# Connect to the source and destination SharePoint sites
204+
Connect-PnPOnline -Url $SourceSiteUrl -Interactive
205+
$SourceConn = Get-PnPConnection
206+
Connect-PnPOnline -Url $DestinationSiteUrl -Interactive
207+
$DestConn = Get-PnPConnection
208+
# Function to copy files recursively and log errors
209+
function Copy-FilesToSharePoint {
210+
param (
211+
[string]$SourceFolderPath,
212+
[string]$DestinationFolderPath
213+
)
214+
$sourceRelativeFolderPath = $SourceFolderPath.Replace($SourceSiteUrl,'')
215+
$sourceFiles = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceRelativeFolderPath -ItemType File -Connection $SourceConn
216+
foreach ($file in $sourceFiles) {
217+
$relativePath = $file.ServerRelativePath
218+
219+
# Check if the destination folder exists
220+
$destinationFolder = Get-PnPFolder -Url $DestinationFolderPath -Connection $DestConn -ErrorAction SilentlyContinue
221+
if ($null -eq $destinationFolder) {
222+
$errorMessage = "Error: Destination folder '$DestinationFolderPath' does not exist."
223+
Write-Host $errorMessage -ForegroundColor Red
224+
Add-Content -Path $logFilePath -Value $errorMessage
225+
continue
226+
}
227+
228+
try {
229+
#get file as stream
230+
$fileUrl = $SourceFolderPath + "/" + $file.Name
231+
$p = $fileUrl.Replace($SourceSiteUrl,'')
232+
$streamResult = Get-PnPFile -Url $p -Connection $SourceConn -AsMemoryStream
233+
# Upload the file to the destination folder
234+
$uploadedFile = Add-PnPFile -Folder $DestinationFolderPath -FileName $file.Name -Stream $streamResult -Values @{"ProcessStatus" = "Pending"} -Connection $DestConn #-ErrorAction St
235+
236+
Write-Host "File '$($file.Name)' copied and status set to 'Pending' in '$DestinationFolderPath'" -ForegroundColor Green
237+
} catch {
238+
$errorMessage = "Error copying file '$($file.Name)' to '$DestinationFolderPath': $($_.Exception.Message)"
239+
Write-Host $errorMessage -ForegroundColor Red
240+
Add-Content -Path $logFilePath -Value $errorMessage
241+
}
242+
}
243+
}
244+
245+
246+
# Call the function to copy files to SharePoint
247+
$sourceRelativeFolderPath = $SourceFolderPath.Replace($SourceSiteUrl,'')
248+
$sourceLevel1Folders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceRelativeFolderPath -ItemType Folder -Connection $SourceConn
249+
Copy-FilesToSharePoint -SourceFolderPath $SourceFolderPath -DestinationFolderPath $DestinationFolderPath
250+
$sourceLevel1Folders | ForEach-Object {
251+
$sourceLevel1Folder = $_
252+
if($_.Name -ne "Forms"){
253+
$sourcePath = $SourceFolderPath + "/" + $sourceLevel1Folder.Name
254+
$destPath = $DestinationFolderPath + "/" + $sourceLevel1Folder.Name
255+
Copy-FilesToSharePoint -SourceFolderPath $sourcePath -DestinationFolderPath $destPath
256+
}
257+
$sourceLevel1Path = $sourceRelativeFolderPath + "/" + $_.Name
258+
$sourceLevel2Folders = Get-PnPFolderItem -FolderSiteRelativeUrl $sourceLevel1Path -ItemType Folder -Connection $SourceConn
259+
$sourceLevel2Folders | ForEach-Object {
260+
$sourceLevel2Folder = $_
261+
$sourcePath = $SourceFolderPath + "/" + $sourceLevel1Folder.Name + "/" + $sourceLevel2Folder.Name
262+
$destPath = $DestinationFolderPath + "/" + $sourceLevel1Folder.Name + "/" + $sourceLevel2Folder.Name
263+
Copy-FilesToSharePoint -SourceFolderPath $sourcePath -DestinationFolderPath $destPath
264+
}
265+
}
266+
# Disconnect from SharePoint
180267
```
181268
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
182269
***

0 commit comments

Comments
 (0)