Skip to content

Instantly share code, notes, and snippets.

@ploegert
Created June 9, 2015 14:27
Show Gist options
  • Select an option

  • Save ploegert/afa5f29b308806027ddd to your computer and use it in GitHub Desktop.

Select an option

Save ploegert/afa5f29b308806027ddd to your computer and use it in GitHub Desktop.
Downloads a file from a url and outputs the path to the file
#
# Downloads the given URL to a local file and outputs the path to the file
#
function Download
{
param([Parameter(Mandatory=$true)][string] $Url)
$webClient = $null
try
{
$uri = [URI] $url
$update = $uri.Segments[$uri.Segments.Count - 1]
$localFile = '{0}\{1}' -f $DSC_MainWorkingFolder, $update
if (Test-Path $localFile) {
throw New-HandlerTerminatingError `
-Code $DSC_Status.WmfInstallError.Code `
-Message ($DSC_Status.WmfInstallError.Message -f $update, (GetLogDirectory))
}
Write-Log "Downloading $Url ..."
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($Url, $localFile)
$localFile
}
finally
{
if ($webClient) {
$webClient.Dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment