WinSCP scripting novice needs help with ASCII file transfer

Posted on 2013-09-27
Medium Priority
Last Modified: 2013-10-01
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 = "XX.XXX.net"
 $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
Question by:First Last
  • 2
  • 2
LVL 16

Expert Comment

ID: 39528874
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 "XX.XXX.net" /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
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39528930
Did you try changing the protcol FTP?

$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp

Author Comment

by:First Last
ID: 39528993
@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.
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39529003
Then you should use binary transfer and make sure that your source file contains CRLF.
LVL 16

Accepted Solution

AlexPace earned 2000 total points
ID: 39529010
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.

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Welcome to 2018! Exciting things lie ahead in the world of tech. To start things off, we compiled great member articles on how to stay safe, ways to learn, and much more! Read on to start your new year right.
The Windows Firewall provides an important layer of protection and a rich interface to configure it. Unfortunately, it lacks item level filtering. This article details my process of implementing firewall-as-code to reduce GPO bloat.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question