嘗試使用 get-PnPFile 從 OneDrive 獲取文件的所有版本時找不到文件 (File not found trying to get all versions of a file from OneDrive using get-PnPFile)


問題描述

嘗試使用 get‑PnPFile 從 OneDrive 獲取文件的所有版本時找不到文件 (File not found trying to get all versions of a file from OneDrive using get‑PnPFile)

我希望編寫一個 PowerShell 腳本以從單個 OneDrive 文件下載所有版本,但我無法獲取 Get‑PnPFile 接受的有效 URL。

$ctx= Get‑PnPContext
$item = Get‑PnPFile ‑Url [url] ‑AsListItem
$file = $item.file
$ctx.Load($file)
$fileVersions = $file.Versions
$ctx.Load($fileVersions)
$ctx.ExecuteQuery()

foreach ($version in $fileVersions) {
       Get‑PnPFile ‑Url $version.Url ‑Path z:\tmp ‑FileName ($file.Name + " " + $version.VersionLabel + ".json") ‑AsFile
    }
}

Powershell 文檔 建議您可以為 ‑Url 使用相對於站點的 URL,$version.Url 的格式為 _vti_history/2662400/Documents/[filename]

adding /personal/[email_address ]/... 無法為我解決“找不到文件”錯誤。


參考解法

方法 1:

Get‑PnPFile is not able to download the file from _vti_history. You need to use the CSOM functionality $version.OpenBinaryStream().

For SharePoint Online I'm using following code:

Get‑PnPFile ‑Url $item.ServerRelativeUrl ‑Path $destinationFolderPath ‑AsFile ‑Force # Latest version
$ctx= Get‑PnPContext
$ctx.Load($item.Versions)
$ctx.ExecuteQuery()
foreach ($version in $item.Versions)
{
    $versionValue = $version.VersionLabel
    $str = $version.OpenBinaryStream()
    $ctx.ExecuteQuery()
    $filename =  (Split‑Path $item.ServerRelativeUrl ‑Leaf) + "." + $versionValue
    $filepath = Join‑Path $destinationFolderPath $filename
    $fs = New‑Object IO.FileStream $filepath ,'Append','Write','Read'
    $str.Value.CopyTo($fs) # Older version
    $fs.Close()
}

(by digihumsteveAlyaKoni)

參考文件

  1. File not found trying to get all versions of a file from OneDrive using get‑PnPFile (CC BY‑SA 2.5/3.0/4.0)

#onedrive #powershell #Version






相關問題

WP7-Skydrive API 下載任何文件並保存隔離存儲 (WP7-Skydrive API Download Any file and Save Isolated Storage)

如何編寫 .NET 控制台應用程序來訪問 SkyDrive? (How can I write a .NET console application to access SkyDrive?)

Loại đăng nhập Skydrive API Windows 8 (Skydrive API login type Windows 8)

如何使用 Rest api 和 oauth2.0 創建文件夾和上傳文件 (How to create folder and upload file using Rest api and oauth2.0)

從 ERP 保存到 OneDrive (Save to OneDrive from ERP)

適用於 Windows 通用 APP 的 OneDrive Api (OneDrive Api for Windows Universal APP)

一種 Drive / Office365 與 Google Apps Script 類似的腳本語言 (One Drive / Office365 similar scripting language as the Google Apps Script)

OneDrive API 瀏覽器 C# (OneDrive API browser C#)

從 Python 應用程序訪問 Microsoft Live 服務 (Access Microsoft Live services from Python application)

一個驅動器 api 安全 URL (One drive api secure url)

嘗試使用 get-PnPFile 從 OneDrive 獲取文件的所有版本時找不到文件 (File not found trying to get all versions of a file from OneDrive using get-PnPFile)

一個驅動器遷移 (One Drive Migration)







留言討論