9
votes

I am struggling to get a PowerShell script to work. I am very new to PowerShell so could be missing something stupid.

$sourceuri = "ftp://ftp.example.com/myfolder/myfile.xml"
$username = "user"
$password = "password"

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

$credentials = New-Object System.Net.NetworkCredential($username,$password)
# set the request's network credentials for an authenticated connection
$ftprequest.Credentials = $credentials

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

# read in the file to upload as a byte array
$content = gc -en byte $fileName
$ftprequest.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftprequest.GetRequestStream()
$rs.Write($content, 0, $content.Length)
# be sure to clean up after ourselves
$rs.Close()
$rs.Dispose()

I get the following error:

Exception calling "GetRequestStream" with "0" argument(s): "The remote server returned an   error: (530) Not logged in."
At C:\temp\copyfile.ps1:63 char:35
+ $rs = $ftprequest.GetRequestStream( <<<< )

I can connect via IE to it easily so thought maybe something else was wrong so did this quickly in C#:

        string filePath = @"C:\temp\myfile.xml";
        string FTPAddress = @"ftp://ftp.example.com/myfolder";
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + Path.GetFileName(filePath));
        request.Method = WebRequestMethods.Ftp.UploadFile;
        string username = "user";
        string password = "password";
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        FileInfo file = new FileInfo(filePath);
        request.ContentLength = file.Length;
        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;

        FileStream fs = file.OpenRead();

        Stream strm = request.GetRequestStream();
        contentLen = fs.Read(buff, 0, buffLength);
        while(contentLen !=0 )
        {
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
        }

        strm.Close();
        fs.Close();

The C# works perfectly, not sure why this isn't working and hoping someone might be able to point out my error

EDIT

Solved it, new it would be something stupid. The password had a "$" sign in it, it was within double quotes but I didn't realize it would need to be escaped, just didn't think of it at all. Ironically I had to change the password etc so that it was safe to post.

3
I can't see anything. The only difference is that you use UsePassive in you C# solution. To avoid errors you might use Set-StrictMode -version ... to avoid uninitialized variables (=typos), but in this case I think it won't bring anything new.stej
Hi Stej, thanks for the response. I am glad its not just me, I tried to replicate the C# in PowerShell.Jon
Many errors lead to 'problem$' with double quotes :)stej
Thank you for posting the edit to solved it. This just helped me, as our password contained a $ as well.steve.lippert
So. 6 years later ... You should answer your own question and mark it answered ... so it will get off the top of the list of unanswered questions.Jaykul

3 Answers

3
votes

In my case the reason was that the FTP server required SSL. Changing this property on the request solved my problem:

request.EnableSsl = true;
2
votes

From the original poster Jon:

Solved it, new it would be something stupid. The password had a "$" sign in it, it was within double quotes but I didn't realize it would need to be escaped, just didn't think of it at all. Ironically I had to change the password etc so that it was safe to post.

1
votes

just remove the double quote from username or password because sometimes $ sign causes problem. Its good to use single quote every time.