首页 > 解决方案 > 如何使多个 Invoke-WebRequest 命令附加到同一个文件?

问题描述

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

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"

有小费吗?谢谢。

标签: windowspowershellhttpscripting

解决方案


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

# 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
    
}

推荐阅读