Powershell 将文件上传到 SharePoint:异常调用"ExecuteQuery" "0"参数错误



我想将所有.csv文件从C:/上载到SharePoint。脚本运行到$Ctx.ExecuteQuery((并返回此错误。我能够在网站上运行其他PowerShell脚本,并且是网站所有者,我怀疑这不是访问权限。

我从哪里开始找?

使用"0"参数调用"ExecuteQuery"时发生异常:"远程服务器返回错误:(403(Forbidden。"+$Ctx.ExecuteQuery((+~~~~~~~~~~~~~~~~~~~+CategoryInfo:未指定:(:([],MethodInvocationException+FullyQualifiedErrorId:WebException


Get-ChildItem "c:usersxxxdownloads" -Filter *.csv | % {
$eachFile =Get-ChildItem "c:usersxxxxdownloads" -Filter *.csv
$eachFile | ForEach-Object{
#parameters
$SiteURL = "https://xxx.sharepoint.com/sites/nrg"
$LibraryName="Audit History"
$UserName = "xxxx@x.onmicrosoft.com"
$Password = "xx"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$Ctx.Credentials = $Credentials

#Get the Library
$Library =  $Ctx.Web.Lists.GetByTitle($LibraryName)
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $OutputFile)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $eachFile -leaf
#sharepoint online upload file powershell
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $SourceFileName
$FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
#powershell upload single file to sharepoint online
$Ctx.Load($FileUploaded)
$Ctx.ExecuteQuery()
#Close file stream
$FileStream.Close()
write-host "File has been uploaded!"
}
}

解决方案是在ForEach对象之前删除Credentials。

最新更新