如何使用Power shell使用Get-Credentials进行身份验证,从OneDrive下载文件?

Sam*_*pra 4 powershell command-line download onedrive

这是我现在使用的代码,它已经过测试工作下载文件,但如果需要身份验证,如一个驱动器,我无法验证和下载该文件.我的代码在这里:

    $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}
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激!

Fox*_*loy 6

为什么Get-Credential不会自行运行

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

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

一般来说,您不会想要推出自己的oAuth解决方案,特别是如果其他人已经为您完成此操作.

一个方便的选择

幸运的是,Marcel Meurer已经为OneDrive编写了一个PowerShell模块,它可以处理所有重要的凭证授权并为您签名!他在这里的帖子中谈到了.

使用以下内容安装模块

Install-Module -Name OneDrive 
Run Code Online (Sandbox Code Playgroud)

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

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

$AuthToken=$Authentication.access_token
Run Code Online (Sandbox Code Playgroud)

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

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

Get-ODItem -AccessToken $AuthToken -Path "/Data/documents/2016/Powershell array custom objects.docx"
Run Code Online (Sandbox Code Playgroud)

这会将文件下载到您当前的文件夹中.有关使用此模块的其他示例,请查看Marcel的博客文章,其中包含大多数用例的深入示例,包括如何处理过期令牌.