Update a hosts file via script

DevilsbaneDevilsbane Member Posts: 4,214 ■■■■■■■■□□
We have an application that is dependant on an entry being in the hosts file. Apparently they decided to update the software, but didn't let us know so now the hosts files are incorrect.

What I want to do is update these files using minimal user interaction. I quickly tossed the following together which will add a new line to the hosts file, but I'm not sure how I would go about removing the old entry.
echo Enter the name for the PC where the Hosts file needs to be updated.
echo.
SET /P Compname=[WSID:] 
SET filename=\\%Compname%\C$\WINDOWS\system32\drivers\etc\hosts
echo. >> %filename%
echo 127.0.0.1 [URL="http://www.myaddress.com"]www.myaddress.com[/URL] >> %filename%
start Notepad.exe %filename%
So all this does is takes the computer name and saves it to a variable called Compname. Then it sets the file path to that of the hosts file on the remote computer. I add a blank line (Because the cursor may not be on it's own line, and a blank line really wouldn't hurt anything) then I add in the new configuration (and I just made up some numbers to stick in here). Lastly I open the hosts file. This is how I could clean up the old entry and the blank line if one was left. But I'm thinking there would be a way to search the file and to remove an entry without needing to open the file.
Decide what to be and go be it.

Comments

  • ClaymooreClaymoore Member Posts: 1,637
    Okay, I'll ask the stupid question. Why aren't you using DNS?

    I'm going to assume that it's something you can't control and you need to do it this way. Have you considered just copying a new hosts file instead of trying to edit the existing? It's easy to do through group policy preferences or even a login script.

    If you can't copy, will the edit script run in the user's security context? Do they have the rights to edit the hosts file?
  • DevilsbaneDevilsbane Member Posts: 4,214 ■■■■■■■■□□
    Claymoore wrote: »
    Okay, I'll ask the stupid question. Why aren't you using DNS?

    That is actually the same question I asked. And the person I asked didn't know, he just said someone told him to do it that way.

    Yes, copying would be much easier, but I don't know what if anything is already contained in them. I am actually running the script from my machine, using the C$ to get onto their system, so yes the script runs.
    Decide what to be and go be it.
  • ClaymooreClaymoore Member Posts: 1,637
    Devilsbane wrote: »
    That is actually the same question I asked. And the person I asked didn't know, he just said someone told him to do it that way.

    Before you waste any more time, find out why. It was probably some requirement by a developer who did not understand DNS and a host file entry is no longer needed. There may be a legitimate reason, and then it will be worth the effort.

    Or you could just start playing with PowerShell - that's never a waste of time. Take a look at this script that will get the entries in the hosts file as objects so you can manipulate their properties:
    SAPIEN Technologies Blog Archive Parse HOSTS file with PowerShell

    An input file full of computer names and a little regular-expression-fu and you can change the hosts entries.
  • DevilsbaneDevilsbane Member Posts: 4,214 ■■■■■■■■□□
    I'm told a DNS entry was created but the fix didn't work.

    There were only 24 pc's that needed to have this fix applied, and 5 of them had already been completed. So I just used the above script to go through and update them accordingly.

    Still curious about a fully automated method (if this had to be done for 500 pc's my method would be garbage).

    Thinking about why the DNS fix failed. Doesn't windows check the hosts file before DNS? So if when they tested the DNS fix they were using a machine that had the old hosts entry in there, wouldn't that make the test fail and lead them to believe that DNS is to blame?

    Thanks for the input.
    Decide what to be and go be it.
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    for 500 PC's you could use a log in script in AD and assign it to an OU or group.

    or use a text file of all the PC names and use a while loop to process the list and run on each PC in turn.

    if you wanted to remove the line you could use the script to read the host file line by line in to a new temp file, skipping any you dont want, and then add the new lines and then over write the old file..

    I am sure you can remover the line from the original but for such a small file the over heads in copy and replace are not going to be that high.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    At least use PowerShell. It is far easier. This assumes you have a CSV file with a column called ComputerName holding the names of all the PCs you are making the changes to. And a CSV with the addresses you wish to change that has two columns, IpAddress and DomainName. I have not tested it...

    $computers = Import-Csv -Path "C:\Path\To\Csv\computers.csv"
    $lines = Import-Csv -Path "C:\Path\To\Csv\addresses.csv"
    
    foreach($computer in $computers)
    {
     $hostsFilePath = "[URL="file://\\"+$computer.ComputerName+"\C$\Windows\system32\drivers\etc\hosts"]\\"+$computer.ComputerName+"\C$\Windows\system32\drivers\etc\hosts[/URL]"
     foreach($line in $lines)
     {
     
      $lineToAdd = $line.IpAddress+" "+$line.DomainName
      $lineToAdd | Out-File -Append -FilePath $hostsFilePath -Force -Encoding "ASCII"
     }
    }
    
Sign In or Register to comment.