Working in VPN Land, or how I learned to use PowerShell to change Proxy Settings

If you are a consultant, then you learn to live with working in multiple client environments, and that often means VPNs of every shape, color, and size. Most corporate IT shops are used to dealing with their internal employees and we itinerants often flummox them. I have been working at a client that not only requires a VPN, but also to get things to work I often have to hit their proxy server (which is on an internal non-routable IP Address). Normally this means that I have to open up IE, click on Tools | Internet Options | Connections | LAN Settings and click on the Use a Proxy Server option box.

This does work, but it gets old after a while. So I wondered…could I use PowerShell to change my Proxy Settings. A quick search lead me to a couple of blog entries that talked about it and decided to adapt it to my own uses.

First I wrote a PowerShell that id the change.

Set-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 1

 

It’s pretty straightforward, you just Set the Item Property to 1 (to turn it on and 0 to turn it off. Initially I wrote two PS scripts. One was turning it on and one off. But that was just silly, so I wrote a routine that looked to see if I was attached to the clients VPN and if so, turn the Proxy On, and if not turn the Proxy Off. To accomplish this I run ipconfig and pipe the results to findstr to see if the client network name is found.

$matches = @(ipconfig /all | findstr "client.network.com")

 

Then a quick if statement to see if it was found and either turn the Proxy on or off. It looks like this:

$matches = @(ipconfig /all | findstr "corp.gaf.com")

if($matches.Count -gt 0){

Write-Output "Connected to Client"

Set-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 1

}

else{

Write-Output "NOT Connected to Client"

Set-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 0

}

 

Now, when I run this, it will connect or disconnect me lickety-split. The last step was to put this in a CMD file for ease of exexution.

powershell.exe ./enableproxy.ps1

pause

That is it. I just hit that CMD file when I need to connect and let PowerShell do the rest. Now, one can get much more complex. For example FatBeard has a post about how you can enter a location and have it set what you need. Another post from Jeff Wouter uses a CSV file with all of the setup information to look at the network name and then set the Proxy Configuration. I didn’t need all of that, so I went simple. Hope this simple version helps you, and if you want more…check out FatBeard or Jeff Wouter.

One Response

  1. ???? ???? December 6, 2014

Leave a Reply

x

We use cookies to ensure the best possible experience on our website. Detailed information on the use of cookies on this site is provided in our Privacy and Cookie Policy. Further instruction on how to disable our cookies can be found there.