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

如何用PowerShell无需凭据下载SharePoint公共共享文件夹?

无需凭据下载SharePoint公共共享文件夹的方法

你用的PnP PowerShell脚本会要求输入凭据,是因为这个模块本来就是给已授权的SharePoint站点操作设计的——哪怕文件夹已经公开共享,它还是会强制走身份验证流程。要实现无凭据下载,得利用SharePoint公开共享内容的匿名访问特性,通过REST API或者直接解析共享链接的方式来获取文件并下载。

前提准备

  • 获取目标文件夹的直接公共共享链接:不是你提供的浏览器列表页面链接,需要在SharePoint里右键目标文件夹→共享→选择「任何人」权限,复制生成的专用共享链接
  • 确保本地PowerShell允许执行脚本:可临时运行Set-ExecutionPolicy RemoteSigned -Scope CurrentUser调整权限

实现脚本示例

以下脚本可递归下载公开共享文件夹下的所有文件及子文件夹:

# 替换为你的文件夹公共共享链接
$publicShareLink = "https://captainitriverside.sharepoint.com/sites/ITWorld/_layouts/15/guestaccess.aspx?e=XXX&token=XXX&folderid=XXX"

# 替换为本地下载路径
$localDownloadPath = "C:\Users\John\Desktop\SharePoint_Downloads"

# 解析共享链接中的站点基础URL和文件夹相对路径
$parsedUrl = [Uri]$publicShareLink
$siteBaseUrl = $parsedUrl.Scheme + "://" + $parsedUrl.Host + $parsedUrl.Segments[0] + $parsedUrl.Segments[1]
$folderRelativePath = ($parsedUrl.Query -split 'folderid=')[1] -replace '%2F', '/'

# 创建本地下载根目录
if (-not (Test-Path $localDownloadPath)) {
    New-Item -ItemType Directory -Path $localDownloadPath | Out-Null
}

# 递归下载文件夹内容的函数
function Download-SharePointPublicFolder {
    param(
        [string]$siteUrl,
        [string]$folderPath,
        [string]$localPath
    )

    # 匿名调用REST API获取当前文件夹下的所有文件
    $filesApiUrl = "$siteUrl/_api/web/GetFolderByServerRelativeUrl('$folderPath')/Files"
    try {
        $files = Invoke-RestMethod -Uri $filesApiUrl -Method Get -Headers @{Accept = "application/json;odata=nometadata"} -UseBasicParsing
    }
    catch {
        Write-Host "获取文件列表失败: $($_.Exception.Message)"
        return
    }

    # 遍历下载每个文件
    foreach ($file in $files.value) {
        $fileLocalPath = Join-Path $localPath $file.Name
        $fileDownloadUrl = "$siteUrl$($file.ServerRelativeUrl)"
        
        try {
            Invoke-WebRequest -Uri $fileDownloadUrl -OutFile $fileLocalPath -UseBasicParsing
            Write-Host "已下载: $fileLocalPath"
        }
        catch {
            Write-Host "下载失败 $($file.Name): $($_.Exception.Message)"
        }
    }

    # 匿名调用REST API获取当前文件夹下的子文件夹
    $foldersApiUrl = "$siteUrl/_api/web/GetFolderByServerRelativeUrl('$folderPath')/Folders"
    try {
        $folders = Invoke-RestMethod -Uri $foldersApiUrl -Method Get -Headers @{Accept = "application/json;odata=nometadata"} -UseBasicParsing
    }
    catch {
        Write-Host "获取子文件夹列表失败: $($_.Exception.Message)"
        return
    }

    # 递归下载子文件夹(跳过系统默认文件夹)
    foreach ($folder in $folders.value) {
        if ($folder.Name -notin @("Forms", "_cts")) {
            $subLocalPath = Join-Path $localPath $folder.Name
            if (-not (Test-Path $subLocalPath)) {
                New-Item -ItemType Directory -Path $subLocalPath | Out-Null
            }
            Download-SharePointPublicFolder -siteUrl $siteUrl -folderPath $folder.ServerRelativeUrl -localPath $subLocalPath
        }
    }
}

# 启动下载流程
Download-SharePointPublicFolder -siteUrl $siteBaseUrl -folderPath $folderRelativePath -localPath $localDownloadPath
Write-Host "全部下载任务完成!"

注意事项

  • 必须确保共享链接的权限是「任何人都可以访问,无需登录」,否则匿名请求会被拒绝
  • 若下载大文件时出错,可给Invoke-WebRequest添加-MaximumRedirection 10参数
  • 部分SharePoint租户可能限制匿名访问REST API,若脚本报错,需再次确认目标文件夹的共享权限设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 06:15:16