请求协助:使用PowerShell脚本将SharePoint文档复制到Azure Storage Container
前置准备
- 安装所需PowerShell模块:
- 执行
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force -AllowClobber安装SharePoint Online管理模块 - 执行
Install-Module -Name Az.Storage -Force -AllowClobber安装Azure存储模块
- 执行
- 确保你拥有以下权限:
- SharePoint站点文档库的读取权限
- Azure存储容器的写入权限
完整脚本
# -------------------------- 配置参数 -------------------------- $siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite" # 替换为你的SharePoint站点URL $libraryName = "Documents" # 替换为目标文档库名称 $filterCondition = { $_.Name -like "*Report*.xlsx" } # 替换为筛选条件(如按文件名、创建日期筛选) $storageAccountName = "yourstorageaccount" # 替换为Azure存储账户名 $containerName = "targetcontainer" # 替换为目标存储容器名 $storageAccountKey = "yourstorageaccountkey" # 替换为存储账户密钥(或改用Azure AD身份验证) # ------------------------------------------------------------- # 1. 连接到SharePoint Online Connect-SPOService -Url $siteUrl # 2. 获取文档库中符合条件的文件 $library = Get-SPOList -Identity $libraryName $files = Get-SPOListItem -List $library | Where-Object $filterCondition # 3. 初始化Azure存储上下文 $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey # 4. 遍历文件并完成复制 foreach ($file in $files) { $fileUrl = $siteUrl + $file["FileRef"] $localTempPath = Join-Path -Path $env:TEMP -ChildPath $file["FileLeafRef"] try { # 下载SharePoint文件到本地临时目录 Invoke-WebRequest -Uri $fileUrl -OutFile $localTempPath -UseBasicParsing # 上传文件到Azure存储容器 Set-AzStorageBlobContent -File $localTempPath ` -Container $containerName ` -Blob $file["FileLeafRef"] ` -Context $context ` -Force Write-Host "成功复制文件: $($file["FileLeafRef"])" } catch { Write-Host "复制文件失败 $($file["FileLeafRef"]): $_" -ForegroundColor Red } finally { # 清理本地临时文件 if (Test-Path $localTempPath) { Remove-Item $localTempPath -Force } } } # 断开SharePoint连接 Disconnect-SPOService
使用说明
- 将脚本顶部的配置参数替换为你的实际信息
- 以管理员身份打开PowerShell,执行脚本
- 按提示完成SharePoint账户身份验证,脚本会自动处理文件筛选、下载和上传流程
可选优化
- 若需保留SharePoint文件夹结构,可解析
FileRef字段中的路径,在存储容器中创建对应目录后再上传 - 可改用
Connect-AzAccount完成Azure身份验证,适配Azure AD权限场景
内容的提问来源于stack exchange,提问作者user3771062
相关产品推荐
相关产品推荐

