Extract remote DNS to local hosts file
- Get link
- X
- Other Apps
So with the whole work from home process, my company recently made changes to its VPN policy to block local DNS queries. This has completely screwed up my work flow for testing.
So with a bit of google-ing and duct taping some code I have come up with a simple powerslell script to extract the DNS zone from my lab to my local host file.
Enjoy!
#VARIABLES
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
$CSV = "$dir\DNS_A_records.csv"
#parameters
$DNSServer = "dc1.home.lab"
$Zone1 = "home.lab"
#$Zone2 = "me.myzone.local"
#SCRIPT MAIN
clear
$DNS_Zones = @()
$DNS_Zones += $Zone1
#$DNS_Zones += $Zone2
$hosts = @()
$DNS_Zones | % {
$zone = $_
Write-Host "Getting DNS A records from $zone"
$DNS_A_records = @(Get-WmiObject -Class MicrosoftDNS_AType -NameSpace Root\MicrosoftDNS -ComputerName $DNSServer -Impersonation 3 -Credential HOME\administrator -Filter "ContainerName = `'$zone`'")
$DNS_A_records | % {
$hostA = "" | select "IPAddress" , "hostname"
$hostA.hostname = $_.OwnerName
$hostA.IPAddress = $_.IPAddress
$hosts += $hostA
}
}
$hosts = $hosts | Sort-Object @{Expression={[Version]$_.IPAddress}}
$hosts | Export-Csv $CSV -NoTypeInformation -Force
$conv = Get-Content $CSV
$conv.Replace('","',",").TrimStart('"').TrimEnd('"') | % {$_ -replace ',',' '} | Out-File .\hosts.txt -Force -Confirm:$false
Get-Content .\hosts.txt | select-string -pattern "IPAddress hostname" -notmatch | Out-File C:\Windows\System32\drivers\etc\hosts -Force -Confirm:$false
- Get link
- X
- Other Apps
Comments
Post a Comment