2012-01-27 113 views
1

我在嘗試使用PowerShell上傳文件時出現問題。我想要做的是將.zip文件發送到目標服務器上的目標用戶帳戶。目標服務器正在運行IIS FTP 7.5並啓用了用戶隔離,數據通道端口範圍爲5500-6500(如果這可能很重要)。使用響應流上傳Powershell文件

下面是我的代碼如下 - 問題是我得到,我無法調用$responsestream請求上的空值表達式上的方法。請讓我知道,如果我打起精神,在網上查找......上傳文件時遇到了很多問題!

另外我想說我使用了一個下載腳本,我轉換上傳,因爲我沒有任何成功的上傳腳本,我以前試過。

$targetpath = "ftp://10.21.109.202/Recieve/account_apps.zip" 
$sourceuri = "D:\AccountManager\Send\$RTMPHOST\account_apps.zip" 
$username = "AccountManager" 
$password = "test" 

# Create a FTPWebRequest object to handle the connection to the ftp server 
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri) 

# set the request's network credentials for" 

#an authenticated connection 
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password) 

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile 
$ftprequest.UseBinary = $true 
$ftprequest.KeepAlive = $false 

# send the ftp request to the server 
$ftpresponse = $ftprequest.GetResponse() 

# get a download stream from the server response 
$responsestream = $ftpresponse.GetRequestStream() 

# create the target file on the local system and the download buffer 
$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create) 
[byte[]]$readbuffer = New-Object byte[] 1024 

# loop through the download stream and send the data to the target file 
do{ 
    $readlength = $responsestream.Read($readbuffer,0,1024) 
    $targetfile.Write($readbuffer,0,$readlength) 
} 
while ($readlength -ne 0) 

$targetfile.close() 

回答

3

這裏有一個簡單的方法來使用​​類上傳文件到URI:

$targetUri = "ftp://10.21.109.202/Recieve/account_apps.zip" 
$sourcePath = "D:\AccountManager\Send\$RTMPHOST\account_apps.zip" 
$client = New-Object System.Net.WebClient 
$client.Credentials = New-Object System.Net.NetworkCredential($username,$password) 

$client.UploadFile($targetUri, $sourcePath) 
0

不要使用上傳的響應。 FTP協議不使用往返。

$stream = $ftprequest.GetRequestStream() 

$stream.Write(...) 

$stream.Close() 

$ftpresponse= $ftprequest.GetResponse() 
#... is success? 
$ftpresponse.Close() 

請求後的反應(與所有字節) 將是上傳成功或失敗。

相關問題