如何通过使用Power Shell使用Get-Credentials进行身份验证从OneDrive下载文件? [英] How to download files from OneDrive by authenticating using Get-Credentials using power shell?

查看:110
本文介绍了如何通过使用Power Shell使用Get-Credentials进行身份验证从OneDrive下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在正在使用的代码,已经过测试可以下载文件,但是,如果需要身份验证(例如一个驱动器),则无法进行身份验证和下载文件.我的代码在这里:

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}

任何帮助将不胜感激!

推荐答案

为什么Get-Credential不能单独使用

要从OneDrive下载文件,将无法像在Get-Credential中那样简单地在System.Management.Automation.PSCredential对象中提供用户名/密码.

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.

使用联机服务时,您必须将权限委派给应用程序(在本例中为PowerShell脚本),这将为您提供一个令牌,您可以使用该令牌来下载文件. oAuth是最流行的凭据委派形式之一.如果您愿意,我会在此博客文章中介绍oAuth基础.想学习如何自行处理oAuth/类似委托.

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.

通常来说,您将不想推出自己的oAuth解决方案,尤其是在其他人已经为您完成的情况下.

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

幸运的是,Marcel Meurer已经为OneDrive写了一个PowerShell模块,该模块可以处理所有繁重的凭据委派工作,并为您注册!他在他的文章中谈到在此处发布.

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-Module -Name OneDrive 

接下来,您可以使用以下两个cmdlet创建一个一小时的令牌.

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

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

$AuthToken=$Authentication.access_token

这将启动一个GUI窗口,您可以在其中使用OneDrive凭据登录并将其分配给令牌.

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.

这篇关于如何通过使用Power Shell使用Get-Credentials进行身份验证从OneDrive下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆