0

So I am connecting to a z/OS ftp server. I am using the code below to try and download a file using Powershell 2.0. This code works on Windows 7, but on either Windows Server 2003 or Windows Server 2008 R2, I get an error:

System.Net.WebException: The remote server returned an error: (501) Syntax error in parameters or arguments.

No parameters were changed, no firewall or other factors seem to be at play (that I can tell) other than the difference in the OS.

The code I am using is as follows:

function Get-FTPFile ($Source,$Target,$UserName,$Password) 
 { 
      $ftprequest = [System.Net.FtpWebRequest]::create($Source) 
      $ftprequest.Credentials = New-Object      System.Net.NetworkCredential($username,$password) 
      $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
      $ftprequest.UseBinary = $false 
      $ftprequest.KeepAlive = $false 

      $ftpresponse = $ftprequest.GetResponse() 
      $responsestream = $ftpresponse.GetResponseStream() 

      $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) 
      [byte[]]$readbuffer = New-Object byte[] 1024 

      do{ 
          $readlength = $responsestream.Read($readbuffer,0,1024) 
          $targetfile.Write($readbuffer,0,$readlength) 
      } 
      while ($readlength -ne 0) 

      $targetfile.close() 
  }

Yes the UseBinary must be false. I am downloading a text file and it comes out all garbled if useBinary is set to true.

Has anyone seen this before?

Bitfiddler
  • 3,942
  • 7
  • 36
  • 51
  • DO you have any poershell.config on either of the systems? Are you running 32 or 64 bit on all of the systems? Are you sure .Net version that powershell is using is the same on all systems? Googling around seems to be suggesting that problem is with the path you are passing. – Andrey Marchuk Aug 23 '12 at 06:43
  • We are going to try matching the .Net versions. We have 2.0 on the servers and I am using 4.0 for development. Both systems are 64-bit but we tried to run the script in both 64 and 32 bit powershell. No custom powershell.configs. I have also read about the path problems, unfortunately there seem to be no solutions for the path issue other than using the commandline ftp.exe with a batch file (this is suboptimal). I will post back when we have tried installing a newer version of the framework. – Bitfiddler Aug 23 '12 at 16:09
  • 1
    Just an update, installed .net 3.5 which works on Windows 7, but still does not work on Windows Server 2008 R2. Looks like I will have to use a commandline batch file. – Bitfiddler Aug 27 '12 at 16:40

0 Answers0