With the purpose of automation DNS documentation I needed to get information programatically from the DNS servers. The answer to this, and many other similar tasks is spelled WMI. That is short for Windows Management Instrumentation, which is an infrastructure for getting information from Windows using a specific query language.
Here is a method to get the host names and IP addresses for each A-record on the specified domain.
Here is a method to get the host names and IP addresses for each A-record on the specified domain.
public string Server = "192.168.0.1"; public string Domain = "domain.local"; public string Username = "DOMAIN\User"; public string Password = "Pa$$w0rd"; public Dictionary<string, string> GetHosts() { Dictionary<string, string> hosts = new Dictionary<string, string>(); ManagementScope scope = new ManagementScope(String.Format(@"\\{0}\Root\MicrosoftDNS", Server)); scope.Options.Impersonation = ImpersonationLevel.Impersonate; scope.Options.Username = Username; scope.Options.Password = Password; ObjectQuery query = new ObjectQuery(String.Format("SELECT * FROM MicrosoftDNS_AType WHERE DomainName = '{0}'", Domain)); using(ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) { foreach(ManagementObject host in searcher.Get()) { if(!hosts.ContainsKey(host["OwnerName"].ToString())) { hosts.Add(host["OwnerName"].ToString(), host["RecordData"].ToString()); } } } return hosts; }
Comments
Post a Comment