使用Powershell从Google云端硬盘下载文件 [英] Using Powershell to download files from Google Drive

查看:1796
本文介绍了使用Powershell从Google云端硬盘下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试找到一个从Google云端硬盘(共享文件,而不是我自己的文件)下载文件到服务器/网络驱动器上的位置的快速解决方案。



我正在使用:

  $ client = new-object System.Net.WebClient 
$ client .Credentials = Get-Credential
$ client.DownloadFile(https://docs.google.com/a/domainname.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI/export?format=xlsx, W:\Corp\Comp Serv\Comp Op\OB\Dep Data\Call\Google备份)

但是它返回消息:
异常使用2参数调用DownloadFile:远程服务器返回错误:(407)需要代理验证 / p>

所以我在Credentials和DownloadFile之间添加了以下行:

  $ client.Proxy.Credentials = [System.Net.CredentialCache] :: DefaultNetworkCredentials 

已经解决了(407)代理验证所需的问题,但现在我收到错误:


异常调用DownloadFile与2 参数:在WebClient请求期间发生一个异常



在行:1 char:21




而且,因为我几乎完全是Powershell的初学者,我不知道为什么我收到这个消息。



最后,我需要它下载共4/5个文件,所有到相同的位置...也是,理想情况下,我需要这个通过批处理命令或等价物运行,所以它可以是(几乎)1点击解决方案...



任何帮助将不胜感激!谢谢〜

解决方案

DownloadFile中的第二个参数应该是文件路径,而不是目录的路径。



参见这里:
错误在Powershell脚本中使用$ client.DownloadFile



编辑:要解决代理异常,您需要为呼叫设置代理身份验证。
示例:

  $ source =https://docs.google.com/a/domainname.co。 uk / spreadsheets / d / 1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI / export?format = xlsx
$ dest =W:\Corp\Comp Serv\Comp Op\OB\Dep Data\Call\Google backup \download.xlsx
$ WebClient = New-Object System.Net.WebClient
$ WebProxy = New-Object System.Net.WebProxy(http://myproxy.com:1111, $ true
$ Credentials = New-Object Net.NetworkCredential(user,domain.local)
$ Credentials = $ Credentials.GetCredential(http://myproxy.com ,1111,KERBEROS);
$ WebProxy.Credentials = $ Credentials
$ WebClient.Proxy = $ WebProxy
$ WebClient.DownloadFile($ source,$ dest)

https://social.technet.microsoft.com/Forums/windowsserver/en-US/1a05b90b- CE 12-4974-b578-0c1e22d03f10 / download-file-through-proxy-server


So, I'm currently trying to find a quick solution to downloading files from Google Drive (shared files, not files I own) to a location on a server / network drive.

I was using:

$client = new-object System.Net.WebClient
$client.Credentials =  Get-Credential
$client.DownloadFile("https://docs.google.com/a/domainname.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI/export?format=xlsx","W:\Corp\Comp Serv\Comp Op\OB\Dep Data\Call\Google backup")

However it returns with the message: Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."

So I added the following line between Credentials and DownloadFile:

$client.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials

This has solved the (407) Proxy Authentication Required problem, but now I'm getting the error:

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."

At line:1 char:21

And, because I'm pretty much a complete beginner with Powershell, I have no idea why I'm getting this message.

Eventually, I need it to download a total of 4/5 files, all to the same location... also, ideally, I need this to run via a batch command or something equivalent so it can be a (almost) 1 Click solution...

Any help would be greatly appreciated! Thanks~

解决方案

The second parameter in DownloadFile should be a file path, not a path to a directory.

See here: Error using $client.DownloadFile in Powershell script

EDIT: To solve the proxy exception you need to set the proxy authentication for your call. Example:

$source = "https://docs.google.com/a/domainname.co.uk/spreadsheets/d/1in0m8PhfiYhu4qCWO1dxNc3OS3p8prF7HWRZ-bjnKBI/export?format=xlsx"
$dest = "W:\Corp\Comp Serv\Comp Op\OB\Dep Data\Call\Google backup\download.xlsx"
$WebClient = New-Object System.Net.WebClient
$WebProxy = New-Object System.Net.WebProxy("http://myproxy.com:1111",$true)
$Credentials = New-Object Net.NetworkCredential("user,"","domain.local")
$Credentials = $Credentials.GetCredential("http://myproxy.com","1111", "KERBEROS");
$WebProxy.Credentials = $Credentials
$WebClient.Proxy = $WebProxy
$WebClient.DownloadFile($source,$dest)

https://social.technet.microsoft.com/Forums/windowsserver/en-US/1a05b90b-ce12-4974-b578-0c1e22d03f10/download-file-through-proxy-server

这篇关于使用Powershell从Google云端硬盘下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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