Skip to content

Instantly share code, notes, and snippets.

@Ayrnio
Last active August 1, 2018 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ayrnio/27d4316160befdd98a6716b554f54ace to your computer and use it in GitHub Desktop.
Save Ayrnio/27d4316160befdd98a6716b554f54ace to your computer and use it in GitHub Desktop.
Hosts File/Registry Modification Scripts for AutoDiscover (Add Entry)
###How to Use###
1. Run PowerShell ISE as admin
2. From the PowerShell ISE menu, click on File, Open, locate the file named Add-Hosts-Entry-For-AutoDiscover.ps1 and click on it to open it
3. On line 46 replace the existing IP with the IP of the AutoDiscover service on destination sever (you can get this with CMD by pinging the autodiscover record for the server, for example ping ar-east.exch091.serverdata.net).
4. Click on the play icon in top menu-bar of PowerShell to run the script
5. Create a new mail profile and open it in Outlook to allow it to download at the user end-point
6. Once you are ready to revert back to the DNS/Local based autodiscover, open the second file Remove-Hosts-Entry-For-AutoDiscover.ps1 in PowerShell ISE
7. Click the play icon in the PowerShell ISE menu-bar to run the script
#Script to mod local autodiscover lookup in Outlook via https://ayrne.io
# Give local cache and hosts file DNS entries priority
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\ServiceProvider"
New-ItemProperty -Path $regPath -Name HostsPriority -propertyType DWORD -Value 3 -force | Out-Null
New-ItemProperty -Path $regPath -Name LocalPriority -propertyType DWORD -Value 4 -force | Out-Null
New-ItemProperty -Path $regPath -Name DnsPriority -propertyType DWORD -Value 5 -force | Out-Null
New-ItemProperty -Path $regPath -Name NetbtPriority -propertyType DWORD -Value 6 -force | Out-Null
# Set domain name
$domainName = Read-Host -Prompt "Enter The Domain Name"
# Create Hosts file entry
function setHostEntries([hashtable] $entries) {
$hostsFile = "$env:windir\System32\drivers\etc\hosts"
$newLines = @()
$c = Get-Content -Path $hostsFile
foreach ($line in $c) {
$bits = [regex]::Split($line, "\s+")
if ($bits.count -eq 2) {
$match = $NULL
ForEach($entry in $entries.GetEnumerator()) {
if($bits[1] -eq $entry.Key) {
$newLines += ($entry.Value + ' ' + $entry.Key)
Write-Host Replacing HOSTS entry for $entry.Key
$match = $entry.Key
break
}
}
if($match -eq $NULL) {
$newLines += $line
} else {
$entries.Remove($match)
}
} else {
$newLines += $line
}
}
foreach($entry in $entries.GetEnumerator()) {
Write-Host Adding HOSTS entry for $entry.Key
$newLines += $entry.Value + ' ' + $entry.Key
}
Write-Host Saving $hostsFile
Clear-Content $hostsFile
foreach ($line in $newLines) {
$line | Out-File -encoding ASCII -append $hostsFile
}
}
$entries = @{
"auotodiscover.$domainName" = "70.182.177.89"
};
setHostEntries($entries)
# Set domain name
Write-Host Clearing the DNS cache
Clear-DnsClientCache
# Fetch DNS client cache
Write-Host Fetching the DNS client cache
Get-DnsClientCache
# Lookup AutoDiscover resolution from the local machine
Write-Host Fetching AutoDiscover resolution info
Resolve-DnsName "auotodiscover.$domainName"
#Script to mod local autodiscover lookup in Outlook via https://ayrne.io
# Restore default DNS entries priority in registry
$regPath = "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\ServiceProvider"
New-ItemProperty -Path $regPath -Name HostsPriority -propertyType DWORD -Value 500 -force | Out-Null
New-ItemProperty -Path $regPath -Name LocalPriority -propertyType DWORD -Value 499 -force | Out-Null
New-ItemProperty -Path $regPath -Name DnsPriority -propertyType DWORD -Value 2000 -force | Out-Null
New-ItemProperty -Path $regPath -Name NetbtPriority -propertyType DWORD -Value 2001 -force | Out-Null
# Remove hosts file entries
$i = 0
$length = get-content $env:windir\System32\drivers\etc\hosts | Measure-Object -Line
$op = get-content C:\Windows\system32\drivers\etc\hosts | Select-Object -first ($length.lines-2)
write-output $op | out-file -Encoding UTF8 C:\Windows\system32\drivers\etc\hosts
# Clear the DNS cache
Write-Host Clearing the DNS cache
Clear-DnsClientCache
# Fetch DNS client cache
Write-Host Fetching the DNS client cache
Get-DnsClientCache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment