Created
June 13, 2019 01:09
-
-
Save IMJLA/f8537a7876748630cc5c7a22c44915fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On Error Resume Next | |
strOldHostname = InputBox ("Enter the hostname to be removed from DNS. Just the hostname, please; not the FQDN.","Enter the host name to be removed","") | |
'Track when the script began | |
StartTime = Now | |
'Begin the log file | |
Set objShell = CreateObject("WScript.Shell") | |
strLocalAppDataPath = objShell.ExpandEnvironmentStrings("%LocalAppData%") | |
Set objFSO = CreateObject("Scripting.FileSystemObject") | |
strLogFolder = strLocalAppDataPath + "\IMJLA" | |
objFSO.CreateFolder(strLogFolder) | |
strLogFolder = strLogFolder + "\DNSCleanup" | |
objFSO.CreateFolder(strLogFolder) | |
logPath = strLogFolder + "\DNSCleanup " + strOldHostname + ".log" | |
Set objLogFile = objFSO.OpenTextFile(logPath,2,True) | |
objLogFile.WriteLine(FormatDateTime(StartTime) + vbTab + "Starting script. Log File: " + logPath) | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "Hostname Input by User: " + strOldHostname) | |
'Determine the FQDN of the specified hostname | |
Set objAdInfo = CreateObject("AdSystemInfo") | |
strDomainFQDN = objAdInfo.DomainDNSName | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "Current Domain: " + strDomainFQDN) | |
strOldFQDN = strOldHostname + "." + strDomainFQDN | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "FQDN to Cleanup: " + strOldFQDN) | |
'Retrieve the static A record(s) from the 'strDomainFQDN' Forward Lookup Zone; we need the IP address(es). (OwnerName= "strOldFQDN") | |
Set objDNS = GetObject("winMgmts:root\MicrosoftDNS") | |
strWmiQuery = "SELECT * FROM MicrosoftDNS_ResourceRecord WHERE OwnerName = " & Chr(34) & strOldFQDN & Chr(34) | |
strWmiQueryLang = "WQL" | |
WmiQueryFlags = wbemFlagForwardOnly | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "Running WMI Query: " + strWmiQuery) | |
Set colPrimaryARecords = objDNS.ExecQuery(strWmiQuery,strWmiQueryLang,WmiQueryFlags) | |
objLogFile.WriteLine(FormatDateTime(Now) & vbTab & colPrimaryARecords.Count & " IP address found.") | |
'Delete the static A record(s) from the 'strDomainFQDN' Forward Lookup Zone: (RecordData -eq "OldIPAddress") | |
'Delete the (same as parent folder) A records from these zones: (RecordData -eq "OldIPAddress") | |
'strDomainFQDN' Forward Lookup Zone | |
'DomainDnsZones.strDomainFQDN' Forward Lookup Zone | |
'ForestDnsZones.strDomainFQDN' Forward Lookup Zone | |
'gc._msdcs.strDomainFQDN' Forward Lookup Zone | |
If colPrimaryARecords.Count = 0 Then | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "No IP address found for " + strOldFQDN + ". Prompting user.") | |
OldIPAddress = InputBox ("No IP address found for " + strOldFQDN + ". Please enter an IP address to cleanup (leave blank to cleanup based on hostname only):","Enter the IP address to be removed","") | |
If OldIPAddress = "" Then | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "User did not enter an IP address.") | |
Else | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "IP to Cleanup (user-specified): " + OldIPAddress) | |
strWmiQuery = "SELECT * FROM MicrosoftDNS_ResourceRecord WHERE RecordData = " & Chr(34) & OldIPAddress & Chr(34) | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "Running WMI Query: " + strWmiQuery) | |
Set ARecords = objDNS.ExecQuery(strWmiQuery,strWmiQueryLang,WmiQueryFlags) | |
objLogFile.WriteLine(FormatDateTime(Now) & vbTab & ARecords.Count & " matching DNS records found.") | |
For Each ARecord in ARecords | |
ARecord.Delete_ | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "DNS Record Deleted: " + ARecord.TextRepresentation) | |
Next | |
End If | |
End If | |
For Each PrimaryARecord in colPrimaryARecords | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "IP to Cleanup (found in DNS): " + PrimaryARecord.RecordData) | |
strWmiQuery = "SELECT * FROM MicrosoftDNS_ResourceRecord WHERE RecordData = " & Chr(34) & PrimaryARecord.RecordData & Chr(34) | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "Running WMI Query: " + strWmiQuery) | |
Set ARecords = objDNS.ExecQuery(strWmiQuery,strWmiQueryLang,WmiQueryFlags) | |
objLogFile.WriteLine(FormatDateTime(Now) & vbTab & ARecords.Count & " matching DNS records found.") | |
For Each ARecord in ARecords | |
ARecord.Delete_ | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "DNS Record Deleted: " + ARecord.TextRepresentation) | |
Next | |
Next | |
'Delete the CNAME record from the '_msdcs.strDomainFQDN' Forward Lookup Zone (RecordData -eq "ServerFQDN.") | |
'Delete the NS record from every zone (RecordData -eq "ServerFQDN.") | |
'Delete the PTR record(s) from the Reverse Lookup Zone(s) (RecordData -eq "ServerFQDN.") | |
strWmiQuery = "SELECT * FROM MicrosoftDNS_ResourceRecord WHERE RecordData = " & Chr(34) & strOldFQDN & Chr(46) & Chr(34) | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "Running WMI Query: " + strWmiQuery) | |
Set CnameNsPtrRecords = objDNS.ExecQuery(strWmiQuery,strWmiQueryLang,WmiQueryFlags) | |
objLogFile.WriteLine(FormatDateTime(Now) & vbTab & CnameNsPtrRecords.Count & " matching DNS records found.") | |
For Each CnameNsPtrRecord in CnameNsPtrRecords | |
CnameNsPtrRecord.Delete_ | |
objLogFile.WriteLine(FormatDateTime(Now) + vbTab + "DNS Record Deleted: " + CnameNsPtrRecord.TextRepresentation) | |
Next | |
'Set objZones = objDNS.ExecQuery("Select * from MicrosoftDNS_Zone ") | |
'For Each objZone In objZones | |
' WScript.Echo objZone.ContainerName | |
'Next | |
'End the log file | |
EndTime = Now | |
TimeElapsed = DateDiff("s",StartTime,EndTime) | |
objLogFile.WriteLine(FormatDateTime(EndTime) & vbTab & "Ending script. Time Elapsed: " & CStr(TimeElapsed) & " seconds") | |
'Cleanup | |
objLogFile.Close | |
Set objLogFile = Nothing | |
WScript.Echo "DNS Cleanup of hostname " & strOldHostname & " has completed successfully. Please verify using NSLOOKUP because sometimes the WMI Delete_ command does not succeed (in this case try running the script on a different DNS server)." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment