首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用PowerShell从VirtualBox下载/上传文件

用PowerShell从VirtualBox下载/上传文件
EN

Stack Overflow用户
提问于 2017-06-23 07:37:59
回答 1查看 1.4K关注 0票数 1

我试图将文件上传到运行Linux和FTP服务器的虚拟机上。我在代码中有一个例外:

代码语言:javascript
运行
复制
Exception calling "GetRequestStream" with "0" argument(s): "The requested FTP command is not supported when using HTTP proxy."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:1
+ $Run = $FTPRequest.GetRequestStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

我用的脚本是:

代码语言:javascript
运行
复制
# Config
$Username = "sammy"
$Password = "1111"
$LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
$RemoteFile = "ftp://192.168.56.101/files/1618716-maybach-exelero.jpg"

# Create FTP Rquest Object

$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
$FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    
$FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Read the File for Upload
$FileContent = gc -en byte $LocalFile
$FTPRequest.ContentLength = $FileContent.Length
# Get Stream Request by bytes
$Run = $FTPRequest.GetRequestStream()
$Run.Write($FileContent, 0, $FileContent.Length)
# Cleanup
$Run.Close()

$Run.Dispose()

PowerShell ver为5.1。我在主机上有代理。

当尝试下载时:

代码语言:javascript
运行
复制
# Config
$Username = "sammy"
$Password = "1111"
$LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
$RemoteFile = "ftp://192.168.56.101/files/1618716-maybach-exelero.jpg"

# Create a FTPWebRequest 
$FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) 
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) 
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
$FTPRequest.UseBinary = $true 
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse() 
# Get a download stream from the server response 
$ResponseStream = $FTPResponse.GetResponseStream() 
# Create the target file on the local system and the download buffer 
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
[byte[]]$ReadBuffer = New-Object byte[] 1024 
# Loop through the download 
do { 
    $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
    $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
} 
while ($ReadLength -ne 0)

我有一个错误:

代码语言:javascript
运行
复制
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:16 char:1
+ $FTPResponse = $FTPRequest.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

尝试WinSCP .NET程序集时:

代码语言:javascript
运行
复制
# Load WinSCP .NET assembly
Add-Type -Path "D:\Users\h.yordanov\Desktop\PowerShellFtp-master\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "192.168.56.101"
    UserName = "sammy"
    Password = "1111"
}

# Configure proxy
$sessionOptions.AddRawSettings("ProxyMethod", "1")
$sessionOptions.AddRawSettings("ProxyHost", "proxy.abc.dfg.bg")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload file
    $LocalFile = "C:\fff\medias\test2.txt"
    $RemoteFile = "/home/sammy/ftp/files/test2.txt"
    $session.PutFiles($LocalFile, $RemoteFile).Check()
}
finally
{
    $session.Dispose()
}

我知道这个错误:

代码语言:javascript
运行
复制
Exception calling "Open" with "1" argument(s): "Connection failed.
Can't connect to proxy server
No connection could be made because the target machine actively refused it.
Connection failed."
At D:\Users\h.yordanov\Desktop\PowerShellFtp-master\script.ps1:21 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

通过FileZilla从连接中登录。(我改变了ip)

代码语言:javascript
运行
复制
2017-06-23 14:50:14 6364 1 Status: Connecting to 192.168.81.3:21...
2017-06-23 14:50:14 6364 1 Status: Connection established, waiting for welcome message...
2017-06-23 14:50:14 6364 1 Response: 220 (vsFTPd 3.0.3)
2017-06-23 14:50:14 6364 1 Command: AUTH TLS
2017-06-23 14:50:14 6364 1 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:14 6364 1 Command: AUTH SSL
2017-06-23 14:50:14 6364 1 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:14 6364 1 Status: Insecure server, it does not support FTP over TLS.
2017-06-23 14:50:14 6364 1 Command: USER sammy
2017-06-23 14:50:14 6364 1 Response: 331 Please specify the password.
2017-06-23 14:50:14 6364 1 Command: PASS ****
2017-06-23 14:50:14 6364 1 Response: 230 Login successful.
2017-06-23 14:50:14 6364 1 Command: SYST
2017-06-23 14:50:14 6364 1 Response: 215 UNIX Type: L8
2017-06-23 14:50:14 6364 1 Command: FEAT
2017-06-23 14:50:14 6364 1 Response: 211-Features:
2017-06-23 14:50:14 6364 1 Response:  EPRT
2017-06-23 14:50:14 6364 1 Response:  EPSV
2017-06-23 14:50:14 6364 1 Response:  MDTM
2017-06-23 14:50:14 6364 1 Response:  PASV
2017-06-23 14:50:14 6364 1 Response:  REST STREAM
2017-06-23 14:50:14 6364 1 Response:  SIZE
2017-06-23 14:50:14 6364 1 Response:  TVFS
2017-06-23 14:50:14 6364 1 Response: 211 End
2017-06-23 14:50:14 6364 1 Status: Server does not support non-ASCII characters.
2017-06-23 14:50:14 6364 1 Status: Logged in
2017-06-23 14:50:14 6364 1 Status: Retrieving directory listing...
2017-06-23 14:50:14 6364 1 Command: PWD
2017-06-23 14:50:14 6364 1 Response: 257 "/" is the current directory
2017-06-23 14:50:14 6364 1 Command: TYPE I
2017-06-23 14:50:14 6364 1 Response: 200 Switching to Binary mode.
2017-06-23 14:50:14 6364 1 Command: PASV
2017-06-23 14:50:14 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,192,147).
2017-06-23 14:50:14 6364 1 Command: LIST
2017-06-23 14:50:14 6364 1 Response: 150 Here comes the directory listing.
2017-06-23 14:50:14 6364 1 Response: 226 Directory send OK.
2017-06-23 14:50:14 6364 1 Status: Directory listing of "/" successful
2017-06-23 14:50:16 6364 1 Status: Retrieving directory listing of "/files"...
2017-06-23 14:50:16 6364 1 Command: CWD files
2017-06-23 14:50:16 6364 1 Response: 250 Directory successfully changed.
2017-06-23 14:50:16 6364 1 Command: PWD
2017-06-23 14:50:16 6364 1 Response: 257 "/files" is the current directory
2017-06-23 14:50:16 6364 1 Command: PASV
2017-06-23 14:50:16 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,193,132).
2017-06-23 14:50:16 6364 1 Command: LIST
2017-06-23 14:50:16 6364 1 Response: 150 Here comes the directory listing.
2017-06-23 14:50:16 6364 1 Response: 226 Directory send OK.
2017-06-23 14:50:16 6364 1 Status: Calculating timezone offset of server...
2017-06-23 14:50:16 6364 1 Command: MDTM test.txt
2017-06-23 14:50:16 6364 1 Response: 213 20170622155419
2017-06-23 14:50:16 6364 1 Status: Timezone offset of server is 0 seconds.
2017-06-23 14:50:16 6364 1 Status: Directory listing of "/files" successful
2017-06-23 14:50:18 6364 1 Status: Retrieving directory listing of "/test"...
2017-06-23 14:50:18 6364 1 Command: CWD /test
2017-06-23 14:50:18 6364 1 Response: 250 Directory successfully changed.
2017-06-23 14:50:18 6364 1 Command: PWD
2017-06-23 14:50:18 6364 1 Response: 257 "/test" is the current directory
2017-06-23 14:50:18 6364 1 Command: PASV
2017-06-23 14:50:18 6364 1 Response: 227 Entering Passive Mode (192,168,81,3,162,156).
2017-06-23 14:50:18 6364 1 Command: LIST
2017-06-23 14:50:18 6364 1 Response: 150 Here comes the directory listing.
2017-06-23 14:50:18 6364 1 Response: 226 Directory send OK.
2017-06-23 14:50:18 6364 1 Status: Directory listing of "/test" successful
2017-06-23 14:50:22 6364 3 Status: Connecting to 192.168.81.3:21...
2017-06-23 14:50:22 6364 3 Status: Connection established, waiting for welcome message...
2017-06-23 14:50:22 6364 3 Response: 220 (vsFTPd 3.0.3)
2017-06-23 14:50:22 6364 3 Command: AUTH TLS
2017-06-23 14:50:22 6364 3 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:22 6364 3 Command: AUTH SSL
2017-06-23 14:50:22 6364 3 Response: 530 Please login with USER and PASS.
2017-06-23 14:50:22 6364 3 Status: Insecure server, it does not support FTP over TLS.
2017-06-23 14:50:22 6364 3 Command: USER sammy
2017-06-23 14:50:22 6364 3 Response: 331 Please specify the password.
2017-06-23 14:50:22 6364 3 Command: PASS ****
2017-06-23 14:50:22 6364 3 Response: 230 Login successful.
2017-06-23 14:50:22 6364 3 Status: Server does not support non-ASCII characters.
2017-06-23 14:50:22 6364 3 Status: Logged in
2017-06-23 14:50:22 6364 3 Status: Starting download of /files/test.txt
2017-06-23 14:50:22 6364 3 Command: CWD /files
2017-06-23 14:50:22 6364 3 Response: 250 Directory successfully changed.
2017-06-23 14:50:22 6364 3 Command: PWD
2017-06-23 14:50:22 6364 3 Response: 257 "/files" is the current directory
2017-06-23 14:50:23 6364 3 Command: TYPE A
2017-06-23 14:50:23 6364 3 Response: 200 Switching to ASCII mode.
2017-06-23 14:50:23 6364 3 Command: PASV
2017-06-23 14:50:23 6364 3 Response: 227 Entering Passive Mode (192,168,81,3,162,251).
2017-06-23 14:50:23 6364 3 Command: RETR test.txt
2017-06-23 14:50:23 6364 3 Response: 150 Opening BINARY mode data connection for test.txt (17 bytes).
2017-06-23 14:50:23 6364 3 Response: 226 Transfer complete.
2017-06-23 14:50:23 6364 3 Status: File transfer successful, transferred 17 bytes in 1 second

我没看到任何代理连接。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-23 07:52:42

FtpWebRequest不支持某些操作(包括文件上传)的HTTP。很明显是记录在MSDN上

如果指定的代理是HTTP代理,则只支持DownloadFile、ListDirectory和ListDirectoryDetails命令。

无法使用PowerShell和标准.NET框架库通过HTTP将文件上载到FTP。

您必须使用第三方FTP库或PowerShell模块。

例如,对于WinSCP .NET组装,您可以使用:

代码语言:javascript
运行
复制
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = "ftp.example.com"
    UserName = "user"
    Password = "mypassword"
}

# Configure proxy
$sessionOptions.AddRawSettings("ProxyMethod", "3")
$sessionOptions.AddRawSettings("ProxyHost", "proxy")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload file
    $LocalFile = "C:\fff\medias\1618716-maybach-exelero.jpg"
    $RemoteFile = "/files/1618716-maybach-exelero.jpg"
    $session.PutFiles($LocalFile, $RemoteFile).Check()
}
finally
{
    $session.Dispose()
}

有关SessionOptions.AddRawSettings的选项,请参见原始设置

你可以拥有WinSCP图形用户界面生成PowerShell FTP上传代码模板,就像上面一样,给你。

(我是WinSCP的作者)

尽管您的实际问题是您在计算机上的Internet连接选项中配置了代理,但实际上您不想使用它们。

正确的解决方案是修复这些选项。

转到控制面板>网络和Internet > Internet选项>连接> LAN设置。

或者作为解决办法,在PowerShell代码中禁用特定连接的代理:

代码语言:javascript
运行
复制
$FTPRequest.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()

绕过PowerShell HTTP中的系统代理设置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44715710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档