{"action":"closed","issue":{"url":"https://api.github.com/repos/aspnet/Home/issues/72","labels_url":"https://api.github.com/repos/aspnet/Home/issues/72/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Home/issues/72/comments","events_url":"https://api.github.com/repos/aspnet/Home/issues/72/events","html_url":"https://github.com/aspnet/Home/issues/72","id":35692263,"number":72,"title":"kvminstall.ps1 fails behind authenticated firewall with (407) Proxy Authentication Required error","user":{"login":"3DHDSoft","id":626239,"avatar_url":"https://avatars.githubusercontent.com/u/626239?v=3","gravatar_id":"","url":"https://api.github.com/users/3DHDSoft","html_url":"https://github.com/3DHDSoft","followers_url":"https://api.github.com/users/3DHDSoft/followers","following_url":"https://api.github.com/users/3DHDSoft/following{/other_user}","gists_url":"https://api.github.com/users/3DHDSoft/gists{/gist_id}","starred_url":"https://api.github.com/users/3DHDSoft/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/3DHDSoft/subscriptions","organizations_url":"https://api.github.com/users/3DHDSoft/orgs","repos_url":"https://api.github.com/users/3DHDSoft/repos","events_url":"https://api.github.com/users/3DHDSoft/events{/privacy}","received_events_url":"https://api.github.com/users/3DHDSoft/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/Home/labels/3+-+Done","name":"3 - Done","color":"CCCCCC"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2014-06-13T17:46:26Z","updated_at":"2015-01-08T22:14:31Z","closed_at":"2015-01-08T22:14:31Z","body":"Behind an authenticated firewall, like most corporate firewalls are, the kvminstall.ps1 fails with (407) Proxy Authentication Required error.\r\nI borrowed the solution from the Chocolatey team, by replacing the $webClient.DownloadFile with Get-WebFile function that properly handles firewalls, user agents, passthru and quite execution.\r\nkvminstall.ps1\r\n===========\r\n```powershell\r\n$kvmSource = \"https://raw.githubusercontent.com/aspnet/Home/master/\"\r\n$kvmPs1 = \"kvm.ps1\"\r\n$kvmCmd = \"kvm.cmd\"\r\n$tempPath = Join-Path $env:TEMP \"kvminstall\"\r\n$kvmPs1Path = Join-Path $tempPath $kvmPs1\r\n$kvmCmdPath = Join-Path $tempPath $kvmCmd\r\n$kvmPs1Url = $kvmSource + $kvmPs1\r\n$kvmCmdUrl = $kvmSource + $kvmCmd\r\n\r\n$PSScriptRoot = (Split-Path $myInvocation.MyCommand.Definition -Parent)\r\n& $PSScriptRoot\\Get-WebFile.ps1\r\n\r\nWrite-Host \"Using temporary directory: $tempPath\"\r\nif (!(Test-Path $tempPath))\r\n{\r\n\tmd $tempPath | Out-Null\r\n}\r\n\r\nWrite-Host \"Downloading KVM.ps1 to $kvmPs1Path\"\r\nGet-WebFile $kvmPs1Url $kvmPs1Path\r\n\r\nWrite-Host \"Downloading KVM.cmd to $kvmCmdPath\"\r\nGet-WebFile $kvmCmdUrl $kvmCmdPath\r\n\r\nWrite-Host \"Installing KVM\"\r\n& $kvmCmdPath setup\r\n```\r\nGet-WebFile.ps1\r\n=============\r\n```powershell\r\n# Usage:\r\n#  - Get url to a file: Get-WebFile \"url\" \"flePath\"\r\n#  - Get url content:   Get-WebFile \"url\" -Passthru\r\nfunction Global:Get-WebFile\r\n{\r\n\t[CmdletBinding()]\r\n\tParam (\r\n\t\t[Parameter(Position=1, Mandatory=$true,  ValueFromPipelineByPropertyName=$true)] [string] $url = \"\", #(Read-Host \"The URL to download\"),\r\n\t\t[Parameter(Position=2, Mandatory=$false,  ValueFromPipelineByPropertyName=$true)] [string] $fileName = $null,\r\n\t\t[Parameter(Position=3, Mandatory=$false, ValueFromPipelineByPropertyName=$true)] [string] $userAgent = \"kvm command line\",\r\n\t\t[Parameter(Position=4, Mandatory=$false, ValueFromPipelineByPropertyName=$true)] [switch] $Passthru,\r\n\t\t[Parameter(Position=5, Mandatory=$false, ValueFromPipelineByPropertyName=$true)] [switch] $quiet\r\n\t)\r\n\tProcess\r\n    {\r\n\t\tWrite-Debug \"Running 'Get-WebFile' for $fileName with url:`'$url`', userAgent: `'$userAgent`' \";\r\n\t\t#if ($url -eq '' return)\r\n\t\t$req = [System.Net.HttpWebRequest]::Create($url);\r\n\t\t#to check if a proxy is required\r\n\t\t$webclient = new-object System.Net.WebClient\r\n\t\tif (!$webclient.Proxy.IsBypassed($url))\r\n\t\t{\r\n\t\t\t$creds = [net.CredentialCache]::DefaultCredentials\r\n\t\t\tif ($creds -eq $null)\r\n\t\t\t{\r\n\t\t\t\tWrite-Debug \"Default credentials were null. Attempting backup method\"\r\n\t\t\t\t$cred = get-credential\r\n\t\t\t\t$creds = $cred.GetNetworkCredential();\r\n\t\t\t}\r\n\t\t\t$proxyaddress = $webclient.Proxy.GetProxy($url).Authority\r\n\t\t\tWrite-host \"Using this proxyserver: $proxyaddress\"\r\n\t\t\t$proxy = New-Object System.Net.WebProxy($proxyaddress)\r\n\t\t\t$proxy.credentials = $creds\r\n\t\t\t$req.proxy = $proxy\r\n\t\t}\r\n\r\n\t\t#http://stackoverflow.com/questions/518181/too-many-automatic-redirections-were-attempted-error-message-when-using-a-httpw\r\n\t\t$req.CookieContainer = New-Object System.Net.CookieContainer\r\n\t\tif ($userAgent -ne $null)\r\n\t\t{\r\n\t\t\tWrite-Debug \"Setting the UserAgent to `'$userAgent`'\"\r\n\t\t\t$req.UserAgent = $userAgent\r\n\t\t}\r\n\t\t$res = $req.GetResponse();\r\n\r\n\t\tif ($fileName -and !(Split-Path $fileName))\r\n\t\t{\r\n\t\t\t$fileName = Join-Path (Get-Location -PSProvider \"FileSystem\") $fileName\r\n\t\t}\r\n\t\telseif ((!$Passthru -and ($fileName -eq $null)))\r\n\t\t{\r\n\t\t    if ((($fileName -ne $null) -and (Test-Path -PathType \"Container\" $fileName)))\r\n\t\t    {\r\n\t\t\t    [string]$fileName = ([regex]'(?i)filename=(.*)$').Match( $res.Headers[\"Content-Disposition\"] ).Groups[1].Value\r\n\t\t\t    $fileName = $fileName.trim(\"\\/\"\"'\")\r\n\t\t\t    if (!$fileName)\r\n\t\t\t    {\r\n\t\t\t\t    $fileName = $res.ResponseUri.Segments[-1]\r\n\t\t\t\t    $fileName = $fileName.trim(\"\\/\")\r\n\t\t\t\t    if(!$fileName)\r\n\t\t\t\t    {\r\n\t\t\t\t\t    $fileName = Read-Host \"Please provide a file name\"\r\n\t\t\t\t    }\r\n\t\t\t\t    $fileName = $fileName.trim(\"\\/\")\r\n\t\t\t\t    if(!([IO.FileInfo]$fileName).Extension)\r\n\t\t\t\t    {\r\n\t\t\t\t\t    $fileName = $fileName + \".\" + $res.ContentType.Split(\";\")[0].Split(\"/\")[1]\r\n\t\t\t\t    }\r\n\t\t\t    }\r\n\t\t\t    $fileName = Join-Path (Get-Location -PSProvider \"FileSystem\") $fileName\r\n\t\t    }\r\n\t\t}\r\n\t\tif ($Passthru)\r\n\t\t{\r\n\t\t\t$encoding = [System.Text.Encoding]::GetEncoding( $res.CharacterSet )\r\n\t\t\t[string]$output = \"\"\r\n\t\t}\r\n\r\n\t\tif ($res.StatusCode -eq 200)\r\n\t\t{\r\n\t\t\t[long]$goal = $res.ContentLength\r\n\t\t\t$reader = $res.GetResponseStream()\r\n\t\t\tif ($fileName)\r\n\t\t\t{\r\n\t\t\t\t$writer = new-object System.IO.FileStream $fileName, \"Create\"\r\n\t\t\t}\r\n\t\t\t[byte[]]$buffer = new-object byte[] 1048576\r\n\t\t\t[long]$total = [long]$count = [long]$iterLoop =0\r\n\t\t\tdo\r\n\t\t\t{\r\n\t\t\t\t$count = $reader.Read($buffer, 0, $buffer.Length);\r\n\t\t\t\tif ($fileName)\r\n\t\t\t\t{\r\n\t\t\t\t\t$writer.Write($buffer, 0, $count);\r\n\t\t\t\t}\r\n\t\t\t\tif ($Passthru)\r\n\t\t\t\t{\r\n\t\t\t\t\t$output += $encoding.GetString($buffer,0,$count)\r\n\t\t\t\t}\r\n\t\t\t\telseif (!$quiet)\r\n\t\t\t\t{\r\n\t\t\t\t\t$total += $count\r\n\t\t\t\t\tif ($goal -gt 0 -and ++$iterLoop%10 -eq 0)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tWrite-Progress \"Downloading $url to $fileName\" \"Saving $total of $goal\" -id 0 -percentComplete (($total/$goal)*100) \r\n\t\t\t\t\t}\r\n\t\t\t\t\tif ($total -eq $goal)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tWrite-Progress \"Completed download of $url.\" \"Completed a total of $total bytes of $fileName\" -id 0 -Completed \r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} while ($count -gt 0)\r\n\r\n\t\t\t$reader.Close()\r\n\t\t\tif ($fileName)\r\n\t\t\t{\r\n\t\t\t\t$writer.Flush()\r\n\t\t\t\t$writer.Close()\r\n\t\t\t}\r\n\t\t\tif ($Passthru)\r\n\t\t\t{\r\n\t\t\t\treturn $output\r\n\t\t\t}\r\n\t\t}\r\n\t\t$res.Close();\r\n    }\r\n}\r\n```"}}