arrays - Powershell - DNS 记录删除数组问题
问题描述
我试图用一些 Powershell 代码清理我们的 DNS 服务器。有多个具有相同 IP 地址的 DNS 记录,我希望删除除该 IP 地址的最新记录之外的所有记录。
我已经设法将重复出现的 IP 地址、名称、时间戳记到一个数组中。但我肯定不能删除除最新记录之外的所有记录,因为数组包含多个 IP 地址,我只需要保留每个 ip 的最新记录。
这是我到目前为止所拥有的,任何帮助将不胜感激:
$CollectionDC = @()
$TotalDuplicateIP = @()
$RecordsDC = Get-DnsServerResourceRecord -ComputerName blaa -ZoneName "blaaaa.com" -RRType A
Foreach ($RecordDC in $RecordsDC)
{
$NewObjectdc = New-Object PsObject -Property @{
RecordName = $RecordDC.HostName
IP = $RecordDC.RecordData.IPv4Address
Timestamp = $RecordDC.TimeStamp
}
$CollectionDC += $NewObjectdc
}
$DuplicateIPDNS = ($CollectionDC.IP | group | ?{$_.Count -gt 1}).Values
foreach($item in $DuplicateIPDNS)
{
if($item.IPAddressToString -like "172.31.*")
{
$TotalDuplicateIP += $CollectionDC | Where-Object {$_.IP -eq $item.IPAddressToString}
}
}
$TotalDuplicateIP = $TotalDuplicateIP | sort IP,RecordName | Where-Object {$_.RecordName -notmatch "@|DomainDnsZones|ForestDnsZones|gc._msdcs"}
foreach($Record in $TotalDuplicateIP)
{
$Record
#Keep only the latest record (timestamp) for each IP
#Remove-DnsServerResourceRecord -WhatIf
}
上面的输出命令:
Timestamp IP RecordName
--------- -- ----------
12/11/2019 3:00:00 AM 172.31.0.107 blaa-217 #Remove
12/11/2019 11:00:00 AM 172.31.0.107 blaa-247 #Keep
12/10/2019 9:00:00 AM 172.31.0.107 blaa-301 #Remove
12/11/2019 2:00:00 AM 172.31.0.107 blaa-306 #Remove
12/11/2019 7:00:00 AM 172.31.0.107 blaa-320 #Remove
12/11/2019 8:00:00 AM 172.31.0.110 blaa-175 #Remove
12/11/2019 9:00:00 AM 172.31.0.110 blaa-236 #Keep
12/11/2019 8:00:00 AM 172.31.0.110 blaa-318 #Remove
解决方案
我会稍微改变一下。如果您有很多 DNS 记录Group-Object
,则需要很长时间来处理。
$IPRange = '192.168.60.*'
$ZoneName = 'test.local.uk'
$DNSQueryDC = 'mydc.test.local.uk'
# Get DNS records - exclude what you can here as "Group-Object" is slow - it will make subsequent processing faster
$RecordsDC = Get-DnsServerResourceRecord -ComputerName $DNSQueryDC -ZoneName $ZoneName -RRType A | Where-Object {
($_.Timestamp)`
-and ($_.HostName -notlike "*$ZoneName*")`
-and ($_.HostName -ne '@')
}
# Get all records matching the IP range
$CollectionDC = $RecordsDC | Where-Object { $_.RecordData.IPv4Address -like $IPRange } | ForEach-Object {
[pscustomobject] @{RecordName = $_.HostName;IP = $_.RecordData.IPv4Address;Timestamp = $_.TimeStamp}
}
# Group by IP to retrieve duplicates
$CollectionDC | Group-Object -Property IP | Where-Object { $_.Count -gt 1} | ForEach-Object {
# Sort by timestamp, then select all except the most recent one
Write-Host "Found duplicate IPs for: " $_.Name -ForegroundColor Yellow
$DuplicateIPs = $_.Group | Sort-Object Timestamp -Descending
Write-Host "`nMost recent record:"
$DuplicateIPs | Select-Object -First 1 | Out-Host
$RecordsToDelete = $DuplicateIPs | Select-Object -Skip 1
Write-Host "Deleting older records:" -ForegroundColor Cyan
$RecordsToDelete | Out-Host
# Now remove them
#
}
推荐阅读
- python - 我怎样才能在我的views.py中写这个过滤器
- node.js - 如何更新 Mogodb 文档的架构
- flutter - RangeError(索引):无效值:不在包含范围内 0..1273:1274
- html - 表格行悬停以显示工具栏
- flutter - 容器设计结构——Flutter
- c - 如何获取命令行参数以从同一 c 程序文件中的另一个方法读取输出?
- unity3d - 为什么 Unity 在构建中包含编辑器目录
- html - 如何使用纯 CSS 在一个 div 中订购(两列)长文本?
- javascript - 无法使用 MySQL 在 node.js 中执行 Sql 查询
- powerbi - 日期PowerBI的累计发生次数?