2017-07-05 69 views
0

重复项目的我试图找到在BIND DNS记录不符。我想输出一个只有这些差异的CSV文件。我有一个CSV文件,其中包含BIND中所有位置的所有记录(ns.prvt,ns.pub,common,includes)。我试图弄清楚的是如何输出只显示差异的CSV。对于2条记录被视为差异,它们必须符合以下条件:的PowerShell:删除阵列

  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 
---------- ---------- ----       --- -------- 
www  CNAME  somedomain.com.test.   600 Includes 
www  CNAME  somedomain.com.    600 Common 

如何在数组中删除所有重复的行:

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 

,我希望结果如何?这与“Select * -unique”不同,因为我不想保留包含重复信息的任何行。

编辑:我认为主要的问题是,因为脚本会针对在CSV每个记录每条记录,这在技术上是一个矛盾。例如,在下表中,由于记录1与记录4不同,因此记录1符合标准是不一致的。但是,由于记录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

如何为创纪录的1相同,记录3? –

+0

糟糕。我的意思是记录1与记录2相同。编辑。谢谢。 – Kyle

+0

我仍然有同样的问题。记录1和2之间的“位置”列是不同的。因此,根据您的标准,它们不是重复的。 –

回答

1

我能与别人谁删除了自己的岗位的帮助下算出这个......这里是我现在使用的找到满足所有下列条件的所有记录的脚本:

  1. 两个记录具有相同的RecordName和记录类型。 -and
  2. 两个记录有不同的数据或TTL。 -and
  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