Я изо всех сил пытаюсь заставить сценарий 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# отлично, не уверенный, почему это не работает и надеется, что кто-то смог указывать на мою ошибку
Править
Решенный это, новый это было бы что-то глупое. Пароль имел "$", входят в систему он, это было в двойных кавычках, но я не понял, что его должны будут оставить, просто не думал о нем вообще. Иронически я должен был изменить пароль и т.д. так, чтобы было безопасно отправить.