|
# ================================================================================================== # |
|
# Prerequisites |
|
# ================================================================================================== # |
|
# Install-Module SharePointPnPPowerShellOnline -Scope CurrentUser |
|
# Install-Module -Name Az -Repository PSGallery -Force -Scope CurrentUser |
|
# ================================================================================================== # |
|
|
|
|
|
# ================================================================================================== # |
|
# Parameters |
|
# ================================================================================================== # |
|
# Azure Storage Account |
|
$storageAccountName = "storageaccname" |
|
$storageAccountKey = "sampleKey61os31UtDZhScbvfGylQlXl3zw8965j2qhrDkhBQqNDRUx2Bz+c+AStSTcL1A==" |
|
$containerName = "containername" |
|
$AzureFolderPath = "Sample Folder A" # Folder within the container that will be used as a destination. |
|
|
|
|
|
# SharePoint |
|
$SharePointSiteUrl = "https://contoso.sharepoint.com/sites/it-sandbox" |
|
$SharePointFolderUrl = "/sites/it-sandbox/Shared Documents/Sample Folder A" |
|
$SharePointLibraryName = "Documents" |
|
|
|
|
|
Connect-PnPOnline -Url $SharePointSiteUrl -UseWebLogin |
|
|
|
# Get a storage context |
|
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey |
|
$Container = Get-AzStorageContainer -Name $containerName -Context $ctx |
|
|
|
# Function to upload file to Azure Blob Storage using a stream |
|
Function Upload-FileToBlob($fileUrl, $folderPath, $fileName) { |
|
# Get the file content from SharePoint |
|
$fileMemoryStream = Get-PnPFile -Url $fileUrl -AsMemoryStream |
|
|
|
# Create the blob path including folder structure |
|
$blobName = "$folderPath/$fileName" |
|
|
|
# Upload the stream to Azure Blob Storage |
|
$BlockBlob = $Container.CloudBlobContainer.GetBlockBlobReference($blobName) |
|
$BlockBlob.UploadFromStream($fileMemoryStream); |
|
Write-Host "Uploaded file: $fileUrl to blob: $blobName" |
|
} |
|
|
|
|
|
# Function to recursively get all folders and files |
|
Function Move-FoldersAndFilesToBlobStorage($SharePointFolderUrl, $AzureFolderPath = "") { |
|
# Retrieve all items in the current folder |
|
$items = Get-PnPListItem -List $SharePointLibraryName -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Text'>$SharePointFolderUrl</Value></Eq></Where></Query></View>" |
|
|
|
foreach ($item in $items) { |
|
# Check if the item is a folder |
|
if ($item.FileSystemObjectType -eq "Folder") { |
|
# Create new folder path |
|
$newFolderPath = "$AzureFolderPath/$($item.FieldValues.FileLeafRef)" |
|
# Recurse into the folder |
|
Move-FoldersAndFilesToBlobStorage -SharePointFolderUrl $item.FieldValues.FileRef -AzureFolderPath $newFolderPath |
|
} else { |
|
# Upload the file to Azure Blob Storage |
|
Upload-FileToBlob -fileUrl $item.FieldValues.FileRef -folderPath $AzureFolderPath -fileName $item.FieldValues.FileLeafRef |
|
} |
|
} |
|
} |
|
|
|
# Start the recursion from the root of the library |
|
Move-FoldersAndFilesToBlobStorage -SharePointFolderUrl $SharePointFolderUrl -AzureFolderPath $AzureFolderPath |