PowerShell Script fo host file modification

While i was implementing SCCM i nedded to add in host file of every computer in our firm records about DP’s and MP (SCCM in untrusted domain then the rest of infrastructure). Doing it manually would take great amount of time to accomplish (over 300 clients). So i have Google’d a bit and combined some codes to get the result i want.


Enabling PowerShell remoting:

The ‘Enable Powershell Remoting’ Policy

All of the settings we are using will be in Computer Configuration so if you want to disable User Configuration as I have go ahead.

  1. Create your GPO, name it what you want, place it where you want, etc.
  2. Edit your policy.

Enabling WinRM

  1. Browse to:
    Policies > Administrative Templates > Windows Components > Windows Remote Management (WinRM) > WinRM Service
  1. Open the “Allow Remote Server management through WinRM” policy setting (Server 2008 R2 and later).
  2. Open the “Allow automatic configuration of listeners” policy setting (Server 2008 and earlier).
  1. Set the Policy to Enabled.
  2. Set the IPv4 and IPv6 filters to * unless you need something specific there (check out the help on the right).

Setting the Firewall Rules

  1. Browse to:
    Policies > Administrative Templates > Network > Network Connections > Windows Firewall > Domain Profile
  2. Open the “Windows Firewall: Define inbound port exceptions” policy setting.
  3. Set it to Enabled if it isn’t already.
  4. Click the “Show…” button and add the port exception. We’re going to be opening TCP port 5985, so the exception string will look something like this:


Host mod script

$cred = Get-Credential domain\admin
#Credentials to use
$session = New-PSSession -cn (get-content “hostlist.txt”) -cred $cred
#Creating remote sessions to computers based on hostlist.txt file
icm -Session $session -ScriptBlock {copy c:\windows\system32\drivers\etc\hosts c:\windows\system32\drivers\etc\hosts.backup}
#Creating backup of host file
icm -Session $session -ScriptBlock {type c:\windows\system32\drivers\etc\hosts > c:\windows\system32\drivers\etc\hostsb}
#changing name of the host file
icm -Session $session -ScriptBlock {echo “XXX.XXX.XXX.XXX      server01.domain.local” >> c:\windows\system32\drivers\etc\hostsb}
icm -Session $session -ScriptBlock {echo “XXX.XXX.XXX.XXX      server02.domain.local” >> c:\windows\system32\drivers\etc\hostsb}
icm -Session $session -ScriptBlock {echo “XXX.XXX.XXX.XXX      server03.domain.local” >> c:\windows\system32\drivers\etc\hostsb}
#Adding lines to the host file
icm -Session $session -ScriptBlock {del c:\windows\system32\drivers\etc\hosts}
#Deleting the old host file
icm -Session $session -ScriptBlock {get-content c:\windows\system32\drivers\etc\hostsb | out-file -encoding utf8 c:\windows\system32\drivers\etc\hosts}
#Adding lines to host file changes the coding and makes the file unreadable for system so we have to change the coding back to UTF. At the same time the name is changed to hosts
icm -Session $session -ScriptBlock {del c:\windows\system32\drivers\etc\hostsb}
#Deleting the working hosts file

I hope it will help somone.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s