【问题标题】:PowerShell SharePoint Online ExecuteQuery Error - Timed OutPowerShell SharePoint Online ExecuteQuery 错误 - 超时
【发布时间】:2018-01-02 18:46:12
【问题描述】:

这个问题已经困扰我好几天了......它的工作时间超过一半,但在调用 ExecuteQuery() 命令时经常会给我这个错误:

使用“0”参数调用“ExecuteQuery”的异常:“操作已超时”

PowerShell:

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webURL) 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName,$Credentials.Password) 

$web = $ctx.Web  
$ctx.Load($web) 

#Retrieve Library
$list = $ctx.Web.Lists.GetByTitle($ITPurchaseRequestListName)
$ctx.load($list)

$files = $list.rootFolder.files
$ctx.load($files)
$ctx.executeQuery()

##########
if((test-path $tempPath) -eq $False)
{
    new-item -itemtype directory -path $temppath
}

foreach($file in $files)
{
    $ctx.load($file)
    $ctx.executeQuery()

    $fileRef = $file.ServerRelativeURL
    $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $fileRef);

    $fileName = $tempPath + $file.Name
    $fileStream = [System.IO.File]::Create($fileName)
    $fileInfo.Stream.CopyTo($fileStream);

    $fileStream.Close()

    start-sleep -seconds 2
}

如果我终止控制台会话并启动一个新的 PowerShell 窗口,它会运行几次,然后再次崩溃。有什么想法吗?

【问题讨论】:

    标签: sharepoint sharepoint-online csom


    【解决方案1】:

    我认为使用 foreach 语句调用 executeQuery() 对性能不利。 最好在foreach语句之外使用“Include”来加载文件对象的属性。

    参考网址

    https://msdn.microsoft.com/en-US/library/office/ee539350(v=office.14).aspx#Anchor_5

    【讨论】:

      【解决方案2】:

      除了其他 cmets 和建议之外,6sc.com 的人员还能够帮助我确定根本原因是 for 循环中缺少 stream.close()。

      foreach($file in $results)
      {
      
          $fileRef = $file["FileRef"];
          $fileName = $tempPath + $file["FileLeafRef"];
      
          $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $fileRef);
          $fileStream = [System.IO.File]::Create($fileName);
          $fileInfo.Stream.CopyTo($fileStream);
      
          $fileStream.Close();
          $fileInfo.Stream.Close();
      }
      

      【讨论】:

        【解决方案3】:

        向您的 PowerShell 脚本添加一个 TRY..CATCH 块,它将处理服务器超时,这将提供连续性执行。

        TRY...CATCH 的另一个好处是,您将能够获得异常的详细信息,并且可以在单独的日志文件中捕获,从而为您提供有关问题的更多线索。现在很难做出有根据的猜测,文件句柄可能正在使用,服务器 I/O,文件可能以独占模式打开,连接可能在短时间内中断,很难判断。

        使用 TRY..CATCH 捕获日期和时间 + 异常的详细信息将帮助您打开 Windows 事件查看器并准确找出发生了什么。

        【讨论】:

          【解决方案4】:

          我宁愿使用.OpenBinaryStream(),将其下载到内存中,然后保存到磁盘。

          代码:

          foreach($file in $files)
          {
              $data = $file.OpenBinaryStream();
              $ctx.Load($file);
              $ctx.ExecuteQuery();
          
              $memory = new-object system.io.MemoryStream
              $buffer = new-object system.byte[](1024*64)
              $pos = 0;
              while (($pos = $data.Value.Read($buffer, 0, $buffer.Length)) -gt 0)
              {
                  $memory.Write($buffer, 0, $pos);
              }
              $memory.Seek(0, [System.IO.SeekOrigin]::Begin);   
          
              $fileStream = [System.IO.File]::Create($fileName)
              $memory.CopyTo($fileStream)
              $memory.Close()
              $fileStream.Close()
          }
          

          【讨论】:

            【解决方案5】:

            包括$fileInfo.Stream.Close();,它会变魔术。

            【讨论】:

              猜你喜欢
              • 2020-09-16
              • 2021-07-22
              • 2023-03-19
              • 2017-12-23
              • 2021-01-10
              • 1970-01-01
              • 1970-01-01
              • 2015-02-06
              • 2017-11-04
              相关资源
              最近更新 更多