Changing Proxy Settings with a Powershell Logon Script
17/02/2012 2 Comments
For some reason the automatically Detect Settings options for the Internet Connection doesn’t work at work. On the other hand coming at a customer’s site or at home, the saved settings prevent to connect to the internet. As a role model for system administrators I had to prove my lazy nature with creating a script to do this for me. So here I go. I first create a Set-Proxy.ps1 file and open it with notepad this will hold my Powershell script. Now I need to determine where I am. There are several ways of doing it but I decided to use the DNS name of the network I was on with my NIC wich I identify with the Mac-address:
$varMac = GWMI -cl “Win32_NetworkAdapterConfiguration” | where {$_.macaddress -eq “xx:x:xx:xx:xx:xx”}
The location of the internet settings are stored in the next variable:
$varPath=”HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings”
Now we can build the if construction that will set or remove the Connection Settings properties:
if ($varmac.dnsdomain -eq “mydomain.com”)
{
set-itemproperty -path $varPath proxyenable -value 1
set-itemproperty -path $varPath proxyserver -value “proxy.mydomain.com:8080”
}
else
{
set-itemproperty -path $varPath proxyenable -value 0
remove-itemproperty -path $varpath proxyserver
}
So far the Powershell part. Because Powershell its primary purpose is a scripting language for admin tasks it’s not easy to script it into a login script. So I encapsulated it in a batch file. The bat file is stored in:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
The code inside the logon.bat looks like this:
powershell.exe C:\<Path>\Set-Proxy.ps1