downloading a file from SharePoint Online with PowerShell

别等时光非礼了梦想. 提交于 2019-11-30 16:21:30

问题


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

any hints would be greatly appreciated

*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()

}


回答1:


While the CSOM code above likely can be made to work I find it easier to use the web client method.

(from http://soerennielsen.wordpress.com/2013/08/25/use-csom-from-powershell/)

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, $_"
        }
    }
}   

The trick here is the $client.UseDefaultCredentials=$true

which will authenticate the webclient for you (as the current user).




回答2:


You could also utilize WebClient.DownloadFile Method by providing SharePoint Online credentials to download the resource from SharePoint Online as demonstrated below.

Prerequisites

SharePoint Online Client Components SDK have to be installed on the machine running the script.

How to download a file in SharePoint Online/O365 in PowerShell

Download-File.ps1 function:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


 Function Download-File([string]$UserName, [string]$Password,[string]$FileUrl,[string]$DownloadPath)
 {
    if([string]::IsNullOrEmpty($Password)) {
      $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString 
    }
    else {
      $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
    }
    $fileName = [System.IO.Path]::GetFileName($FileUrl)
    $downloadFilePath = [System.IO.Path]::Combine($DownloadPath,$fileName)


    $client = New-Object System.Net.WebClient 
    $client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    $client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    $client.DownloadFile($FileUrl, $downloadFilePath)
    $client.Dispose()
}

Usage

Download-File -UserName "username@contoso.onmicrosoft.com" -Password "passowrd" -FileUrl https://consoto.sharepoint.com/Shared Documents/SharePoint User Guide.docx -DownloadPath "c:\downloads"



回答3:


I was able to download the file successfully with the following relevant code snippet. You should be able to extend it for your situation.

Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$siteUrl = Read-Host -Prompt "Enter web URL"
$username = Read-Host -Prompt "Enter your username"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$source = "/filepath/sourcefilename.dat" #server relative URL here
$target = "C:/detinationfilename.dat" #URI of the file locally stored

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials

[Microsoft.SharePoint.Client.FileInformation] $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$source);
[System.IO.FileStream] $writeStream = [System.IO.File]::Open($target,[System.IO.FileMode]::Create);

$fileInfo.Stream.CopyTo($writeStream);
$writeStream.Close();



回答4:


So I gave up on this. it turned out to be much easier to write an SSIS script component to do the job. I have awarded Soeren as he posted some code that will work for regular websites but not sodding SharePoint Online.

Thanks Sorean!




回答5:


The direct and almost shortest answer to the question is simply:

$url = 'https://the.server/path/to/the/file.txt'
$outfile = "$env:userprofile\file.txt"
Invoke-WebRequest -Uri $url -OutFile $outfile -Credential (Get-Credential)

This works at least in Powershell 5.1...



来源:https://stackoverflow.com/questions/18866958/downloading-a-file-from-sharepoint-online-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!