powershell transfer txt file always binary

Advertisement

dafinch
Guest

powershell transfer txt file always binary

I'm trying to transfer a text file from a windows server to a linux server. I am able to transfer the file but when I look at the file is a binary file.


$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Ascii

$transferResult = $session.PutFiles("c:\test.txt", "/tmp/", $FALSE, $transferOptions)

I've got the transfermode set to Ascii. Not sure what i'm doing wrong.

Reply with quote

Advertisement

dafinch
Guest

binary file

When I attempt to look at the file from a linux prompt I get a prompt that "text.txt" may be a binary file. See it anyway.

And instead of seeing Hello World! I see

<FF><FE>h^@e^@l^@l^@o^@ ^@w^@o^@r^@l^@d^@!^@^M^@
^@
test.txt (END)

Usually when I less a text file I don't see that warning and just see the text.

Reply with quote

Guest

Figured it out

Sorry I figured out my issue. The files needed to be converted from dos format cr/lf to unix. Once that was done they worked.

Reply with quote

martin
Site Admin
martin avatar

Re: Figured it out

Anonymous wrote:

Sorry I figured out my issue. The files needed to be converted from dos format cr/lf to unix. Once that was done they worked.
Well, that's what the ascii transfer mode should do for you automatically. Please attach a complete session log file.

Reply with quote

Advertisement

gpendleton
Joined:
Posts:
2
Location:
Nashville, TN

same issue, different user

here is the relevant code for our situation:
$transferOptions = new-object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::$xfermode
$transferResults = $session.GetFiles("$remoteFolder$file", $localFolder)
$transferResults | out-file $logfile -append

$xfermode has been confirmed to be 'ASCII':

[DBG]: PS C:\Windows\system32>> $xfermode
ASCII

[DBG]: PS C:\Windows\system32>> $transferOptions


PreserveTimestamp : True
FilePermissions :
TransferMode : Ascii
FileMask :
ResumeSupport : default
SpeedLimit : 0
OverwriteMode : Overwrite



here is the log file where it is using binary mode:

. 2018-05-21 09:46:07.277 --------------------------------------------------------------------------
. 2018-05-21 09:46:07.277 WinSCP Version 5.9.6 (Build 7601) (OS 6.3.9600 - Windows Server 2012 R2 Datacenter)
. 2018-05-21 09:46:07.277 Configuration: nul
. 2018-05-21 09:46:07.277 Log level: Normal
. 2018-05-21 09:46:07.277 Local account: NET\<user>_sa
. 2018-05-21 09:46:07.277 Working directory: F:\apps\WinSCP
. 2018-05-21 09:46:07.277 Process ID: 3844
. 2018-05-21 09:46:07.277 Command-line: "F:\apps\WinSCP\winscp.exe" /xmllog="C:\Users\<user>_sa\AppData\Local\Temp\wscp2E28.01010C54.tmp" /xmlgroups /xmllogrequired /nointeractiveinput /dotnet=596 /ini=nul /log="<log path>" /console /consoleinstance=_11816_20417935_753
. 2018-05-21 09:46:07.277 Time zone: Current: GMT-5, Standard: GMT-6 (Central Standard Time), DST: GMT-5 (Central Daylight Time), DST Start: 3/11/2018, DST End: 11/4/2018
. 2018-05-21 09:46:07.277 Login time: Monday, May 21, 2018 9:46:07 AM
. 2018-05-21 09:46:07.277 --------------------------------------------------------------------------
. 2018-05-21 09:46:07.277 Script: Retrospectively logging previous script records:
> 2018-05-21 09:46:07.277 Script: option batch on
< 2018-05-21 09:46:07.277 Script: batch on
< 2018-05-21 09:46:07.277 Script: reconnecttime 120
> 2018-05-21 09:46:07.277 Script: option confirm off
< 2018-05-21 09:46:07.277 Script: confirm off
> 2018-05-21 09:46:07.277 Script: option reconnecttime 120
< 2018-05-21 09:46:07.277 Script: reconnecttime 120
> 2018-05-21 09:46:07.277 Script: open sftp://<user@server> -hostkey="<hostkey>" -timeout=15
. 2018-05-21 09:46:07.277 --------------------------------------------------------------------------
. 2018-05-21 09:46:07.277 Session name: <user@server> (Ad-Hoc site)
. 2018-05-21 09:46:07.277 Host name: <server> (Port: 22)
. 2018-05-21 09:46:07.277 User name: <user> (Password: Yes, Key file: No, Passphrase: No)
. 2018-05-21 09:46:07.277 Tunnel: No
. 2018-05-21 09:46:07.277 Transfer Protocol: SFTP
. 2018-05-21 09:46:07.277 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2018-05-21 09:46:07.277 Disable Nagle: No
. 2018-05-21 09:46:07.277 Proxy: None
. 2018-05-21 09:46:07.277 Send buffer: 262144
. 2018-05-21 09:46:07.277 SSH protocol version: 2; Compression: No
. 2018-05-21 09:46:07.277 Bypass authentication: No
. 2018-05-21 09:46:07.277 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2018-05-21 09:46:07.277 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2018-05-21 09:46:07.277 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2018-05-21 09:46:07.277 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2018-05-21 09:46:07.277 Simple channel: Yes
. 2018-05-21 09:46:07.277 Return code variable: Autodetect; Lookup user groups: Auto
. 2018-05-21 09:46:07.277 Shell: default
. 2018-05-21 09:46:07.277 EOL: LF, UTF: Auto
. 2018-05-21 09:46:07.277 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2018-05-21 09:46:07.277 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2018-05-21 09:46:07.277 SFTP Bugs: Auto,Auto
. 2018-05-21 09:46:07.277 SFTP Server: default
. 2018-05-21 09:46:07.277 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2018-05-21 09:46:07.277 Cache directory changes: Yes, Permanent: Yes
. 2018-05-21 09:46:07.277 Recycle bin: Delete to: No, Overwritten to: No, Bin path:
. 2018-05-21 09:46:07.277 DST mode: Unix
. 2018-05-21 09:46:07.277 --------------------------------------------------------------------------
. 2018-05-21 09:46:07.277 Looking up host "<server>" for SSH connection
. 2018-05-21 09:46:07.277 Connecting to <server> port 22
. 2018-05-21 09:46:07.324 We claim version: SSH-2.0-WinSCP_release_5.9.6
. 2018-05-21 09:46:07.355 Server version: SSH-2.0-2.0
. 2018-05-21 09:46:07.355 We believe remote version has SSH-2 channel request bug
. 2018-05-21 09:46:07.355 Using SSH protocol version 2
. 2018-05-21 09:46:07.355 Have a known host key of type rsa2
. 2018-05-21 09:46:07.386 Using Diffie-Hellman with standard group "group14"
. 2018-05-21 09:46:07.386 Doing Diffie-Hellman key exchange with hash SHA-1
. 2018-05-21 09:46:07.574 Host key fingerprint is:
. 2018-05-21 09:46:07.574 ssh-rsa 1024 <key>
. 2018-05-21 09:46:07.574 Verifying host key rsa2 <key>
. 2018-05-21 09:46:07.574 Host key matches configured key
. 2018-05-21 09:46:07.574 Initialised AES-256 SDCTR client->server encryption
. 2018-05-21 09:46:07.574 Initialised HMAC-SHA-256 client->server MAC algorithm
. 2018-05-21 09:46:07.574 Initialised AES-256 SDCTR server->client encryption
. 2018-05-21 09:46:07.574 Initialised HMAC-SHA-256 server->client MAC algorithm
! 2018-05-21 09:46:07.636 Using username "<user>".
. 2018-05-21 09:46:07.668 Server offered these authentication methods: password
. 2018-05-21 09:46:07.668 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2018-05-21 09:46:07.668 Using stored password.
. 2018-05-21 09:46:07.668 Sent password
. 2018-05-21 09:46:07.715 Access granted
. 2018-05-21 09:46:07.715 Opening session as main channel
. 2018-05-21 09:46:07.746 Opened main channel
. 2018-05-21 09:46:07.855 Started a shell/command
. 2018-05-21 09:46:07.855 --------------------------------------------------------------------------
. 2018-05-21 09:46:07.855 Using SFTP protocol.
. 2018-05-21 09:46:07.855 Doing startup conversation with host.
> 2018-05-21 09:46:07.871 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2018-05-21 09:46:07.886 Type: SSH_FXP_VERSION, Size: 72, Number: -1
. 2018-05-21 09:46:07.886 SFTP version 6 negotiated.
. 2018-05-21 09:46:07.886 Server requests EOL sequence "\r\n".
. 2018-05-21 09:46:07.886 Server support information (supported2):
. 2018-05-21 09:46:07.886 Attribute mask: 33D, Attribute bits: 17, Open flags: 7
. 2018-05-21 09:46:07.886 Access mask: 3, Open block vector: 0, Block vector: 0, Max read size: 1048576
. 2018-05-21 09:46:07.886 Attribute extensions (0)
. 2018-05-21 09:46:07.886 Extensions (0)
. 2018-05-21 09:46:07.886 We will use UTF-8 strings as it is mandatory with SFTP version 4 and newer
. 2018-05-21 09:46:07.886 Getting current directory name.
. 2018-05-21 09:46:07.886 Getting real path for '.'
> 2018-05-21 09:46:07.886 Type: SSH_FXP_REALPATH, Size: 11, Number: 16
< 2018-05-21 09:46:07.934 Type: SSH_FXP_NAME, Size: 92, Number: 16
. 2018-05-21 09:46:07.934 Real path is '/'
. 2018-05-21 09:46:07.934 Startup conversation with host finished.
< 2018-05-21 09:46:07.934 Script: Active session: [1] <user@server>
> 2018-05-21 09:46:08.090 Script: pwd
< 2018-05-21 09:46:08.090 Script: /
> 2018-05-21 09:46:08.152 Script: get -nopermissions -preservetime -transfer="binary" -- "<path1>" "<path2>"
. 2018-05-21 09:46:08.152 Listing file "<path>".
> 2018-05-21 09:46:08.152 Type: SSH_FXP_LSTAT, Size: 37, Number: 263
< 2018-05-21 09:46:08.183 Type: SSH_FXP_ATTRS, Size: 55, Number: 263
. 2018-05-21 09:46:08.183 Promis;-;3750720;2018-05-10T16:06:42.000Z;3;"user" [0];"group" [0];rw-rw-rw-;1
. 2018-05-21 09:46:08.183 Copying 1 files/directories to local directory "<path>"
. 2018-05-21 09:46:08.183 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask:
. 2018-05-21 09:46:08.183 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
. 2018-05-21 09:46:08.183 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2018-05-21 09:46:08.183 File: '<path>' [2018-05-10T16:06:42.000Z] [3750720]
. 2018-05-21 09:46:08.183 Copying "<path>" to local directory started.
. 2018-05-21 09:46:08.183 Binary transfer mode selected.
. 2018-05-21 09:46:08.183 Checking existence of partially transfered file.
. 2018-05-21 09:46:08.183 Opening remote file.
> 2018-05-21 09:46:08.183 Type: SSH_FXP_OPEN, Size: 46, Number: 515
< 2018-05-21 09:46:08.230 Type: SSH_FXP_HANDLE, Size: 17, Number: 515
> 2018-05-21 09:46:08.230 Type: SSH_FXP_FSTAT, Size: 21, Number: 776
< 2018-05-21 09:46:08.262 Type: SSH_FXP_ATTRS, Size: 18, Number: 776
> 2018-05-21 09:46:08.262 Type: SSH_FXP_READ, Size: 29, Number: 1029
< 2018-05-21 09:46:10.387 Status code: 1
. 2018-05-21 09:46:10.387 262 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2018-05-21 09:46:10.387 Type: SSH_FXP_CLOSE, Size: 17, Number: 38660
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 30725
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 30981
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 31237
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 31493
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 31749
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 32005
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 32261
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 32517
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 32773
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 33029
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 33285
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 33541
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 33797
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 34053
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 34309
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 34565
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 34821
< 2018-05-21 09:46:10.387 Type: SSH_FXP_STATUS, Size: 31, Number: 35077
< 2018-05-21 09:46:10.418 Type: SSH_FXP_STATUS, Size: 31, Number: 35333
< 2018-05-21 09:46:10.418 Type: SSH_FXP_STATUS, Size: 31, Number: 35589
< 2018-05-21 09:46:10.418 Type: SSH_FXP_STATUS, Size: 31, Number: 35845
< 2018-05-21 09:46:10.418 Type: SSH_FXP_STATUS, Size: 31, Number: 36101
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 36357
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 36613
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 36869
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 37125
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 37381
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 37637
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 37893
< 2018-05-21 09:46:10.434 Type: SSH_FXP_STATUS, Size: 31, Number: 38149
< 2018-05-21 09:46:10.528 Type: SSH_FXP_STATUS, Size: 31, Number: 38405
. 2018-05-21 09:46:10.528 Preserving timestamp [2018-05-10T16:06:42.000Z]
. 2018-05-21 09:46:10.528 Transfer done: '<path>' [3750720]
> 2018-05-21 09:46:11.122 Script: exit
. 2018-05-21 09:46:11.122 Script: Exit code: 0
. 2018-05-21 09:46:11.122 Closing connection.
. 2018-05-21 09:46:11.122 Sending special code: 12
. 2018-05-21 09:46:11.122 Sent EOF message

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,453
Location:
Prague, Czechia

Re: same issue, different user

gpendleton wrote:

here is the relevant code for our situation:
$transferOptions = new-object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::$xfermode
$transferResults = $session.GetFiles("$remoteFolder$file", $localFolder)
$transferResults | out-file $logfile -append
Note that you never use the $transferOptions variable. It should be used as the 4th argument of Session.GetFiles:

$transferResults = $session.GetFiles("$remoteFolder$file", $localFolder, $False, $transferOptions)

See https://winscp.net/eng/docs/library_session_getfiles
and https://winscp.net/eng/docs/faq_library_parameters

Reply with quote

Advertisement

You can post new topics in this forum