t在PowerShell脚本中将FTP从二进制文件更改为asci

t在PowerShell脚本中将FTP从二进制文件更改为asci

本文介绍了使用WebClient在PowerShell脚本中将FTP从二进制文件更改为ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的PowerShell脚本。它下载一个没有问题的文件(二进制文件)。我需要它在ascii。

WebClient 不支持ascii / text FTP模式。



使用取而代之,并将为false。


$ b

  $ File =c:\temp\ftpfile.txt
$ ftp = FTP://名为myUsername:[email protected]/'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()

参考:


$请注意, WebClient 使用 FtpWebRequest 内部,但不公开它的 .UseBinary 属性。


Simple PowerShell script. It downloads a file (in binary) with no issues. I need it in ascii.

$File = "c:\temp\ftpfile.txt"
$ftp = "ftp://myusername:[email protected]/'report'";
$webclient = New-Object -TypeName System.Net.WebClient;
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
$webclient.DownloadFile($uri, $File);
解决方案

The WebClient does not support ascii/text FTP mode.

Use FtpWebRequest instead and set .UseBinary to false.

$File = "c:\temp\ftpfile.txt"
$ftp = "ftp://myusername:[email protected]/'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

do
{
    $readlength = $responsestream.Read($readbuffer, 0, 1024)
    $targetfile.Write($readbuffer, 0, $readlength)
}
while ($readlength -ne 0)

$targetfile.close()

Reference: What's the best way to automate secure FTP in PowerShell?


Note that the WebClient uses the FtpWebRequest internally, but does not expose its .UseBinary property.

这篇关于使用WebClient在PowerShell脚本中将FTP从二进制文件更改为ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:41