本文介紹了使用 WebClient 在 PowerShell 腳本中將 FTP 從二進制更改為 ascii的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
簡單的 PowerShell 腳本.它下載一個文件(二進制)沒有問題.我需要它在ASCII中.
Simple PowerShell script. It downloads a file (in binary) with no issues. I need it in ascii.
$File = "c: empftpfile.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);
推薦答案
WebClient
不支持ascii/text FTP模式.
The WebClient
does not support ascii/text FTP mode.
使用 FtpWebRequest
而是設置 .UseBinary代碼> 為假.
Use FtpWebRequest
instead and set .UseBinary
to false.
$File = "c: empftpfile.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)
$responsestream.CopyTo($targetfile)
$targetfile.close()
參考:在 PowerShell 中自動化安全 FTP 的最佳方法是什么?
請注意,WebClient
在內部使用 FtpWebRequest
,但不公開其 .UseBinary
屬性.
Note that the WebClient
uses the FtpWebRequest
internally, but does not expose its .UseBinary
property.
這篇關于使用 WebClient 在 PowerShell 腳本中將 FTP 從二進制更改為 ascii的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!