downloading a file from SharePoint Online with PowerShell

前端 未结 5 2053
终归单人心
终归单人心 2020-12-10 09:43

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

相关标签:
5条回答
  • 2020-12-10 10:05

    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).

    0 讨论(0)
  • 2020-12-10 10:10

    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...

    0 讨论(0)
  • 2020-12-10 10:11

    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!

    0 讨论(0)
  • 2020-12-10 10:24

    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();
    
    0 讨论(0)
  • 2020-12-10 10:29

    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"
    
    0 讨论(0)
提交回复
热议问题