【问题标题】:PowerShell: Remove duplicated items from arrayPowerShell:从数组中删除重复项
【发布时间】:2017-07-05 18:59:51
【问题描述】:

我正在尝试查找 BIND DNS 记录中的差异。我想输出一个只有这些差异的 CSV 文件。我有一个 CSV 文件,其中包含 BIND 中所有位置的所有记录(ns.prvt、ns.pub、common、includes)。我想弄清楚的是如何输出只显示差异的CSV。要使 2 条记录被视为差异,它们必须满足以下条件:

  1. 两条记录具有相同的 RecordName 和 RecordType。
  2. 两条记录的数据或 TTL 不同。
  3. 两条记录来自不同的位置。

我几乎可以使用以下脚本,但它不断向我显示一些不一定符合上述条件的行。

$Records = Import-Csv C:\Temp\Domain_ALL.csv | Select * | Sort Data,Location
$RecordsRev = @()
$Records | % {
    $Record = $_
    $Records | % {
        $DataFE = $_
        If (
        ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).RecordName -eq $DataFE.RecordName) -and 
        ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).RecordType -eq $DataFE.RecordType) -and 
        ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).Location -ne $DataFE.Location) -and 
        (([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).Data -ne $DataFE.Data) -or 
        ([string]($Record | ? {($_.RecordName -eq $DataFE.RecordName)}).TTL -ne $DataFE.TTL))
        ) {
            $RecordsRev += $_
        }
    }
}
$RecordsRev | Export-Csv C:\Temp\Domain_Discrepancies.csv -NoType

我得到的结果是:

RecordName RecordType Data                           TTL Location
---------- ---------- ----                           --- --------
domain.com TXT        "MS=abc1234566"                600 Includes
domain.com TXT        "MS=abc1234566"                600 Common  
domain.com TXT        "site-verification=abcd1234"   600 Includes
domain.com TXT        "site-verification=abcd1234"   600 Common  
www        CNAME      somedomain.com.test.           600 Includes
www        CNAME      somedomain.com.                600 Common

我期望的结果是:

RecordName RecordType Data                           TTL Location
---------- ---------- ----                           --- -------- 
www        CNAME      somedomain.com.test.           600 Includes
www        CNAME      somedomain.com.                600 Common

如何删除数组中所有重复的行?这与“Select * -unique”不同,因为我不想保留任何包含重复信息的行。

编辑:我认为主要问题是,由于脚本会根据 CSV 中的每条记录检查每条记录,因此在技术上存在差异。例如,在下表中,记录 1 符合差异标准,因为它与记录 4 不同。但是,由于记录 1 与记录 2 相同,因此实际上应该从结果中省略它。

RecordNumber RecordName RecordType Data                           TTL Location
------------ ---------- ---------- ----                           --- --------
1            domain.com TXT        "MS=abc1234566"                600 Includes
2            domain.com TXT        "MS=abc1234566"                600 Common  
3            domain.com TXT        "site-verification=abcd1234"   600 Includes
4            domain.com TXT        "site-verification=abcd1234"   600 Common  
5            www        CNAME      somedomain.com.test.           600 Includes
6            www        CNAME      somedomain.com.                600 Common

任何帮助将不胜感激。

凯尔

【问题讨论】:

  • 记录 1 与记录 3 有何相同之处?
  • 糟糕。我的意思是记录 1 与记录 2 相同。已编辑。谢谢。
  • 我还是有同样的问题。记录 1 和 2 之间的 Location 列不同。因此,根据您的标准,它们不是重复的。
  • 我只希望记录满足以下条件时显示: 1. 两条记录具有相同的 RecordName 和 RecordType。 2. 两条记录的Data或TTL不同。 3. 两条记录来自不同的位置。
  • 对,所以 1 和 2 都会被列出,因为它们符合您的条件(位置不同)。

标签: arrays powershell csv duplicates unique


【解决方案1】:

在删除帖子的人的帮助下,我能够解决这个问题...这是我现在用来查找满足以下所有条件的所有记录的脚本:

  1. 两条记录具有相同的 RecordName 和 RecordType。 -和
  2. 两条记录的数据或 TTL 不同。 -和
  3. 两条记录来自不同的位置。

    $Records = Import-Csv C:\Temp\Domain_ALL.csv | Select * | Sort Data,Location
    $Discrepancies = @()
    $GoodRecords = @()
    $BadRecords = @()
    
    $Records | ForEach-Object { 
    
        # for each record $_, compare it against every other record..
        foreach ($R in $Records) {
    
            # if Both records have the same RecordName and RecordType.. 
            if (($_.RecordName -eq $R.RecordName) -and ($_.RecordType -eq $R.RecordType)) {
    
                # and if Both records come from different locations..
                if ($_.Location -ne $R.Location) {
    
                    # if Both records have the same Data and TTL then they are considered good:
                    if (($_.Data -eq $R.Data) -and ($_.TTL -eq $R.TTL)) {
                        $GoodRecords += $_
                    }
                    Else{
                        # if Both records have different Data or TTL then they are considered bad:
                        $BadRecords += $_
                    }
                }
            }
        }
    
    } 
    
    ForEach ($BadRecord in $BadRecords){
        If (($GoodRecords -notcontains $BadRecord)){
            $Discrepancies += $BadRecord
        }
    }
    $Discrepancies | Select * -Unique | Sort RecordName,Location,Data | ft
    

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 2011-06-29
    • 2011-01-04
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多