We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

I need help downloading a file from an FTP server using Powershell 1.0.  Here is the code that I am using with the error at the bottom.

Medium Priority
1,693 Views
Last Modified: 2013-12-09
The IP address on line one is invalid, however in my script I am using a  a valid IP, path and file name. I also tried it on a second anonymous FTP server and received the same error. Thanks.

1) $url = "ftp://66.111.122.100/patcher/"     
2) $file = "appver.ini"
3) $user = ""
4) $password = ""
5) $client = New-Object System.Net.WebClient
6) $credentials = New-Object System.Net.NetworkCredential -arg $user, $password
7) $client.Credentials = $credentials
8) $client.DownloadFile($url, $file)

ERROR
Exception calling "DownloadFile" with "2" argument(s):
"The requested URI is invalid for this FTP command."
At :line:8 char:20
+ $client.DownloadFile <<<< ($url, $file)
Comment
Watch Question

Commented:
1) Webclient is not an FTP client. Unless your site allows http downloads it most likely will not work
2) I would use netcmdlets located here: http://www.nsoftware.com/powershell/netcmdlets/default.aspx
3) Powershell Zone: http://www.experts-exchange.com/Programming/Languages/Scripting/MSH-Monad/

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
I had looked at /n Software commandlets but prefer to use native functionality in Powershell rather than a proprietary solution. (My employer won't pay for it.) The Microsoft documentation indicates that System.Net.WebClient will support HTTP or FTP but do not provide any FTP examples, so it may be in the context that you have indicated, only if the site allows HTTP transfers. I'll experiment a bit more and will post my solution if I can work it out. Thanks for the response though, the net commandlets from \n software would definitely work.
Commented:
I think there is a free Powershell tool from sapien.com you may want to check out. I do not know the link.

Author

Commented:
I'll check out Sapien's extensions. Thanks again.

Author

Commented:
After a bit of experimenting I was able to work this out. This Powershell code snippet successfully downloads a file via FTP. My error with the original script was using an incomplete Url and invalid destination. The URL must be complete and include protocol, domain, path & filename. The $file variable is the location where the file is to be saved complete with drive, path and filename.

# this works !!!

$url = "ftp://somedomain.com/test/GSA_0158.csv"
$file = "c:\test\GSA_0158.ini"
$username = "johnh"
$password = "mypassword"
$client = New-Object System.Net.WebClient
$credentials = New-Object System.Net.NetworkCredential -arg $username, $password
$client.Credentials = $credentials
$client.DownloadFile($url, $file)

Commented:
That is good to know!

Author

Commented:
I'm new to this forum and am just learning how it works. Thanks for your suggestions. I'm still working on some other ftp issues and will need some other help.

Commented:
Your script does not work if you download it to a diferent folder name.

$url = "ftp://somedomain.com/test/GSA_0158.csv"
$file = "c:\test\GSA_0158.ini"

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: 227 Entering Pa
ssive Mode     (10,1,1,19)
."
At H:\PowerShell\MyScripts\Ftp2Local.ps1:10 char:21
+ $client.DownloadFile <<<< ($url, $file)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.