使用 Powershell 从 Google Drive 下载文件
Using Powershell to download files from Google Drive
因此,我目前正在尝试找到一种快速解决方案,以将文件从 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 backup")
但是它 returns 带有消息:
使用“2”个参数调用 "DownloadFile" 的异常:"The remote server returned an error: (407) Proxy Authentication Required."
所以我在 Credentials 和 DownloadFile 之间添加了以下行:
$client.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
这已经解决了 (407) Proxy Authentication Required 问题,但现在我收到错误:
Exception calling "DownloadFile" with "2" argument(s): "An exception
occurred during a WebClient request."
At line:1 char:21
- $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")
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : DotNetMethodException
而且,因为我几乎是 Powershell 的初学者,所以我不知道为什么会收到此消息。
最终,我需要它来下载总共 4/5 个文件,所有文件都到同一个位置......另外,理想情况下,我需要通过批处理命令或类似的命令将它 运行 所以它可以是(几乎)一键式解决方案...
如有任何帮助,我们将不胜感激!谢谢~
DownloadFile 中的第二个参数应该是文件路径,而不是目录路径。
看这里:
Error using $client.DownloadFile in Powershell script
编辑:要解决代理异常,您需要为您的呼叫设置代理身份验证。
示例:
$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)
因此,我目前正在尝试找到一种快速解决方案,以将文件从 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 backup")
但是它 returns 带有消息: 使用“2”个参数调用 "DownloadFile" 的异常:"The remote server returned an error: (407) Proxy Authentication Required."
所以我在 Credentials 和 DownloadFile 之间添加了以下行:
$client.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
这已经解决了 (407) Proxy Authentication Required 问题,但现在我收到错误:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:1 char:21
- $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")
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : DotNetMethodException
而且,因为我几乎是 Powershell 的初学者,所以我不知道为什么会收到此消息。
最终,我需要它来下载总共 4/5 个文件,所有文件都到同一个位置......另外,理想情况下,我需要通过批处理命令或类似的命令将它 运行 所以它可以是(几乎)一键式解决方案...
如有任何帮助,我们将不胜感激!谢谢~
DownloadFile 中的第二个参数应该是文件路径,而不是目录路径。
看这里: Error using $client.DownloadFile in Powershell script
编辑:要解决代理异常,您需要为您的呼叫设置代理身份验证。 示例:
$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)