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

Azure Automation跨SharePoint站点拷贝文件报错及优化方案咨询

SharePoint跨站点文件拷贝问题修复方案

现有代码报错修复

你遇到的Cannot send a content-body with this verb-type报错由多个可修复的代码问题导致,逐一对应修改即可:

  1. 基础语法错误:获取凭证的行缺少变量前缀$,导致后续凭证调用为空
  2. 路径规则不匹配:OpenBinaryDirect/SaveBinaryDirect方法要求传入对应ClientContext所属站点的内部相对路径,你当前传入的是SharePoint全局根路径,和Context绑定的站点不匹配,导致请求构造异常
  3. 缺少TLS 1.2协议声明:SharePoint Online强制要求TLS 1.2加密,默认PowerShell运行环境未启用该协议会触发请求异常

修复后的完整代码如下:

param(
    [Parameter (Mandatory = $true)][string]$FilePath,
    [Parameter (Mandatory = $true)][string]$FileName
)
# 启用TLS1.2,适配SharePoint Online要求
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$Client = "C:\Modules\User\Microsoft.SharePoint.Client\Microsoft.SharePoint.Client.dll"
$ClientRT = "C:\Modules\User\Microsoft.SharePoint.Client\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path $Client
Add-Type -Path $ClientRT

# 补全变量前缀$
$Creds = Get-AutomationPSCredential -Name "SharepointCreds"
 
#Set parameter values
$TargetSiteURL="https://domain.SharePoint.com/sites/site1"
$SourceSiteURL="https://domain.Sharepoint.com/sites/site2"
 
# 调整为站点内部相对路径,例如文件在源站点的"Shared Documents/xxx/yyy.txt",则对应填写"Shared Documents/$FilePath/$FileName"
$SourceFileURL="$FilePath/$FileName"
$TargetFileURL="$FilePath/$FileName"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName, $Creds.Password)
 
#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials
 
#Get the Source File
$FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceCtx, $SourceFileURL)

#Copy File to the Target location
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetCtx, $TargetFileURL, $FileInfo.Stream,$True)

更优实现方案

不需要通过Logic App触发Runbook的复杂架构,直接用更轻量的方案即可实现需求:

  • 方案1:纯Logic App实现(推荐)
    直接使用Logic App内置的SharePoint连接器,不需要维护任何代码:
    • 触发条件选择「当文件被创建时(属性仅)」,绑定源SharePoint站点的监控文件夹
    • 后续步骤添加「复制文件」操作,绑定目标SharePoint站点的存放路径即可
      原生连接器自动处理鉴权、重试、大文件分片、路径适配等逻辑,运维成本远低于自定义脚本。
  • 方案2:PnP PowerShell简化脚本实现
    如果必须使用脚本,可使用微软官方维护的PnP PowerShell模块,代码量减少70%,出错概率更低:
    param(
      [Parameter (Mandatory = $true)][string]$FilePath,
      [Parameter (Mandatory = $true)][string]$FileName
    )
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $Creds = Get-AutomationPSCredential -Name "SharepointCreds"
    $SourceSiteURL="https://domain.Sharepoint.com/sites/site2"
    $TargetSiteURL="https://domain.SharePoint.com/sites/site1"
    
    # 连接源站点
    Connect-PnPOnline -Url $SourceSiteURL -Credentials $Creds
    # 跨站点拷贝文件,-OverwriteIfAlreadyExists参数控制覆盖逻辑
    Copy-PnPFile -SourceUrl "$FilePath/$FileName" -TargetUrl "$FilePath/$FileName" -TargetSiteUrl $TargetSiteURL -OverwriteIfAlreadyExists
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 09:36:01