如何在 PowerShell 中使用 Invoke-Webrequest 下载图片?
powershellmicrosoft technologiessoftware & coding
要使用 Invoke-WebRequest 命令从网页下载图片,我们可以使用结果中的 images 属性来获取图片 URL,然后使用该方法将图片下载到指定位置。假设我们有一个 URI: https://theautomationcode.com 来获取图片。
运行以下命令后,您将看到 Images 属性。
Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"
要获取图片 URL,
$req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/" $req.Images | Select -ExpandProperty src
输出
https://i1.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-9.png?resize=178%2C60&ssl=1 https://i0.wp.com/theautomationcode.com/wp-content/uploads/2020/11/image-10.png?resize=640%2C68&ssl=1
以上所有 URL 都指向图片,因此我们可以下载。
$wc = New-Object System.Net.WebClient
$req = Invoke-WebRequest -Uri "https://theautomationcode.com/feed/"
$images = $req.Images | Select -ExpandProperty src
$count = 0
foreach($img in $images){
$wc.DownloadFile($img,"C:\Temp\WebImages\img$count.jpg")
}
输出的图片将存储在 C:\temp\WebImages 文件夹中。

