1

I'm having a problem here trying to upload a file with powershell. What I'm trying to do is shoot a .zip file to a target user account on a destination server. Destination server is running IIS FTP 7.5 and has user isolation enabled with a datachannel port range of 5500-6500 (if that might matter).

Here's my code below - the problem is I get that I cannot call a method on a null-valued expression on the $responsestream request. Please let me know if I goofed up, looking all over the net... have had so many problems uploading files!

Also I want to say I used a download script that I'm converting to upload since I haven't had any success with the upload scripts I've tried out before.

$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()

2 Answers 2

3

Here's a simpler approach to uploading a file to an URI using the WebClient class:

$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)
Sign up to request clarification or add additional context in comments.

Comments

0

Dont use a response for the upload. The FTP Protocol dont use round trips.

$stream = $ftprequest.GetRequestStream()

$stream.Write(...)

$stream.Close()

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

The response AFTER the request (with all bytes) would be the upload success or fail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.