downloading a file from SharePoint Online with PowerShell

前端 未结 5 2077
终归单人心
终归单人心 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: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();
    

提交回复
热议问题