如何使用 PowerShell 清理過時的 DNS 記錄
當您刪除 AD 計算機對象時,DNS 記錄將保留在 Windows Server DNS 中。最好的方法是設置 DNS 老化和清理。但如果這些記錄是靜態的,它們不會自動刪除。另外,如果您等不及想立即刪除過時的記錄怎麼辦?這時候 PowerShell 就可以派上用場了。在本文中,您將了解如何使用 PowerShell 清理過時的 DNS 記錄。
DNS 資源記錄包含區域維護的有關該區域包含的資源(例如主機)的信息。典型的資源記錄包含以下內容:
- 資源記錄的名稱(主機)。
- 有關資源記錄可以在緩存中保留多長時間的信息。
- 資源記錄類型,例如主機 (A) 資源記錄。
- 特定於記錄類型的數據,例如主機的 IPv4 地址。
您可以直接添加資源記錄,也可以在基於 Windows、啟用動態主機配置協議 (DHCP) 的客戶端使用動態更新加入網絡時自動添加資源記錄。
以下類型的資源記錄類型是眾所周知的:
- 主機(A、AAAA)記錄
- 別名 (CNAME) 記錄
- 郵件交換器 (MX) 記錄
- 指針 (PTR) 記錄
- 服務位置 (SRV) 記錄
- 名稱服務器 (NS) 記錄
- 文本(TXT)記錄
- 委託名稱 (DNAME) 記錄
- 授權開始 (SOA) 記錄
檢查 DNS 管理器中的舊記錄
開始DNS管理器並檢查包含以下內容的舊 DNS 記錄主機名,完全合格域名, 和IP地址。
在我們的示例中,我們希望找到我們降級的域控制器:
- 主機名:DC01-2019
- 完整域名:DC01-2019.exoip.local。
- IP地址:192.168.1.51
右鍵單擊一個區域,然後單擊特性。
點擊名稱服務器。
舊服務器出現在列表中,它是名稱服務器 (NS) 記錄。
存在主機 (A) 記錄。
存在服務位置 (SRV) 記錄。
如果我們遍歷更多區域,我們將找到該特定 AD 計算機對象的所有陳舊 DNS 記錄。
遍歷所有區域並刪除舊的 DNS 記錄需要時間。也可能發生您看不到記錄的情況,並且該記錄將保留在那裡。
我們將使用 PowerShell 自動化該過程並清理所有過時的記錄,而不是手動遍歷所有 DNS 服務器區域並刪除 AD 計算機對象的 DNS 記錄。
筆記:您應該在 Active Directory 中設置 DNS 老化和清理,這樣它將刪除過時的動態 DNS 記錄。雖然這會清理 DNS 中的過時記錄,但不會刪除舊的靜態 DNS 記錄,而這正是下面的 PowerShell 腳本將要做的事情。
使用 PowerShell 腳本刪除過時的 DNS 條目
刪除舊 DNS 記錄的一個極好方法是使用使用以下三個 cmdlet 的 Remove-DNSRecords.ps1 PowerShell 腳本:
- 獲取 DnsServerZone用於檢索主要區域的 cmdlet。
- 獲取 DnsServerResourceRecordcmdlet 查找與 FQDN、主機名和 IP 地址相同的所有資源記錄。
- 刪除-DnsServerResourceRecord刪除檢索到的 DNS 記錄。
步驟1.下載Remove-DNSRecords PowerShell腳本
下載Remove-DNSRecords.ps1 PowerShell腳本並將其放置在域控制器上C:腳本文件夾。如果你沒有腳本文件夾,創建一個。
更多閱讀:使用 PowerShell 將 DNS 記錄導出到 CSV
確保文件未被阻止,以防止運行腳本時出現錯誤。請閱讀文章運行 PowerShell 腳本時出現未數字簽名錯誤來了解更多信息。
另一種選擇是將以下代碼複製並粘貼到記事本中。給它起個名字刪除-DNSRecords.ps1並將其放置在C:腳本文件夾。
<#
.SYNOPSIS
Remove-DNSRecords.ps1
.DESCRIPTION
Clean up stale DNS records with PowerShell.
.LINK
www.alitajran.com/clean-up-dns-records-powershell/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 01/20/2024 - Initial version
#>
$ServerFQDN = "dc01-2019.exoip.local." #Keep the dot (.) at the end
$ServerHostname = "dc01-2019"
$IPAddress = "192.168.1.51"
$Zones = Get-DnsServerZone | Where-Object { $_.ZoneType -eq "Primary" } |
Select-Object -ExpandProperty ZoneName
foreach ($Zone in $Zones) {
Get-DnsServerResourceRecord -ZoneName $Zone | Where-Object {
$_.RecordData.IPv4Address -eq $IPAddress -or
$_.RecordData.NameServer -eq $ServerFQDN -or
$_.RecordData.DomainName -eq $ServerFQDN -or
$_.RecordData.HostnameAlias -eq $ServerFQDN -or
$_.RecordData.MailExchange -eq $ServerFQDN -or
$_.HostName -eq $ServerHostname
} | Remove-DnsServerResourceRecord -ZoneName $Zone -Force -WhatIf
}
- 第 20、21、22 行:將 ServerFQDN、ServerHostname 和 IPAddress 值更改為要從中刪除過時 DNS 記錄的 AD 計算機對象。
步驟 2. 運行Remove-DNSRecords PowerShell 腳本
啟動 Windows PowerShell 並運行刪除-DNSRecords.ps1腳本。
筆記:運行腳本時環境不會發生任何變化,因為腳本中添加了 -WhatIf 參數。識別並確認過時記錄後,刪除 -WhatIf 參數並重新運行腳本。
C:scripts.Remove-DNSRecords.ps1
這就是我們示例中的樣子。
What if: Removing DNS resource record @ of type NS from zone _msdcs.exoip.local on DC01-2022 server.
What if: Removing DNS resource record @ of type NS from zone autodiscover.exoip.com on DC01-2022 server.
What if: Removing DNS resource record dc01-2019 of type A from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record @ of type NS from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record DomainDnsZones of type A from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record _ldap._tcp.Default-First-Site-Name._sites.DomainDnsZones of type SRV from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record _ldap._tcp.Default-First-Site-Name._sites.ForestDnsZones of type SRV from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record _ldap._tcp.DomainDnsZones of type SRV from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record ForestDnsZones of type A from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record _ldap._tcp.ForestDnsZones of type SRV from zone exoip.local on DC01-2022 server.
What if: Removing DNS resource record @ of type NS from zone TrustAnchors on DC01-2022 server.
驗證該特定 AD 計算機對象的舊 DNS 記錄後,刪除-如果什麼參數並重新運行腳本。
C:scripts.Remove-DNSRecords.ps1
已從 Windows Server DNS 中成功刪除該特定 AD 計算機對象的所有 DNS 記錄。
就是這樣!
結論
您學習瞭如何使用 PowerShell 清理過時的 DNS 記錄。下次您想要從 DNS 中刪除 DNS 記錄時,請使用 PowerShell 腳本刪除 DNS 記錄並節省時間。
你喜歡這篇文章嗎?您可能還喜歡如何禁用安裝 Office 加載項的訪問權限。不要忘記關注我們並分享這篇文章。
