我在嘗試使用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()