使用 PowerShell 从 SharePoint Online 下载文件 [英] downloading a file from SharePoint Online with PowerShell
问题描述
我需要使用 powershell 从共享点在线文档库下载文件
I have a requirement to download files from a sharepoint online document library using powershell
我已经设法达到下载应该发生但没有运气的地步.
I've managed to get to the point where the download should happen but no luck.
我知道这与我使用流/编写器的方式有关
I know its something to do with how I am using the stream/writer
任何提示将不胜感激
*编辑我的本地目录中只有 0 个长度的文件没有抛出错误消息
*Edit No error messages are thrown just 0 length files in my local Directory
$SPClient = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$SPRuntime = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$webUrl = Read-Host -Prompt "HTTPS URL for your SP Online 2013 site"
$username = Read-Host -Prompt "Email address for logging into that site"
$password = Read-Host -Prompt "Password for $username" -AsSecureString
$folder = "PoSHTest"
$destination = "C:\\test"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$web = $ctx.Web
$lists = $web.Lists.GetByTitle($folder)
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000)
$result = $lists.GetItems($query)
$ctx.Load($Lists)
$ctx.Load($result)
$ctx.ExecuteQuery()
#Edited the foreach as per @JNK
foreach ($File in $result) {
Write-host "Url: $($File["FileRef"]), title: $($File["FileLeafRef"]) "
$binary = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$File["FileRef"])
$Action = [System.IO.FileMode]::Create
$new = $destination + "\\" + $File["FileLeafRef"]
$stream = New-Object System.IO.FileStream $new, $Action
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
}
推荐答案
虽然上面的 CSOM 代码可能可以工作,但我发现使用 Web 客户端方法更容易.
While the CSOM code above likely can be made to work I find it easier to use the web client method.
(来自 http://soerennielsen.wordpress.com/2013/08/25/use-csom-from-powershell/)
我使用下面的代码将一堆文件(来自 CSOM 查询的元数据)检索到一个文件夹(使用您的 $result 集合,其他参数应该稍微调整一下):
I've used the code below, to retrieve a bunch of files (metadata from CSOM queries) to a folder (using your $result collection, other params should be adjusted a bit):
#$siteUrlString site collection url
#$outPath path to export directory
$siteUri = [Uri]$siteUrlString
$client = new-object System.Net.WebClient
$client.UseDefaultCredentials=$true
if ( -not (Test-Path $outPath) ) {
New-Item $outPath -Type Directory | Out-Null
}
$result |% {
$url = new-object Uri($siteUri, $_["FileRef"])
$fileName = $_["FileLeafRef"]
$outFile = Join-Path $outPath $fileName
Write-Host "Downloading $url to $outFile"
try{
$client.DownloadFile( $url, $outFile )
}
catch{
#one simple retry...
try{
$client.DownloadFile( $url, $outFile )
}
catch{
write-error "Failed to download $url, $_"
}
}
}
这里的诀窍是$client.UseDefaultCredentials=$true
这将为您(作为当前用户)验证网络客户端.
which will authenticate the webclient for you (as the current user).
这篇关于使用 PowerShell 从 SharePoint Online 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!