I am trying to get a PowerShell script to work. I am very new to PowerShell, so something stupid could have been missed.
$sourceuri = "ftp://ftp.example.com/myfolder/myfile.xml"
$username = "user"
$password = "password"
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)
$credentials = New-Object System.Net.NetworkCredential($username,$password)
$ftprequest.Credentials = $credentials
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftprequest.UseBinary = 1
$ftprequest.KeepAlive = 0
$content = gc -en byte $fileName
$ftprequest.ContentLength = $content.Length
$rs = $ftprequest.GetRequestStream()
$rs.Write($content, 0, $content.Length)
$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 through IE to it easily, so maybe something else is wrong, so it's fast 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();
C # works fine, not sure why this is not working and hopes someone can point out my error
EDIT
I decided, but that would be something stupid. The password had a "$" sign, it was in double quotes, but I did not understand that it should be avoided, I just did not think about it at all. Ironically, I had to change my password, etc., so that it was safe to publish messages.