【问题标题】:How do I make multiple Invoke-WebRequest commands append to the same file?如何使多个 Invoke-WebRequest 命令附加到同一个文件?
【发布时间】:2021-08-03 21:02:17
【问题描述】:

我才刚开始,所以我一定错过了一些东西。 下载每个文件都很好:我只是不知道如何附加到同一个输出文件。这是我目前拥有的。

Invoke-WebRequest -Uri "https://website.com/part1.bin" -OutFile "D:\stuff\bigfile.bin"
Invoke-WebRequest -Uri "https://website.com/part2.bin" -OutFile "D:\stuff\bigfile.bin"
Invoke-WebRequest -Uri "https://website.com/part3.bin" -OutFile "D:\stuff\bigfile.bin"

有什么建议吗?谢谢。

【问题讨论】:

  • 也许您可以将结果通过管道传递给Out-File -Path «wherever» -Append,而不是使用-OutFile
  • 不确定您是否可以在二进制文件上使用Out-File。也许最好将它们作为单独的部分下载,然后将它们作为字节加入内存中。
  • @JeffZeitlin - 谢谢,但我不知道该怎么做;能给我举个例子吗?
  • 假设Out-File 将允许二进制数据,您将在第一部分使用Invoke-WebRequest -URI "https://website.com/part1.bin" | Out-File -Path "D:\stuff\bigfile.bin",对第二部分使用Invoke-WebRequest -URI "https://website.com/part2.bin" | Out-File -Path "D:\stuff\bigfile.bin" -Append(第三部分,适当更改URI)。

标签: windows powershell http scripting


【解决方案1】:

这是我最终使用的。为我工作!

# Requires Powershell 6.0+ because of AsByteStream

# Example array containing URLs to combine
$arrUrls = @('https://website.com/part1.bin','https://website.com/part2.bin','https://website.com/part3.bin')
$tempFile = "temp.bin"
$outputFile = "wholefile.bin"

# For each url, download in a temp file, then append to output file
foreach ($myUrl in $arrUrls) 
{
    Invoke-WebRequest -Uri $myUrl -OutFile $tempFile
    $byteArray = Get-Content $tempFile -AsByteStream -Raw
    Add-Content $outputFile -Value $byteArray -AsByteStream
    
}

【讨论】:

    猜你喜欢
    • 2022-10-23
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2012-08-12
    • 1970-01-01
    • 2020-08-25
    相关资源
    最近更新 更多