This is redacted version of a powershell script I had to come up with recently to fix an issue with a web application in our environment.

$hostsPath = "$env:windir\System32\drivers\etc\hosts"
$ip = ipconfig | findstr /i IPv4 |Out-string
$ip = $ip.TrimStart("IPv4 Address. . . . . . . . . . . : ")
$ip = $ip.TrimEnd("`n")
$ip = $ip.TrimEnd("`r")
Clear-Content $hostsPath
ipconfig /flushdns
Add-Content $hostsPath "$ip"

I will walk through what is going on here…

The general form of “$nameofvariable = something” is how you declare a variable in powershell code.

So this script declares two variables and does a few interesting things with them…

First $hostpath has been set to the windows “hosts” file as that is the file I want to manipulate. Using this variable helps keep things clean.

$ip is declared several times, manipulating the variable contents to clean them up a bit each time. I will walk through what I am doing here…

The command ipconfig when run outputs something like this to the command line:

Windows IP Configuration

Ethernet adapter Local Area Connection 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Local Area Connection 5:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::e004:8e1a:ce89:ce8d%49
   IPv4 Address. . . . . . . . . . . :
   Subnet Mask . . . . . . . . . . . :
   Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection 4:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

This is a very “stupid simple” specific use-case script in that it will only work for environment where you have one connected adapter with a single assigned IPv4 address.The above output is similar to what I got when running this on the server in question. In this case, I was only interested in collecting one line of output the first time the $ip variable is declared. So to strip away all of the junk I pipe (i.e. “|”) the output of ipconfig to the findstr (find string) command and tell it I am looking for the string that contains “IPv4” and then pipe that to the out-string commandlet. The “/i” tells it to ignore case.

The final result is that the $ip variable now contains only this info:

IPv4 Address. . . . . . . . . . . :

Great, but I still need to strip down to just the IP address and I have some leading characters I need to get rid of and a trailing carriage return and “new line”. To get rid of those I use the “Trimstart” and “trimend” functions with the $ip variable. So the next line trims off the “IPv4 Address. . . . . . . . . . . : ” and the next line then trims off and “new lines” and the final line trims off “carriage returns”. The final result is that my $ip variable only contains:

Which is exactly what I need for my hosts file.

The next commandlet is “clear-content” and this simply empties an existing file at a specified path without deleting the file. I am emptying the hosts file specified by the $hostpath variable I set at the start.

The next command tells my system to empty its DNS resolver cache. This ensures there are no lingering DNS entries and the hosts file will be authoritative.

Finally, I use the “Add-content” commandlet against my hosts file (defined by the variable $hostpath again) and I am entering a new line that consists of the IP address I pulled and the URL of the site I want to resolve to that IP.

This script works perfectly if set to run during startup of an Azure web role cloud service/vm. If you need to modify a local file in your web role every time the system starts, a powershell script is a good way to go about it. In typical fashion I had to look at about 6 different sites to piece this solution together so I am archiving here for my own personal future use and hopefully your benefit 🙂



1 of 1

One comment on: Powershell Script for Pulling the Local Server IP and Dynamically Updating the Windows Hosts file

  1. httpJunkie

    Could I run this at startup on my dev machine? If so, do you know how I would do that?
    Using the first answer from this stack overflow question, I will try. Obviously I will backup the host file even though it doesn’t really contain anything but comments and one entry.

Leave a Reply