powershell - Azure 自动化 - 将文件从一个 Sharepoint 站点复制到另一个
问题描述
我想创建一个工作流,自动将上传到 Sharepoint 站点的文件复制到另一个 SharePoint 站点(用于与客户交换文件)。因此,我创建了一个逻辑应用程序,该应用程序触发具有以下内容的 Runbook:
param(
[Parameter (Mandatory = $true)][string]$FilePath,
[Parameter (Mandatory = $true)][string]$FileName
)
$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"
#Set Source and Destination File URL
$SourceFileURL="/sites/Sitename1/$Filepath/$FileName"
$TargetFileURL="/sites/Sitename2/$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)
无论如何,我一直收到以下错误:Cannot send a content-body with this verb-type
我该如何解决这个问题?有更好的方法吗?
解决方案
在 sharepoint 中,我们可以使用MoveCopyUtil 类在站点之间复制文件
例如:
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$Ctx.Credentials = $Credentials
#Copy the File
$MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
$Overwrite = $True
[Microsoft.SharePoint.Client.MoveCopyUtil]::CopyFile($Ctx, $SourceFileURL, $TargetFileURL, $Overwrite, $MoveCopyOpt)
$Ctx.ExecuteQuery()
推荐阅读
- android - Android 应用程序在长按编辑文本时崩溃
- mysql - VB.NET:如何修复给定路径的格式不支持错误
- ionic-framework - 离子底固定内容
- php - php和mysql上的INNER JOIN和foreach循环
- python - 从嵌套字典创建多个子字典
- c# - 桌面应用程序中的 SQL Server Windows 身份验证 - 连接错误
- asp.net - 为什么要创建 DBContext 的私有变量并将其初始化为类的公共构造函数?
- php - 502 Bad Gateway on fresh api-platform docker-compose
- apache - 即使在 ssl 设置之后,https 也不会提供内容,但 http 可以
- typescript - 使用新定义文件添加到现有库 typescript 类型