[sgmb id=”1″]Why?
I am changing some special hosts file entries according to different networks, I am connecting to.
One is with Direct Access (DA) into my companies’ network, that works with IPv4 address resolution. The other one is directly connecting to the office’s network, using IPv6.
So I have to change my entries by hand, to reflect address resolution, every time, when I am changing between these two networks… and that is really annoying.
After a couple of years (right…! doing this day by day with the goal, finding a way out next day (every day), I passed over years ) and many, difficult to track, issues, that could easy by solved by not forgetting to change theses entries in hosts file, I tried to find a solution….
…and here it is!
It is that simple, that I shouldn’t post it here, to save my face 🙂. But I think, I am not alone with that.
Windows Tasks Scheduler is the key. So, let me explain:
Think of a simple hosts file entry:
# 10.12.35.2 wlan.my.intern 192.168.2.1 home.my.intern
(Currently I am connected by DA over IPv4, therefore I uncomment the second line and commented out the first one.)
For automatically changing these settings to the invert, I can (and of course, there are other possible solution) create a Tasks, that runs on detected NetworkProfile change. With a little powershell script the right settings in the hosts file will be modified. So have an eye on the following instuction:
- Open Windows Tasks-Scheduler
- Create new Task by right clicking somewhere in Task Schedulers tree
-
In next dialog enter a Name for the task
- (it’s up to you, to decide, whether to choose running with logged on user or not)
- Making changes to hosts file is only with administrative privileges possible, so click “Run with highest privileges“
- Also set the configuration in respect to the running machine
-
Switch to tab “Triggers“
- Select at “Begin the task:” On an event
- Search for Microsoft-Windows-NetworkProfile/Operational at “Log:“
- On “Source:” select NetworkProfile
- As “Event-ID:” enter 1000 (means “Network changed”)
-
OK… coming slowly to an End…
- Switch one tab further to “Actions“
- Leave “Start a program” in “Action:“
- Now we want a Powershell script to be triggered on the networkchanged-event
- So… add as “Program/Script” name PowerShell.exe
- As “Add arguments (optional):” add script’s path
-
If you are not willing to spend the time into selecting the right event, but want to achieve the same result here is a simpler solution
- Switch to tab “Conditions“
- Select last checkbox and choose the network that triggers
- Save everything an go to next step….
The triggered powershell script
Now, everything is ready to run something an the networkchange event. So here come the script (I do not have to mention, that there are more elegant ways):
$profile = (Get-NetConnectionProfile).Name $hostsPath = "$env:windir\System32\drivers\etc\hosts" Write-Host $profile if($profile -contains 'MyWLan'){ # Uncomment lines for current profile $hosts = get-content $hostsPath $hosts = $hosts | Foreach {if ($_ -match '^\s*#\s*(.*?\d{1,3}.*?wlan.*)') {$matches[1]} else {$_}} $hosts | Out-File $hostsPath -enc ascii # Comment lines for the other profile: $hosts = get-content $hostsPath $hosts | Foreach {if ($_ -match '^\s*([^#].*?\d{1,3}.*?home.*)') {"# " + $matches[1]} else {$_}} | Out-File $hostsPath -enc ascii } else{ # Uncomment lines for current profile $hosts = get-content $hostsPath $hosts = $hosts | Foreach {if ($_ -match '^\s*#\s*(.*?\d{1,3}.*?home.*)') {$matches[1]} else {$_}} $hosts | Out-File $hostsPath -enc ascii # Comment lines for the other profile: $hosts = get-content $hostsPath $hosts | Foreach {if ($_ -match '^\s*([^#].*?\d{1,3}.*?wlan.*)') {"# " + $matches[1]} else {$_}} | Out-File $hostsPath -enc ascii }
(again my example hosts file)
# 10.12.35.2 wlan.my.intern 192.168.2.1 home.my.intern
In the first line the script retrieves the network profile name (the name, that is also listed in that combobox in picture 6.b.). In relation to the name of the current netw.profile, all lines in the hosts file, that contain the string “wlan” will be uncommented and others with “home” commented.
The result after network switch is :
10.12.35.2 wlan.my.intern # 192.168.2.1 home.my.intern