Proxy Authentication with Powershell

Today I just wanted to write a quick note about how to authenticate to a proxy within a Powershell script.  Most corporate networks have proxies, and most also require authentication.  The problem is, if you try to connect to any Internet resource, such as installing a Powershell module via the Install-Module command or downloading a file or other info from a website, you may see something like this:

Powershell Proxy Auth Error

Powershell Proxy Auth Error

Luckily, there’s an easy fix.  We simply tell our Web Client object what credentials to use.  In the below example, it will use the current user.

That’s right, just one additional line of code solves our proxy authentication woes.

Hope this helps, and happy scripting!

Bookmark the permalink.

3 Comments

  1. Hi Ryan,

    I could cry. This works great if we assume the proxy server uses network credentials. Mine doesn’t. There is a separate set of credentials. I’ve tried using the -ProxyCredential switch, but it only allows for the use of the username. I get a popup that prompts for the password, which I dutifully enter, and then I get:

    Invoke-WebRequest : Unable to connect to the remote server
    At line:1 char:1
    + Invoke-WebRequest -Proxy http://proxyserver:8888 -ProxyCredential UserName https …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
    eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

    (Names have been changed to protect the innocent.) I can turn the proxy off (which I’ve done for testing) and my request will then go through. If I don’t provide the proxy credentials, I get the standard 407 error.

    I’ve searched, and searched, and searched, and searched, and searched, and… well, you get the idea. I cannot, anywhere [so far] find someone who can recommend how to set the proxy server AND credentials when the latter are not the credentials of the user logged into the desktop. I’m sure the method exists. But my forehead is bloody from all the banging on the wall to try and figure it out. Can you help?

    p.s. No, I do not have the option of modifying the proxy itself to accept my domain credentials. Politics.

    • Kyle,

      Ah yes, the never-ending struggle that is IT politics. Like the Jedi and Sith, locked in an eternal struggle.

      If you create a WebProxy object, assign the uri and credentials, then add that to the WebClient object, it should work. like so:

      $Client = New-Object -TypeName System.Net.WebClient
      $proxy = New-Object -TypeName System.Net.WebProxy

      $proxy.Address = [uri]”http://proxy.domain.local:9090″
      $proxy.Credentials = (new-object System.Net.NetworkCredential(‘Username’, ‘Password’, ‘Domain’))

      $Client.Proxy = $proxy

      $Client.DownloadString(‘https://www.ryandrane.com’)

      I hope this helps, let me know how it goes, not sure I’ll be able to sleep until I figure this one out.

  2. Is there any way to adapt this to: New-AzureStorageContainer

Leave a Reply

Your email address will not be published. Required fields are marked *