WinSCP scripting novice needs help with ASCII file transfer

Hello scripting experts! I'm a network admin attempting to use WinSCP scripting for a file transfer. The problem I am encountering is the format. The file needs to be in standard ASCII with CRLF at the end of each line. Right now when viewed in a text editor those are missing. I have made several attempts at changing the script parameters but have seen no change which leads me to believe the problem is my syntax. The script is below, I would greatly appreciate any feedback on how to modify it to force the file to transfer in ASCII format so I get the CRLF formatting to show up. Thanks in advance!

 # Load WinSCP .NET assembly
 # Use "winscp.dll" for the releases before the latest beta version.
 [Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCP.dll") | Out-Null

 # Setup session options
 $sessionOptions = New-Object WinSCP.SessionOptions
 $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
 $sessionOptions.HostName = ""
 $sessionOptions.UserName = "USERNAME"
 $sessionOptions.Password = "PASSWORD"
 $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

 $session = New-Object WinSCP.Session

 # Connect

 $localPath = "C:\XXXX File\"
 $remotePath = "/Inbox/"
 $file = "file.txt"

 # Transfer Options set to ASCII
 $transferOptions = New-Object WinSCP.TransferOptions
 $transferOptions.TransferMode = [WinSCP.TransferMode]::ASCII

 # Download the file and throw on any error
 ($remotePath + $file),
 ($localPath + $file)).Check()
 # Disconnect, clean up

 exit 0
 catch [Exception]
 Write-Host $_.Exception.Message
 exit 1
First LastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Not all SFTP connections can do ASCII mode.  The original SFTP had no ASCII mode at all, it was all binary.  Some FTP/SFTP client software are able to "fake it" by replacing \r or \n with \r\n.  The newer SFTP spec has a text mode that should do this automatically but both the client and server must support the protocol extensions.

I don't know if WinSCP supports the newer extensions or if it can fake it if the server doesn't support it.  I think Robo-FTP can fake it... a Robo-FTP script that does what your script does would look something like this:
FTPLOGON "" /user="USERNAME" /pw="PASSWORD" /servertype=SFTP /trust=all
FTPCD "/Inbox"
RCVFILE "file.txt" /type=ASCII

Open in new window

If you saved that as a file named download.s then you could launch it from a command line like this:
   robo-ftp.exe -s"download.s"

... you can launch so it is invisible by also adding the -v command line argument
Gerwin Jansen, EE MVETopic Advisor Commented:
Did you try changing the protcol FTP?

$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
First LastAuthor Commented:
@AlexPace - Do you know if robo-ftp supports the sftp protocol?  That is the only type that they will allow.  The version of WinSCP I'm running (current non-beta) does support both ASCII and Binary but I'm not 100% sure my syntax is right.

@gerwinjansen - the company I am transferring the file from requires a sftp connection unfortunately.
Gerwin Jansen, EE MVETopic Advisor Commented:
Then you should use binary transfer and make sure that your source file contains CRLF.
Yes, that Robo-FTP example I wrote above does SFTP.  The thing about ASCII for SFTP is that there are two kinds.... simulated ASCII and the new text mode.  It is possible for an SFTP client to support the new text mode but not the simulated ASCII.  The problem is not all SFTP servers support text mode.  I'm not sure how WinSCP does it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.