You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求协助:使用PowerShell脚本将SharePoint文档复制到Azure Storage Container

SharePoint文档库文档复制到Azure存储容器的PowerShell脚本

前置准备

  • 安装所需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

使用说明

  1. 将脚本顶部的配置参数替换为你的实际信息
  2. 以管理员身份打开PowerShell,执行脚本
  3. 按提示完成SharePoint账户身份验证,脚本会自动处理文件筛选、下载和上传流程

可选优化

  • 若需保留SharePoint文件夹结构,可解析FileRef字段中的路径,在存储容器中创建对应目录后再上传
  • 可改用Connect-AzAccount完成Azure身份验证,适配Azure AD权限场景

内容的提问来源于stack exchange,提问作者user3771062

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 23:47:15