問題描述
嘗試使用 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 digihumsteve、AlyaKoni)