add module download image using jobs

This commit is contained in:
Alex Kup 2023-11-04 01:41:34 +03:00 committed by GitHub
parent bcae4dfa7b
commit b5c6cfddc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,31 @@
function Download-Image {
param (
[Parameter(Mandatory = $True)]$url
)
$folder = $url -replace "http.+://" -replace "/","-" -replace "-$"
$path = "$home\Pictures\$folder"
if (Test-Path $path) {
Remove-Item $path -Recurse -Force
New-Item -ItemType Directory $path > $null
} else {
New-Item -ItemType Directory $path > $null
}
$irm = Invoke-WebRequest -Uri $url
foreach ($img in $irm.Images.src) {
$name = $img -replace ".+/"
Start-Job {
Invoke-WebRequest $using:img -OutFile "$using:path\$using:name"
} > $null
}
while ($True){
$status_job = (Get-Job).State[-1]
if ($status_job -like "Completed"){
Get-Job | Remove-Job -Force
break
}}
$count_all = $irm.Images.src.Count
$count_down = (Get-Item $path\*).count
"Downloaded $count_down of $count_all files to $path"
}
# Download-Image -url https://losst.pro/