0

I am trying to download jenkins.war file through powershell on windows server, but I get 401 Unauthorized error in downloadFile. I passed correct network credentials still getting this error. Below is my code:

$source = "http://mirrors.jenkins.io/war-stable/latest/jenkins.war"
$destination = "$jenkinsDir\jenkins.war"
$client = new-object System.Net.WebClient

$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential("admin", "P@ssw0rd")
$credCache.Add("http://mirrors.jenkins.io/war-stable/latest/jenkins.war", "Basic", $creds)
$client.Credentials = $credCache

$client.downloadFile($source, $destination)

How to get rid of this 401 error?

I also tried this link System.Net.WebClient doesn't work with Windows Authentication but no luck.

user2025527
  • 109
  • 2
  • 12
  • I previously used the below script to download files - does this work for you? In my experience proxies and passing credentials can be a fickle thing... $UserPath = "$($env:USERPROFILE)" $site = 'https://www.sqlite.org/download.html' $proxy = [System.Net.WebRequest]::GetSystemWebProxy().GetProxy($site) Invoke-webrequest -Uri $site -Proxy $proxy -ProxyUseDefaultCredentials -TimeOutSec 10 -ErrorAction:SilentlyContinue -OutFile $UserPath\Desktop\download.html If that works it should just be a matter of changing $site and $outfile – Shovers_ Dec 03 '18 at 13:54
  • Why are you using `WebClient`? Look at `Invoke-WebRequest -OutFile` – Maximilian Burszley Dec 03 '18 at 17:03
  • @Shovers_ Got same error: Invoke-webrequest : Proxy Authorization Required Description: Authorization is required for access to this proxy Error Code: NT_STATUS_LOGON_FAILURE User Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.15063.1387 Error Detail: Logon failure Invoke-webrequest -Uri $site -Proxy $proxy -ProxyUseDefaultCredential ... + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId: WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand – user2025527 Dec 04 '18 at 06:24
  • I'd have to guess then that the account your using either doesn't have access to get out to that site, or some other thing in between is blocking it, either a firewall or some other security product. If you do "runas" on Iexplore.ex (or your browser) and use those credentials do you get out to the site or 407 ? – Shovers_ Dec 04 '18 at 09:55
  • 1
    @Shovers_ If I open above url in chrome/IE, it asks me to Sign-in to proxy url, after entering same credentials, the download of file starts, so its working with same credentials in browser but not with powershell. Also If I click on Remember me in IE then above powershell script runs successfully by adding client.UseDefaultCredentials = $true but I cannot do this for all other environments. – user2025527 Dec 04 '18 at 12:30
  • the only other thing I can think of is instead of UseDefaultCredentials is to prompr for them using Get-Credential https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6 – Shovers_ Dec 05 '18 at 12:19

0 Answers0