Created
October 8, 2016 21:37
-
-
Save sdwheeler/86afe5e72dee9c7805e19ba89c55d15a to your computer and use it in GitHub Desktop.
Use powshell to download a file
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
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