简体   繁体   English

Powershell 将文件上传到 SharePoint:异常调用带有“0”参数的“ExecuteQuery”错误

[英]Powershell uploading files to SharePoint : Exception calling “ExecuteQuery” with “0” argument(s) error

I want to upload all.csv files from a C:/ to SharePoint.我想将所有.csv 文件从 C:/ 上传到 SharePoint。 The script runs up to $Ctx.ExecuteQuery() and returns this error.该脚本运行到 $Ctx.ExecuteQuery() 并返回此错误。 I am able to run other PowerShell scripts on the site, and am the site owner, I suspect it is something other than access privileges.我能够在站点上运行其他 PowerShell 脚本,并且是站点所有者,我怀疑这不是访问权限。

Where do I start looking?我从哪里开始寻找?

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(403) Forbidden。” + $Ctx.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo: NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId: WebException + $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!"

        }
        }

Solution was to remove the Credentials to before the ForEach-Object.解决方案是将凭据删除到 ForEach-Object 之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“1”参数调用“SetAccessRule”的异常::: Powershell 错误 - Exception calling "SetAccessRule" with "1" argument(s) ::: Powershell Error 复制文件夹时出错! 使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(400) 错误请求。” - Error Copying the Folder! Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (400) Bad Request." 使用“0”参数调用“ExecuteQuery”的异常:“远程服务器返回错误:(401)未经授权。” - Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error: (401) Unauthorized.” Powershell 错误:使用“2”参数调用“批准”的异常:值不能是 null - Powershell Error: Exception Calling “Approve” with “2” argument(s): Value cannot be null Powershell:使用“ 3”参数调用“ AttachDatabase”的异常 - Powershell: Exception calling “AttachDatabase” with “3” argument(s) PowerShell:使用“7”参数调用“GetListItems”异常 - PowerShell: Exception calling “GetListItems” with “7” argument(s) Windows Server 2012 RC PowerShell错误:使用“ 0”参数调用“ SetInfo”的异常:“指定的网络 - Windows Server 2012 RC PowerShell Error: Exception calling “SetInfo” with “0” argument(s): "The specified network 使用“0”参数调用“ExecuteReader”的异常:“错误 [57014] - Exception calling "ExecuteReader" with "0" argument(s): "ERROR [57014] Powershell Invoke-SSHCommand:使用“1”参数调用“EndExecute”的异常 - Powershell Invoke-SSHCommand: Exception calling “EndExecute” with “1” argument(s) PowerShell 异常从另一个函数中使用“0”参数调用“Start” - PowerShell Exception calling “Start” with “0” argument(s) from within another function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM