下载SharePoint文件PowerShell异常:无法从的重写外部调用WriteObject和WriteError方法
我收到以下错误 Get-PnPFile:不能从BeginProcessing、ProcessRecord和EndProcessing方法重写的外部调用WriteObject和WriteError方法,只能从同一线程内调用它们。验证cmdlet是否正确进行这些调用,或者与Microsoft客户支持服务部门联系 运行此PowerShell脚本时:下载SharePoint文件PowerShell异常:无法从的重写外部调用WriteObject和WriteError方法,powershell,exception,sharepoint,windows-server-2012,Powershell,Exception,Sharepoint,Windows Server 2012,我收到以下错误 Get-PnPFile:不能从BeginProcessing、ProcessRecord和EndProcessing方法重写的外部调用WriteObject和WriteError方法,只能从同一线程内调用它们。验证cmdlet是否正确进行这些调用,或者与Microsoft客户支持服务部门联系 运行此PowerShell脚本时: $cred = Get-Credential; $webUrl = "https://...sharepoint.com"; $listUrl = "..
$cred = Get-Credential;
$webUrl = "https://...sharepoint.com";
$listUrl = "..";
$destination = "C:\\Folder1"
Connect-PnPOnline -Url $webUrl -Credentials $cred
$web = Get-PnPWeb
$list = Get-PNPList -Identity $listUrl
function ProcessFolder($folderUrl, $destinationFolder) {
$folder = Get-PnPFolder -RelativeUrl $folderUrl
$tempfiles = Get-PnPProperty -ClientObject $folder -Property Files
if (!(Test-Path -Path $destinationfolder)) {
$dest = New-Item $destinationfolder -Type Directory
}
$total = $folder.Files.Count
for ($i = 0; $i -lt $total; $i++) {
$file = $folder.Files[$i]
Get-PnPFile -ServerRelativeUrl $file.ServerRelativeUrl -Path
$destinationfolder -FileName $file.Name -AsFile
}
}
function ProcessSubFolders($folders, $currentPath) {
foreach ($folder in $folders) {
$tempurls = Get-PnPProperty -ClientObject $folder -Property ServerRelativeUrl
# Avoid Forms folders
if ($folder.Name -ne "Forms") {
$targetFolder = $currentPath +"\"+ $folder.Name;
ProcessFolder
$folder.ServerRelativeUrl.Substring($web.ServerRelativeUrl.Length)
$targetFolder
$tempfolders = Get-PnPProperty -ClientObject $folder -Property Folders
ProcessSubFolders $tempfolders $targetFolder
}
}
}
# Download root files
ProcessFolder $listUrl $destination + "\"
# Download files in folders
$tempfolders = Get-PnPProperty -ClientObject $list.RootFolder -Property Folders
ProcessSubFolders $tempfolders $destination + "\"
此脚本在Win10 PC上按预期工作,但在Win服务器上不工作。有谁能告诉我原因是什么吗?今天早上再次尝试后,上面的脚本在Windows 10 PC和Windows服务器上都可以运行
Alexandra在某些情况下,这可能有助于人们寻找确切的例外情况。例外情况似乎无法解释实际发生的情况,因此以下是一些背景,让人们看看情况是否与我的类似: 它是在我使用Powershell和
Get PNPFile
从Sharepoint站点批量获取文件时生成的。经过一番深思熟虑之后,这似乎与本地系统上存在以前下载的文件有关。由于脚本用于保存sharepoint网站上某些文件夹的本地副本,因此如果服务器文件较新,我将尝试在本地覆盖这些文件。(有点像粗糙的rsync)
如果文件已经存在,Get PnpFile
似乎不会关闭(以静默方式覆盖)该文件,但会生成此异常。以下是无误运行的代码的概要:
Try {
$local_path = ($local_dir + $local_file)
#test for the local file first and rename it
if( Test-Path ( $local_path ) -PathType Leaf ){
Rename-Item $local_path ($local_path + '.old' )
}
#get the file
Get-PnpFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $local_dir -FileName $local_file -AsFile
#remove the old file
if( Test-Path ( $local_path ) -PathType Leaf ){
Remove-Item ($local_path + '.old' )
}
}
Catch {
#handle error and continue
}
基本上先测试本地文件,然后重命名它,下载文件,删除重命名的本地文件。我将其包装在一个Try-catch块中,这样,如果该文件的下载失败,我可以继续作为循环的一部分,稍后我可以搜索任何带有.old后缀的文件名,并恢复失败的最新本地文件,并记下这些文件
重新运行脚本时,脚本将无法在本地看到远程文件(因为名称不匹配,无法获取新的副本),但如果成功获取文件,请清除.old(以前运行过的)文件
我没有再看到例外情况。
希望这能帮助别人
注意(如果您的任何文件以旧文件结尾,则使用上述简单方法会发生奇怪/未知的事情…您已收到警告)
更新
每当我看到此异常的新原因时,我都会在此处更新
更新
文件名中的“[”或“]”方括号也可能导致这种情况。可能URL中需要URL编码或转义