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

求助:如何下载大型只读SharePoint压缩文件库(约1TB)

解决方案与增量下载脚本实现

可行方案

1. PnP PowerShell 批量下载

这是适配只读权限场景的最优方案,无需关联OneDrive,直接通过PowerShell调用SharePoint API遍历文件并下载,默认支持100MB以上大文件的分片下载。

2. SharePoint Online Management Shell 辅助

若PnP模块安装存在障碍,也可使用原生的SharePoint Online管理模块,虽灵活性稍弱,但能满足基础批量下载需求。

增量下载脚本(PnP PowerShell)

以下脚本会记录已下载文件的最后修改时间,每次运行仅下载新增或更新的文件,同时完整保留原库的目录结构:

# 配置参数
$siteUrl = "你的SharePoint站点链接"
$libraryName = "目标库名称"
$localDownloadPath = "本地保存路径(例如:D:\SPDownloads)"
$logFilePath = Join-Path $localDownloadPath "DownloadLog.txt"

# 初始化日志文件
if (-not (Test-Path $logFilePath)) {
    New-Item -Path $logFilePath -ItemType File | Out-Null
}

# 加载已下载文件记录
$downloadedFiles = @{}
Get-Content $logFilePath | ForEach-Object {
    $parts = $_ -split "`t"
    if ($parts.Count -eq 2) {
        $downloadedFiles[$parts[0]] = [DateTime]$parts[1]
    }
}

# 连接到SharePoint站点
Connect-PnPOnline -Url $siteUrl -Interactive

# 获取库内所有文件(包含子文件夹)
$files = Get-PnPListItem -List $libraryName -PageSize 500 -Fields FileLeafRef, FileDirRef, Modified

# 遍历文件并处理下载
foreach ($file in $files) {
    $serverRelativeUrl = $file["FileDirRef"] + "/" + $file["FileLeafRef"]
    $lastModified = $file["Modified"]
    $localFilePath = Join-Path $localDownloadPath ($serverRelativeUrl -replace "^/[^/]+/", "")

    # 检查是否需要下载
    if (-not $downloadedFiles.ContainsKey($serverRelativeUrl) -or $downloadedFiles[$serverRelativeUrl] -lt $lastModified) {
        # 创建本地目录
        $localDir = Split-Path $localFilePath -Parent
        if (-not (Test-Path $localDir)) {
            New-Item -Path $localDir -ItemType Directory -Force | Out-Null
        }

        # 下载文件(自动处理大文件分片)
        Write-Host "正在下载: $serverRelativeUrl"
        Get-PnPFile -Url $serverRelativeUrl -Path $localDir -FileName $file["FileLeafRef"] -AsFile -Force

        # 更新日志
        "$serverRelativeUrl`t$lastModified" | Out-File $logFilePath -Append
    } else {
        Write-Host "文件已最新,跳过: $serverRelativeUrl"
    }
}

# 断开连接
Disconnect-PnPOnline

使用说明

  1. 先安装PnP PowerShell模块:Install-Module -Name PnP.PowerShell -Force -AllowClobber
  2. 替换脚本中的$siteUrl、$libraryName、$localDownloadPath为实际值
  3. 首次运行会下载所有文件,后续运行仅处理新增或修改过的文件
  4. 只读权限足够运行此脚本,全程仅涉及读取文件列表和下载操作

注意事项

  • 若文件数量达上万级,可调整-PageSize参数,或添加分页逻辑优化性能
  • 大文件下载时确保网络稳定,脚本会自动重试分片下载失败的部分
  • 日志文件DownloadLog.txt请勿手动修改,否则会破坏增量判断逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:41:03