2012-01-27 112 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() 

请求后的反应(与所有字节) 将是上传成功或失败。

相关问题