Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 16, 2010 2:11 PM
Hello,
I am new to Powershell and I am being asked to create a script that moves a folder from an FTP server to my local machine. Here is what I have so far, but this is throwing an error.
C:\ get-ftp -server servername -user User -password Password - ssh explicit
Copy-Item \servername\c$\scripts c:\test
Any help would be appreciated,
Dave
Dave SQL Developer
All replies (11)
Thursday, September 16, 2010 2:40 PM âś…Answered
This is what you are looking for I think
http://stackoverflow.com/questions/265339/whats-the-best-way-to-automate-secure-ftp-in-powershell
Thursday, September 16, 2010 2:20 PM
not really sure I follow you.
who's Get-FTP are you using?
the copy item is from the admin share on a server to the local machine, if that's accurate, why do you need the FTP command?
Thursday, September 16, 2010 2:31 PM
Thanks for your response...
I am new to Powershell, so I am not sure if this is even correct. I need a script that moves a folder from an FTP site using SSH/SFTP connection to my local machine. I grabbed this code off the web, so the logic is probably off. If you know a better way , please let me know...
Thanks again
Dave SQL Developer
Thursday, September 16, 2010 3:02 PM
Thanks, you are a life saver! I hate when my boss asks me to learn a new language for a project that has a hard deadline...Dave SQL Developer
Thursday, September 16, 2010 3:19 PM
have to keep things new and exciting :)
If you have any other questions don't hesitate to ask.
Justin
Thursday, September 16, 2010 4:54 PM
Ok, I am getting an error that says "You cannot call a method on a null-valued expression", and the screen runs non-stop with this message. Not sure what the heck I did... LOL
Here is what I have for a script...
Code Snippet:
$sourceuri = "ftp://ftp.servername/c$/folder"
$targetpath = "C:\folder"
$username = "Username"
$password = "Password"
# 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]::DownloadFile
$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.GetResponseStream()
# 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()
Dave SQL Developer
Thursday, September 16, 2010 5:48 PM
The joy of powershell is that you can execute this line by line in the console to see whats causing the problem :)
based on the way the source URI is, it looks like you are trying to get a file from the hidden admin share (UNC path) and not so much the FTP service itself.
Friday, September 17, 2010 6:36 PM
Thanks for the help JRich, but I am still having issues with this script. When I change the variables to my FTP box, I get errors saying that the variables are unrecognized (or something to that extent) so I am not sure if it's the actual syntax of the path I am using, or if the script was designed for a different protocol...
Does anyone have a script that copies a folder from an FTP server using SSH\SFTP connection to a local box? I have a dealine for EOD today, and I am running out of options..
Thanks for your understanding..
Dave
Dave SQL Developer
Friday, September 17, 2010 9:02 PM
The only things you have changed are the top 4 variables? sourceuri, username/password and local path?
are you downloading a single file, or everything in a folder?
paste any error you get here.
Tuesday, September 21, 2010 2:29 PM
Hi Jrich,
I was on a few Powershell forums, and I was told that you cannot move files to and from and FTP site using SSH connection. They are saying that I need a third party tool. I am questioning this because the guy I was talking to represented a company that sold that tool. Is this possible in Powershell?
Here is a script that I glued together from the previous code. I assume I have to specify the connection somewhere, but I am not sure of the syntax or where I should place it.
Code Snippet:
$sourceuri = "ftp://ftp.servername/c$/folder"
$targetpath = "C:\folder"
$username = "Username"
$password = "Password"
Copy-Item $sourceuri $targetpath -username $username -password $password
Thanks
Dave SQL Developer
Tuesday, September 21, 2010 3:19 PM
well you cant natively do it in powershell, meaning, copy-item isnt going to work.
you can however dig in to .NET and use system.net.ftpwebrequest class. This is what makes powershell so awesome. if PS cant do it, just dig in to the .NET framework and there is usually a way to do it... so the code I pasted to you before is all .NET, you could have just as well done it in C# or VB.NET... not much actual PS going on there...
that being said, its also not all that easy.. you have to deal with byte streams and, as I found, a weird method property...
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
if you look that up on MSDN its actually listed as System.Net.WebRequestMethods+Ftp... it took me some time to figure out why you use a + there and I wasn't able to find why...
so, if you have a .NET object for FTPWebRequest called $ftprequest and you set its method to downloadfile, you can download from it... if you are trying to upload there is a method for that (I use the word method losely because its not really your typical programming method(), but a property called method of the ftpwebrequest object..
here are the different FTP Methods (commands) you can set..
http://msdn.microsoft.com/en-us/library/system.net.webrequestmethods.ftp_members.aspx
you'll see there are FTP commands such as ListDirectory and Uploadfile etc etc..
the code I gave you above will work for downloading from the FTP server. you'll just need to change a few variables... the top 4 should be it really...
this code will work without any changes provided you know the full path to the file you are trying to download.
if you need to figure out the file name before downloading... or if you need to upload then this code will need some working...
you should just forget about copy-item, not going to help you here :)