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 Of course by using PowerShell (well duh!)
When I plug my laptop in a network it will find a network name… let’s say customer1.lan 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 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
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>”
$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.
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:
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
Hope you find this one as useful as I have