如何通过使用 power shell 使用 Get-Credentials 进行身份验证来从 OneDrive 下载文件?

How to download files from OneDrive by authenticating using Get-Credentials using power shell?

这是我现在正在使用的代码,它已经过测试可以下载文件,但是如果需要验证,比如一个驱动器,我无法验证和下载文件。我的代码在这里:

    $CheckFile = Test-Path "$PSScriptRoot.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 对象中简单地提供您的用户名/密码。

使用在线服务时,您必须将权限委派给应用程序(在本例中为您的 PowerShell 脚本),这将为您提供一个可用于下载文件的令牌。最流行的凭证委托形式之一是 oAuth。如果您想了解如何自己处理 oAuth/similar 委托,我会在 this blog post here 中介绍 oAuth 基础知识。

一般来说,您不会想要推出自己的 oAuth 解决方案,尤其是如果其他人已经为您完成了。

一个方便的选择

幸运的是,Marcel Meurer 已经为 OneDrive 编写了一个 PowerShell 模块,它可以处理凭证委托的所有繁重工作并为您登录!他在 his post here 中谈到。

使用以下内容安装模块

Install-Module -Name OneDrive 

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

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

$AuthToken=$Authentication.access_token

这将启动一个 GUI window,您可以在其中使用您的 OneDrive 凭据登录并将它们分配给令牌。

完成后,您可以使用以下语法下载文件

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

这会将文件下载到您当前的文件夹中。有关使用此模块的更多示例,check out Marcel's blog post 其中包含针对大多数用例的深入示例,包括如何处理过期令牌。