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

使用Copy-PnPFile复制SharePoint文档遇权限拒绝问题求助

SharePoint Online: Access Denied When Copying Document Sets via PnP PowerShell

I'm getting an Access denied error when trying to copy document sets (including their .docx files) between two SharePoint Online document libraries.

Oddly, the script works without issues when using two newly created test libraries in the same site collection, but fails consistently on our production libraries.

I've already tried:

  • Using an app registration with FullControl permissions on the site collection
  • Running the script with a SharePoint account that has FullControl access across all site collections

Here's the code I'm using to copy a single document set by ID:

$clientId = "**************************"
$clientSecret = "**************************"

$siteUrl = "https://***********.sharepoint.com/sites/TransportClaimsDatabase/"
$SourceLibrary = "Claims (test)"
$DestinationLibrary = "Claims (Archive)"

# Define the source and destination URLs for the document set
$SourceUrl = "/sites/TransportClaimsDatabase/$SourceLibrary/$DocumentSetName"
$TargetUrl = "/sites/TransportClaimsDatabase/$DestinationLibrary/$DocumentSetName"

Write-Host "Connecting to $siteUrl" -ForegroundColor Yellow 
# Connect-PnPOnline -Url $siteUrl -UseWebLogin
Connect-PnPOnline -Url $siteURL -ClientId $clientId -ClientSecret $clientSecret

#####################################################################################################
$DocumentSetIDToCopy = "16" 

$DocumentSet = Get-PnPListItem -List $SourceLibrary -Id $DocumentSetIDToCopy

$DocumentSetName = $DocumentSet["FileLeafRef"]
$SourceUrl = "/sites/TransportClaimsDatabase/$SourceLibrary/$DocumentSetName"
$TargetUrl = "/sites/TransportClaimsDatabase/$DestinationLibrary/$DocumentSetName"

Write-Output "`nProcessing Document Set: $DocumentSetName with ID: $DocumentSetIDToCopy"
Write-Output "SourceUrl: $SourceUrl"
Write-Output "TargetUrl: $TargetUrl"

try {
    Copy-PnPFile -SourceUrl $SourceUrl -TargetUrl $TargetUrl -OverwriteIfAlreadyExists -Force
    Write-Output "Successfully copied $DocumentSetName to $DestinationLibrary."
    
}
catch {
    Write-Output "Failed to copy $DocumentSetName. Error details:"
    $_ | Format-List -Force
}
#####################################################################################################

# Disconnect
Disconnect-PnPOnline
Write-Output "Document Sets have been successfully moved from $SourceLibrary to $DestinationLibrary."

I need help figuring out why this access denied error only happens on the production libraries and how to fix it.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 07:20:56