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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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
Hope you find this one as useful as I have
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.
Hi Jimmy,
Good catch, I’ve changed the script a little, it should work now.
Jeff.
Hey Jeff,
saw your change there. Thanx. so it’s that easy? Thanx.
Sure, you don’t have to be a rocket scientist to do PowerShell 😀
Goodmorning Jeff,
I’m a newbie to powershell and your post is one of the reasons i want to start using powershell. Could you help me.. What parts of the script do i need to change to make it work for my environment. I already edited the .csv file.
kind regards
Marc
Hi Marc,
The CSC file should be all you’ll need to change.
If it’s not working, drop me an email at contact @ jeffwouters dot nl and I’ll help where I can.
Jeff.
Thanks for such wonderful script.
How do I set my proxy according to my NIC IP?
Say I have a IP block that is 192.168.0.0/24
and another one is different.
any clue?
Regards
-akter
Sure, use the Get-NETIPAddress cmdlet and a switch statement.
Something like this (fill in the appropriate actions in the switch):
$NIC = Get-NetIPAddress -InterfaceIndex 7
switch ($NIC.IPAddress) {
{$_ -like “10.0.*” } { ‘10.0.x.x’ ; break }
{$_ -like “192.168.*” } { ‘192.168.x.x’ ; break }
}
Nice Script, thanks a lot!
For systems that change network connections while being logged on, you can configure a Scheduled Task to start the script that is triggered whenever networks change.
Source NetworkProfile Event ID 10000 = connection established
Source NetworkProfile event ID 10001 is connection disconnected
Might be useful for others who do not want to run this script (only) during logon.
Looking for a script to retrieve if “Use a Proxy Server for you LAN” is enable or disabled from a list of computers on a CSV file. Also how to enable the checkbox or disabled it.
Thank You,
What about when connecting with VPN to another network?