12 Apr 2012 @ 5:41 PM 

Yet again one in the category of useful and practical scripts.
Because I’m a consultant I get around… and attach my laptop to every customers network. Since most of my customers are using proxy servers, I was starting to get a bit annoyed to set the proxy settings for the customer of that specific day in Internet Explorer.

And when I get home, I‘ll have to change those proxy settings again since I’m not using the same proxy configuration at my internal home network as my customer(s).

So… this morning I found some time to solve this irritation Emoticon met brede lach Of course by using PowerShell (well duh!) Verhitte emoticon

When I plug my laptop in a network it will find a network name… let’s say customer1.lan Glimlach So, based on a network name, I know which proxy settings to use, right?
Now what if I could use a simple script which compared the network name it found on the NIC to a CSV file and if a match is found correctly configures the appropriate proxy settings…now THAT would be useful Verhitte emoticon And since I want this script to be run at logon, I do not want anything to do with script parameters or something, just execute the darn thing Knipogende emoticon

Here’s the script (Set-Proxy.ps1) which configures the proxy by comparing the network name found by the local NIC to the ones in the CSV (CustomerNetworks.csv) file:

function Configure-Proxy ($Proxy, $Port)
{
# Function that actually does the configuring of the proxy settings.
Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyEnable -Value 1
Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyServer -Value $Proxy”:”$Port
Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyOverride -Value “<local>”
}

Function Set-Proxy
{
  $Customers = Import-Csv CustomerNetworks.csv
  $NetworkConfig = @(ipconfig.exe /all)
  $Match = $false
  ForEach ($customer in $customers)
  {
    $Port = $Customer.Port
    $Proxy = $Customer.Proxy
    $Network = $Customer.Network
    # If the network name found by the NIC matches to one in the CSV the proxy
    # settings are configured by parsing the right values to the Configure-Proxy function.
    if ($NetworkConfig -like “*$Network*”)
    {
      Configure-Proxy $Proxy $Port
      $Match = $true
    }
    # If no value applies, disable the proxy configuration.
    elseif (!$match)
    {
      Set-ItemProperty “HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings” -Name ProxyEnable -Value 0
    }
  }
}

And here is an example of the content of the CSV file:

Customer,Network,Proxy,Port
Customer1,customer1.com,proxy.customer1.com,8080
customer2,customer2.com,isa.customer2.com,8080
jeffwouters,jeffwouters.lan,tmg.jeffwouters.nl,8080

Note that the first values are the name of the customer. These values are not used in the script but I do find them useful since they make it easy to find the proxy settings when you need to find them Knipogende emoticon

Hope you find this one as useful as I have Verlegen emoticon

Post to Twitter

Posted By: Jeff Wouters
Last Edit: 06 Mar 2013 @ 07:49 AM

EmailPermalink
Tags


  • Jimmy

    Thanks for the script. It works, but not when there are more then two entries in the CSV. The scripts sets the values for each entry in the CSV. But when the first is found, the proxy is set, then it reads the second line (foreach) and that network name is not found, the value is set to “0″. I am new to powershell, so it will take a while untill I have a solution. If you have one…. Please leave an update :)
    Thanks in advance.

  • http://www.jeffwouters.nl Jeff Wouters

    Hi Jimmy,

    Good catch, I’ve changed the script a little, it should work now.
    Jeff.

  • Jimmy

    Hey Jeff,
    saw your change there. Thanx. so it’s that easy? Thanx.

  • http://www.jeffwouters.nl Jeff Wouters

    Sure, you don’t have to be a rocket scientist to do PowerShell :-D


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 249
  • Comments » 429
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.