Skip to content

Instantly share code, notes, and snippets.

@sdwheeler
Created October 8, 2016 21:37
Show Gist options
  • Save sdwheeler/86afe5e72dee9c7805e19ba89c55d15a to your computer and use it in GitHub Desktop.
Save sdwheeler/86afe5e72dee9c7805e19ba89c55d15a to your computer and use it in GitHub Desktop.
Use powshell to download a file
function grab-url {
param(
[string[]]$filelist,
$headers,
[switch]$force
)
$curdir = (get-location).path
$wc = New-Object System.Net.WebClient
if ($headers) {
foreach ($key in $headers.keys) {
echo $key,$headers[$key]
$wc.Headers.Add($key,$headers[$key])
}
}
$filelist | %{
$uri = [system.uri]$_
$file = "{0}\{1}" -f $curdir,$uri.Segments[-1]
if ((!(Test-Path $file)) -or $force){
"Downloading $file"
$url = '{0}://{1}{2}' -f $uri.Scheme,$uri.Host,($uri.AbsolutePath -replace ":","%3A")
$wc.DownloadFile($url,$file)
} else {
"Skipping $file"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment