gpt4 book ai didi

powershell - 使用PowerShell下载目录中的所有文件

转载 作者:行者123 更新时间:2023-12-03 00:18:14 25 4
gpt4 key购买 nike

我有一个有效的PowerShell脚本,该脚本遍历通过FTP访问的目录并打印其内容。该脚本如下所示:

$sourceuri = "<string>"
$targetpath = "<string>"
$username = "<string>"
$password = "<string>"

# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)

# set the request's network credentials for an authenticated connection
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false

# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()
$stream = $ftpresponse.getresponsestream()

$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding

$outputBuffer = ""
$foundMore = $false

## Read all the data available from the stream, writing it to the
## output buffer when done.
do
{
## Allow data to buffer for a bit
start-sleep -m 1000

## Read what data is available
$foundmore = $false
$stream.ReadTimeout = 1000

do
{
try
{
$read = $stream.Read($buffer, 0, 1024)
if($read -gt 0)
{

$foundmore = $true
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
}
catch
{
$foundMore = $false; $read = 0
}
}
while($read -gt 0)
}
while($foundmore)

$outputBuffer

我的实际目标不是仅列出文件,而是将它们下载到运行脚本的计算机上。我发现这有点棘手,因为我的循环仅引用字节而不是文件名。如何使用此循环下载目录中的所有文件,而不仅仅是列出它们?

最佳答案

我更喜欢使用像winscp一样的真正的FTP库,但是还有其他示例。 http://winscp.net/eng/docs/library_session_listdirectory#example

关于powershell - 使用PowerShell下载目录中的所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28351795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com