PowerShell SharePoint Online ExecuteQuery 错误 - 超时
PowerShell SharePoint Online ExecuteQuery Error - Timed Out
这个问题让我困惑了好几天...它有一半以上的时间都有效,但在调用 ExecuteQuery() 命令时经常会给我这个错误:
Exception calling "ExecuteQuery" with "0" argument(s): "The operation has timed out"
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 window,它将运行几次,然后再次退出。有什么想法吗?
将 TRY..CATCH 块添加到您的 PowerShell 脚本,它将处理服务器超时,这将提供连续执行。
TRY...CATCH 的另一个好处是,您将能够获得异常的详细信息,并且可以在单独的日志文件中进行捕获,从而为您提供有关问题的更多线索。现在很难做出有根据的猜测,文件句柄可能正在使用,服务器 I/O,文件可能以独占模式打开,连接可能已经中断了很短的时间,很难判断。
使用 TRY..CATCH 捕获日期和时间 + 异常的详细信息将帮助您打开 Windows 事件查看器并准确找出发生了什么。
我认为用foreach 语句调用executeQuery() 对性能不利。
最好使用"Include"在foreach语句外加载文件对象的属性
引用URL
https://msdn.microsoft.com/en-US/library/office/ee539350(v=office.14).aspx#Anchor_5
我宁愿使用.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()
}
除了其他评论和建议之外,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();
}
包括 $fileInfo.Stream.Close();
会变魔术。
这个问题让我困惑了好几天...它有一半以上的时间都有效,但在调用 ExecuteQuery() 命令时经常会给我这个错误:
Exception calling "ExecuteQuery" with "0" argument(s): "The operation has timed out"
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 window,它将运行几次,然后再次退出。有什么想法吗?
将 TRY..CATCH 块添加到您的 PowerShell 脚本,它将处理服务器超时,这将提供连续执行。
TRY...CATCH 的另一个好处是,您将能够获得异常的详细信息,并且可以在单独的日志文件中进行捕获,从而为您提供有关问题的更多线索。现在很难做出有根据的猜测,文件句柄可能正在使用,服务器 I/O,文件可能以独占模式打开,连接可能已经中断了很短的时间,很难判断。
使用 TRY..CATCH 捕获日期和时间 + 异常的详细信息将帮助您打开 Windows 事件查看器并准确找出发生了什么。
我认为用foreach 语句调用executeQuery() 对性能不利。 最好使用"Include"在foreach语句外加载文件对象的属性
引用URL
https://msdn.microsoft.com/en-US/library/office/ee539350(v=office.14).aspx#Anchor_5
我宁愿使用.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()
}
除了其他评论和建议之外,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();
}
包括 $fileInfo.Stream.Close();
会变魔术。