powershell - Powershell 将文件上传到 SharePoint:使用“0”参数错误调用“ExecuteQuery”的异常
问题描述
我想将所有 .csv 文件从 C:/ 上传到 SharePoint。该脚本运行到 $Ctx.ExecuteQuery() 并返回此错误。我能够在站点上运行其他 PowerShell 脚本,并且是站点所有者,我怀疑这不是访问权限。
我从哪里开始寻找?
使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(403) Forbidden。” + $Ctx.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
Get-ChildItem "c:\users\xxx\downloads" -Filter *.csv | % {
$eachFile =Get-ChildItem "c:\users\xxxx\downloads\" -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-Object 之前。
推荐阅读
- swift - UITapGestureRecognizer 在 ios13.1 中的 PDFView 上没有响应
- arrays - 如何在打字稿中比较对象中的多个值
- svelte - 无法使用 Sapper 导出生成动态路由
- typescript - TypeScript:函数重载的错误参数错误——TS 限制?
- docker - curl命令参数上的YAML解析错误
- c# - 我用 C# 过滤了 Excel 工作表,修改它以一次选择和过滤多个值
- python - 查找四行的均值、计数和标准差
- python-3.x - 保存模型、加载模型并继续训练的最佳方式是什么?
- c - 无法获得正确的纳秒时间间隔
- c# - 尝试向表中添加行时无法将 Int32 转换为 null