Changing FTP from binary to ascii in PowerShell script using WebClient -


simple powershell script. downloads file (in binary) no issues. need in ascii.

$file = "c:\temp\ftpfile.txt" $ftp = "ftp://myusername:mypass@12.345.6.78/'report'"; $webclient = new-object -typename system.net.webclient; $uri = new-object -typename system.uri -argumentlist $ftp; $webclient.downloadfile($uri, $file); 

the webclient not support ascii/text ftp mode.

use ftpwebrequest instead , set .usebinary false.

$file = "c:\temp\ftpfile.txt" $ftp = "ftp://myusername:mypass@12.345.6.78/'report'";  $ftprequest = [system.net.ftpwebrequest]::create($ftp)  $ftprequest.method = [system.net.webrequestmethods+ftp]::downloadfile $ftprequest.usebinary = $false  $ftpresponse = $ftprequest.getresponse()  $responsestream = $ftpresponse.getresponsestream()  $targetfile = new-object io.filestream($file, [io.filemode]::create) [byte[]]$readbuffer = new-object byte[] 1024  {     $readlength = $responsestream.read($readbuffer, 0, 1024)     $targetfile.write($readbuffer, 0, $readlength) } while ($readlength -ne 0)  $targetfile.close() 

reference: what's best way automate secure ftp in powershell?


note webclient uses ftpwebrequest internally, not expose .usebinary property.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -