First Last
asked on
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!
try
{
# Load WinSCP .NET assembly
# Use "winscp.dll" for the releases before the latest beta version.
[Reflection.Assembly]::Loa dFrom("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.SshHostKey Fingerprin t = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx :xx:xx:xx: xx:xx:xx:x x"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOpti ons)
$localPath = "C:\XXXX File\"
$remotePath = "/Inbox/"
$file = "file.txt"
# Transfer Options set to ASCII
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferM ode = [WinSCP.TransferMode]::ASC II
# Download the file and throw on any error
$session.GetFiles(
($remotePath + $file),
($localPath + $file)).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
try
{
# Load WinSCP .NET assembly
# Use "winscp.dll" for the releases before the latest beta version.
[Reflection.Assembly]::Loa
# 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.SshHostKey
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOpti
$localPath = "C:\XXXX File\"
$remotePath = "/Inbox/"
$file = "file.txt"
# Transfer Options set to ASCII
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferM
# Download the file and throw on any error
$session.GetFiles(
($remotePath + $file),
($localPath + $file)).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
Did you try changing the protcol FTP?
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
ASKER
@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.
@gerwinjansen - the company I am transferring the file from requires a sftp connection unfortunately.
Then you should use binary transfer and make sure that your source file contains CRLF.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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