• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1457
  • Last Modified:

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.

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)
0
KeyBasher
Asked:
KeyBasher
  • 4
  • 3
2 Solutions
 
BSonPoshCommented:
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/
0
 
KeyBasherAuthor 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.
0
 
BSonPoshCommented:
I think there is a free Powershell tool from sapien.com you may want to check out. I do not know the link.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KeyBasherAuthor Commented:
I'll check out Sapien's extensions. Thanks again.
0
 
KeyBasherAuthor 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)
0
 
BSonPoshCommented:
That is good to know!
0
 
KeyBasherAuthor 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.
0
 
IT-OPSCommented:
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
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now