I've been searching for a very simple method of FTP-ing a file from within Powershell. My search came up with a lot of complex answers, which I'm sure have their place. However, I was looking for the simplest way to just grab a file via FTP from a server that requires a username and password.
It turns out you can simply rely on Windows native FTP client. The ftp command in Windows will take a -s switch with a file spec. That file needs to be a list of the responses for your ftp session. One per line and in order, just like a script.
So to grab the file myfile.csv from the server 192.168.1.1 with a username and password of tomtom and letmein you would use this:
ftp -s:myscript.txt 192.168.1.1
Where the myscript.txt file would look like this: tomtom letmein get myfile.csv quit
Yes, you can use that command straight from the powershell prompt or from a DOS prompt, assuming that your script file is in the directory that you are in at the time when its run. Of course, you can use full paths if you need to.
Sure, I know that having my FTP username and password in a plain text file sitting on my server is a security risk. I handled that by make the privs on that account very limited. It can read-only, and only see one directory, which is where the file I need sits and nothing more. So, the risk is quite manageable. YMMV.
I hope that helps folks that just need to grab a file quickly and easily via FTP and Powershell.
|