我想使用 powershell 脚本将一些共享点文档复制到 Azure 存储容器。
我是 azure 新手。需要这方面的帮助。
将文档从 SharePoint 库复制到 Azure 存储容器
您可以使用下面的脚本通过 PowerShell 将文件从 SharePoint 复制到 Azure Blob 存储。
脚本:
$SiteURL = "https://<tenantname>.sharepoint.com/sites/<sitename>"
$FileServerRelativeURL = "/sites/<sitename>/Shared%20Documents/<files>"
$DestinationFolder ="C:\demo"
Try {
#Check if the Destination Folder exists. If not, create the folder for targetfile
If(!(Test-Path -Path $DestinationFolder))
{
New-Item -ItemType Directory -Path $DestinationFolder | Out-Null
Write-host -f Yellow "Created a New Folder '$DestinationFolder'"
}
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Check if File exists
$File = Get-PnPFile -Url $FileServerRelativeURL -ErrorAction SilentlyContinue
If($File -ne $Null)
{
#Download file from sharepoint online
Get-PnPFile -Url $FileServerRelativeURL -Path $DestinationFolder -Filename $File.Name -AsFile -Force
Write-host "File Downloaded Successfully!" -f Green
}
Else
{
Write-host "Could not Find File at "$FileServerRelativeURL -f Red
}
}
Catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
$account_name="venkat789"
$account_key="xxxx"
$context = New-AzStorageContext -StorageAccountName $account_name -StorageAccountKey $account_key
$containerName = "test"
$files = Get-ChildItem -Path $DestinationFolder -File -Recurse
$files | Set-AzStorageBlobContent -Container $containerName -Context $context
# Delete the local file
Remove-Item $DestinationFolder -Recurse -Force
上面的代码从 SharePoint 站点下载文件到本地临时文件夹,然后将其上传到 Azure Blob 存储容器并删除本地目录中的临时文件夹。
输出:
File Downloaded Successfully!
AccountName: venkat789, ContainerName: test
Name BlobType Length ContentType LastModified AccessTier SnapshotTime IsDeleted VersionId
---- -------- ------ ----------- ------------ ---------- ------------ --------- ---------
mountainview.png BlockBlob 51548 application/octet-stream 2024-03-19 07:07:19Z Hot False
传送门: