Skip to content

SQL Ryan

Some SQL and some data viz

  • About Ryan
  • Twitter
  • LinkedIn

Tag: login

Setting default web proxy in Powershell

Powershell doesn’t seem to respect the default proxy settings carried through from the OS/Browser (possibly a bug like this one or this one, if you feel like reading). If you to fetch content from a website and your corporate network has a proxy in place, you’ll get an error like this one:

PS > Invoke-WebRequest "http://www.contoso.com"
Invoke-WebRequest:
Proxy Web Gateway - Notification
Request Blocked

(Insert scary message about trying to circumvent your corporate proxy here and you'll get in super-big trouble if this is malicious. For real, don't do it!) If you're not trying to break the rules, please contact your helpdesk for assistance.

URL: break_line("http://www.contoso.com/");

You can resolve this by specifying the proxy settings as parameters on your request:

PS > Invoke-WebRequest "http://www.contoso.com" -Proxy "http://proxy.yourcorp.com:8080" -ProxyUseDefaultCredentials
StatusCode        : 200
StatusDescription : OK
Content           : <html><head><title>Microsoft Corporation</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"></meta <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><meta name="...(and so on)...

However, you don’t have to specify that proxy settings on every single request – you can just set the default proxy settings in your Powershell session. The first line below sets the default proxy, and the second line tells it use the currently logged-in user to access the proxy (this isn’t necessary if your proxy doesn’t require login, but most do):

PS > [System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://proxy.yourcorp.com:8080', $true)
PS > [System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

Once you’ve set those proxy settings for the default client, you can make a regular old web call without specifying the proxy details:

PS > Invoke-WebRequest "http://www.contoso.com"
StatusCode        : 200
StatusDescription : OK
Content           : <html><head><title>Microsoft Corporation</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"></meta <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><meta name="...(and so on)...

But if you’re the kind of person who doesn’t like setting your proxy settings every single time you launch Powershell, you’re in luck! Powershell, like other shells, has a default profile script that it runs when you log in and you can add the lines above to that script and they’ll execute by default every time you launch Powershell. When you try to open it, it will even create a blank script for you if one doesn’t already exist:

PS > notepad $PROFILE

Then just dump in the contents of the commands above, plus a little note to remind you that you set this up so if you have problems later, you’ll know where to check 🙂

# Set proxy settings manually since it doesn't pick it up from IE
[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://proxy.yourcorp.com:8080', $true)
[System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
Write-Output "Proxy configuration set via profile. To update, run 'notepad `$PROFILE' at the PS prompt."
Author Ryan McCauleyPosted on April 11, 2024Categories PowershellTags login, Powershell, proxyLeave a comment on Setting default web proxy in Powershell

Recent Posts

  • Setting default web proxy in Powershell
  • Printing/Escaping a variable name in Powershell
  • Converting two-column DataFrame to a dictionary in Python
  • Run Windows Troubleshooting Wizards manually (from the command line)
  • “There is no data in the database for @catalogitemid” in Power BI Report Server

Tags

  • .NET
  • Active Directory
  • Alerts
  • ChromeOS
  • Column
  • Computer Troubleshooting
  • DataFrame
  • Dictionary
  • DLL
  • Email
  • Encryption
  • Excel
  • Filtering
  • goldengate
  • Hive
  • HTTP
  • HTTPS
  • IIS
  • IT Entitlement
  • M-code
  • Microsoft Access Database Engine
  • ODBC
  • oracle
  • pandas
  • partner
  • PBIRS
  • Power BI
  • Power BI Report Server
  • Power BI Server
  • Power BI Service
  • Power Query
  • Powershell
  • proxy
  • Python
  • replication
  • Report Usage
  • Running an IT department
  • Server Side Property
  • sql compact
  • sql server
  • SSP
  • SSRS
  • T-SQL
  • Updates
  • Variables

Categories

  • .NET (10)
  • Big Data (3)
  • Data Reporting (10)
  • Encryption (3)
  • Excel (2)
  • IT (3)
  • Open Source Projects (1)
  • Power BI (12)
  • Power Query (4)
  • Powershell (3)
  • Python (2)
  • SQL Compact (3)
  • SQL Reporting Services (8)
  • SQL Server (40)
  • T-SQL Scripts (23)
  • Team Foundation Server (1)
  • Troubleshooting (2)
  • Uncategorized (3)
  • Windows Clustering (7)
  • Windows Server (17)
  • About Ryan
  • Twitter
  • LinkedIn
SQL Ryan Proudly powered by WordPress