Created
August 1, 2016 14:39
-
-
Save Dan1el42/57855af4fe33485e63672a3c41644ad0 to your computer and use it in GitHub Desktop.
A possible solution for https://powershell.org/forums/topic/how-to-specify-which-file-i-want-to-download/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module BitsTransfer | |
$Uri = 'http://download.nai.com/products/licensed/superdat/nai/Brazilian' | |
$DestinationPath = 'D:\Temp' | |
$Response = Invoke-WebRequest -UseBasicParsing -Uri $Uri | |
if ($Response.StatusCode -eq 200) { | |
$Matches = [regex]::Matches($Response.RawContent, '(\d+xdat\.exe)"') | |
if ($Matches.Count -ge 1) | |
{ | |
$LastMatch = $Matches[$Matches.Count - 1] | |
$FileName = $LastMatch.Groups[$LastMatch.Groups.Count - 1] | |
$Source = "${Uri}/${FileName}" | |
$Destination = Join-Path -Path $DestinationPath -ChildPath $FileName | |
$Job = Start-BitsTransfer -Source $Source -Destination $Destination -Asynchronous | |
Do | |
{ | |
$Percentage = [int](($Job.BytesTransferred*100) / $Job.BytesTotal) | |
Write-Progress -Activity "Downloading file..." -CurrentOperation "${Percentage}% complete" | |
Start-Sleep -Seconds 1 | |
} While ($Job.JobState -in 'Connecting', 'Transferring') | |
Complete-BitsTransfer -BitsJob $Job | |
"Definition file downloaded to ${Destination}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment