Link to home
Start Free TrialLog in
Avatar of JB Blanco
JB BlancoFlag for United States of America

asked on

Need a good solution to deploying hosts files to several servers that does not involve GPO

Need a good solution to deploying hosts files to several servers that does not involve GPO

Hi,

I need a good and fast way to deploy a hosts file or edit the hosts file of many Application servers at once and then be able to roll back if necessary.

Also, these servers are not all on the same domain.  And some are not even part of a domain but a workgroup.   So this makes things complicated.  

We need to make this happen in preparation for a SQL DB migration that is occurring next week.

I was looking online for different ways to do this.

I found some powershell script solutions but there may be a great tool for this instead.  I feel this is a very common situation for Application servers and making sure there are no issues incase DNS goes broke for whatever reason.  I feel it is also great for Failover situations.  

Your thoughts please.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB Blanco

ASKER

List of servers on Servers.txt on desktop of the current user:
========example of server.txt =========
server1
server2
server3
========end of example of server.txt ====

Powershell Archive.
$Shared="\\dc\c$\HostFiles" # Shared Folder where you want to save all the host files in case of rollback
$Servertxt=  "$env:userprofile\desktop\server.txt"  #location of server.txt

#powershell Script:

[Cmdletbinding()]
param(
    [Parameter(Position=0,Mandatory=$false)][Switch]$rollback=$false
)

$Shared="\\dc\c$\HostFiles" # Shared Folder where you want to save all the host files in case of rollback
$Servertxt=  "$env:userprofile\desktop\server.txt"  #location of server.txt
$hostfile = "D:\hosts" #host file to be deployed

if(!$rollback){
    Get-Content $servertxt | % {
        #make a folder with the name of the current server on the $shared folder to save the host file.
        if(!(Test-Path $Shared\$_)){
            New-object  New-Item -ItemType directory -Path $Shared\$_ | out-null
        }
        #copy backup
        copy-item -Path "\\$_\C$\Windows\System32\drivers\etc" -Destination "$shared\$_\hosts" -WhatIf
        #remove current
        Remove-Item "\\$_\C$\Windows\System32\drivers\etc\hosts" -Confirm:$false -WhatIf
        #copy new
        copy-item -Path $hostfile -Destination "\\$_\C$\Windows\System32\drivers\etc" -WhatIf
    }
}
else{
    #get the host file from every folder
    Get-content $servertxt | %{
        #remove Whatif to test it
        Remove-Item "\\$_\C$\Windows\System32\drivers\etc\hosts" -Confirm:$false -WhatIf
        copy-item -Path "$shared\$_\hosts" -Destination "\\$_\C$\Windows\System32\drivers\etc" -WhatIf
    }

}

Open in new window

check them with whatif
remove all whatif for production

Wow, nice!  First of all thank you for your response!  

Can you elaborate more on your last few lines "Check them with whatif remove all whatif for production"

What does it do exactly?
If you check the WhatIF is basically a switch where you want to "Simulate" what the script does.
To actually do it you must remove the "-WhatIf" all over the place to test it in a secure environment :) the whole idea is that you could safely run the script and check the steps it does before actually doing it.
I will test this out this week and get back to you.  

Thanks!
Looks like this may be exactly what we need.  Thanks again.  Still in the testing phase and tweaking it to our needs.

Thanks for your patience
Amazing to know that and I'm glad to help