簡體   English   中英

使用PowerShell從virtualbox下載/上傳ftp文件

[英]Downloading/upload files from ftp with powershell from virtualbox

您好我正在嘗試將文件上傳到運行Linux和ftp服務器的虛擬機。 我在代碼中有一個例外:

    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

我使用的腳本是這樣的:

    # 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。 我在主機上有代理。

編輯:我現在正在使用下載命令:

# 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)

但是我有這個錯誤:

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

第二次編輯:

# 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()
}

在此之后我得到這個錯誤:

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

編輯3:

通過filezilla連接登錄。 (我換了ip)

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

我在這里看不到任何代理連接。

FtpWebRequest不支持某些操作的HTTP代理,包括文件上載。 在MSDN上清楚地記錄在案

如果指定的代理是HTTP代理,則僅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令。

使用PowerShell和標准.NET框架庫,無法通過HTTP代理將文件上載到FTP。

您必須使用第三方FTP庫或PowerShell模塊。


例如,使用WinSCP .NET程序集 ,您可以使用:

# 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 GUI為您生成PowerShell FTP上傳代碼模板 ,如上所述。

(我是WinSCP的作者)


雖然您的實際問題是您在計算機上的Internet連接選項中配置了代理,但實際上您並不想使用它們。

正確的解決方案是修復選項。
轉到“ 控制面板”>“網絡和Internet”>“Internet選項”>“連接”>“LAN”設置

或者作為解決方法,在PowerShell代碼中禁用特定連接的代理:

$FTPRequest.Proxy = [System.Net.GlobalProxySelection]::GetEmptyWebProxy()

請參閱powershell http獲取不使用代理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM