How to download files from OneDrive by authenticat

2019-06-23 22:17发布

Here is the code I am using right now, it has been tested working to download a file, however if there is authentication required such as one drive, I am not able to authenticate and download the file. The code I have is here:

    $CheckFile = Test-Path "$PSScriptRoot\5.2.0\Content Manager Setup.exe"
if ($CheckFile) {exit} else {$colorscheme = (Get-Host).PrivateData
$colorscheme.ProgressBackgroundColor = "black"
$colorscheme.ProgressForegroundColor = "red"
 Function MakingProgress
 {
param(
    [Parameter(Mandatory=$true)]
    [String] $url,
    [Parameter(Mandatory=$false)]
    [String] $localFile = (Join-Path $pwd.Path $url.SubString($url.LastIndexOf('/'))) 
)

begin {
    $client = New-Object System.Net.WebClient
    $Global:downloadComplete = $false
    $eventDataComplete = Register-ObjectEvent $client DownloadFileCompleted `
        -SourceIdentifier WebClient.DownloadFileComplete `
        -Action {$Global:downloadComplete = $true}
    $eventDataProgress = Register-ObjectEvent $client DownloadProgressChanged `
        -SourceIdentifier WebClient.DownloadProgressChanged `
        -Action { $Global:DPCEventArgs = $EventArgs }    
}
process {
    Write-Progress -Activity 'Downloading file' -Status $url
    $client.Credentials =  Get-Credential
    $client.DownloadFileAsync($url, $localFile)

    while (!($Global:downloadComplete)) {                
        $pc = $Global:DPCEventArgs.ProgressPercentage
        if ($pc -ne $null) {
            Write-Progress -Activity 'Downloading file' -Status $url -PercentComplete $pc
        }
    }

    Write-Progress -Activity 'Downloading file' -Status $url -Complete
}
end {
    Unregister-Event -SourceIdentifier WebClient.DownloadProgressChanged
    Unregister-Event -SourceIdentifier WebClient.DownloadFileComplete
    $client.Dispose()
    $Global:downloadComplete = $null
    $Global:DPCEventArgs = $null
    Remove-Variable client
    Remove-Variable eventDataComplete
    Remove-Variable eventDataProgress
    [GC]::Collect()    
} 
 }
$SRC = "https://fourwindsinteractivehq-my.sharepoint.com/personal/sameer_chopra_fourwindsinteractive_com/_layouts/15/guestaccess.aspx?docid=115e84d95a5944c1995b56e4f738fdde9&authkey=AU7hSoQX7TXgk1Pb2rLhPIk&expiration=2017-12-14T17%3A29%3A32.000Z&e=19981f1187a447ad8bd4b6e3cb46e9f9"
$DEST = "5.2.0\Content Manager Setup.exe"
MakingProgress $SRC $DEST}

Any help would be much appreciated!

1条回答
唯我独甜
2楼-- · 2019-06-23 22:41

Why Get-Credential Won't work on it's own

In order to download a file from OneDrive, you will not be able to simply provide your username / password in a System.Management.Automation.PSCredential object like you get from Get-Credential.

When working with online services you have to delegate permissions to an application (in this case, your PowerShell script), which will give you a token you can use to download files. One of the most popular forms of credential delegation is oAuth. I cover oAuth basics in this blog post here, if you want to learn how to handle oAuth/similar delegation on your own.

Generally speaking, you will not want to roll your own oAuth solution, especially if someone else has already done it for you.

A Convenient Alternative

Fortunately Marcel Meurer already wrote a PowerShell module for OneDrive which handles all of the heavy lifting of Credential Delegation and sign on for you! He talks about in his post here.

Install the Module with the following

Install-Module -Name OneDrive 

Next, you can create a one hour token using the following two cmdlets.

$Authentication=Get-ODAuthentication -ClientID "00000000…….."

$AuthToken=$Authentication.access_token

This will launch a GUI window in which you sign in with your OneDrive credentials and assign them to a token.

Once you've done that, you can download a file using the following syntax

Get-ODItem -AccessToken $AuthToken -Path "/Data/documents/2016/Powershell array custom objects.docx"

This will download the file into your current folder. For additional examples on working with this module, check out Marcel's blog post which has in-depth samples for most use cases, including how to handle expiring tokens.

查看更多
登录 后发表回答