Powershell ftp上传错误530未登录

时间:2010-02-25 12:18:20

标签: powershell ftp

我正在努力让PowerShell脚本工作。我是PowerShell的新手,所以可能会遗漏一些愚蠢的东西。

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

我收到以下错误:

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

我可以通过IE轻松连接到它,所以想到其他可能是错误的,所以在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();

C#完美无缺,不确定为什么这不起作用,希望有人能够指出我的错误

修改

解决了它,新的它将是一个愚蠢的东西。密码中有一个“$”符号,它在双引号内,但我没有意识到它需要被转义,只是根本没有想到它。具有讽刺意味的是,我不得不更改密码等,以便发布。

3 个答案:

答案 0 :(得分:2)

来自原始海报Jon

解决了它,新的它将是一个愚蠢的东西。密码有&#34; $&#34;签到它,它是在双引号内,但我没有意识到它需要被逃脱,只是没有想到它。具有讽刺意味的是,我不得不更改密码等,以便发布。

答案 1 :(得分:1)

只需从用户名或密码中删除双引号,因为有时$ sign会导致问题。每次使用单引号都很好。

答案 2 :(得分:0)

在我的情况下,原因是FTP服务器需要SSL。在请求上更改此属性可以解决我的问题:

request.EnableSsl = true;