Powershell |组合多个数组并将它们与表格进行比较

时间:2013-11-04 17:00:47

标签: arrays excel powershell dns

我遇到了一个新问题,我甚至不知道从哪里开始解释。我会尽我所能,如果有什么不清楚请问我。

我有一个包含有关DNS记录的信息(多行)的Excel工作簿 - 非常类似于powershell DNS语法。 e.g:

HostName    RecordType    TimeStamp    TimeToLive    RecordData
@           A             0            00:05:00      127.0.0.1

我用以下小代码将它们作为数组读取 - 不是很快,但它可以工作!:

#Read Excel
        $row = [int]2
        do {
            if ($Sheet4.Cells.Item($Row,1).Text) {$ZoneName      += $Sheet4.Cells.Item($Row,1).Text}
                                                  $HostName      += $Sheet4.Cells.Item($Row,2).Text
                                                  $RecordType    += $Sheet4.Cells.Item($Row,3).Text
                                                  $TimeStamp     += $Sheet4.Cells.Item($Row,4).Text
                                                  $TimeToLive    += $Sheet4.Cells.Item($Row,5).Text
                                                  $RecordData    += $Sheet4.Cells.Item($Row,6).Text
            $row = $row + [int] 1
           } until (!$Sheet4.Cless.Item($row,2))

现在我有6个阵列都在不同阵列中使用信息,但都有相同数量的线。

现在很棘手(至少对我来说!)部分:

我想将这6个数组填充到一些我不知道的特殊数组中,或者在某种表格中我不知道如何创建。 为什么? 因为我想将这些行与此代码进行比较($ records具体):

        $ZoneNames = (Get-DnsServerZone -ComputerName $DnsServer).zonename
        $ZoneNames | foreach {$Records = (Get-DnsServerResourceRecord -ComputerName $DnsServer -ZoneName $_)}

$ Records [0]会告诉我这个(例如):

HostName                  RecordType Timestamp            TimeToLive      RecordData                                        
--------                  ---------- ---------            ----------      ----------                                        
@                         A          0                    00:05:00        127.0.0.1 

但是:如果我更深入:$ Records [0] .RecordData:

IPv4Address                                                        PSComputerName                                                    
-----------                                                        --------------                                                    
127.0.0.1      

所以我需要重新创建这个(上面)的层次结构来比较它们(如果我是对的?)。

我用这样的表尝试了它(不起作用):

#Create Table object
$table = New-Object system.Data.DataTable “$ExcelRecords”
#Define Columns
$col2 = New-Object system.Data.DataColumn HostName,([string])
$col3 = New-Object system.Data.DataColumn RecordType,([string])
$col4 = New-Object system.Data.DataColumn TimeStamp,([string])
$col5 = New-Object system.Data.DataColumn TimeToLive,([string])
$col6 = New-Object system.Data.DataColumn RecordData,([string])
#Add the Columns
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
#Create a row
$r = $table.NewRow()
#Enter data in the row
$r.HostName = $HostName[$counter]
$r.RecordType = $RecordType[$counter]
$r.TimeStamp = $TimeStamp[$counter]
$r.TimeToLive = $TimeToLive[$counter]
$r.RecordData = $RecordData[$counter]
$RecordData
#Add the row to the table
$table.Rows.Add($r)

尝试比较这样(不起作用):

if ($records[0] -like $table[0]) {write-host "works"}

这确实有效:

if ($records[0].hostname -like $table[0].hostname) {write-host "works"}
works

这没有(我猜这是我问题的根源):

if ($Records[0].RecordData -like $table[0].RecordData) {write-host "works"}

我的主要目标: 检查DNS服务器上是否有记录,这些记录未在Excel表格中说明,并从DNS服务器中删除!

如果您仔细阅读了所有文字,感谢您这样做!感谢每一个帮助。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

我首先从电子表格数据中创建一个PS对象数组。

#Read Excel
        $row = [int]2
        $DNS_Records = 
        do {
            if ($Sheet4.Cells.Item($Row,1).Text) {
              New-Object PSObject -Property @{
                ZoneName      = $Sheet4.Cells.Item($Row,1).Text
                HostName      = $Sheet4.Cells.Item($Row,2).Text
                RecordType    = $Sheet4.Cells.Item($Row,3).Text
                TimeStamp     = $Sheet4.Cells.Item($Row,4).Text
                TimeToLive    = $Sheet4.Cells.Item($Row,5).Text
                RecordData    = $Sheet4.Cells.Item($Row,6).Text
               }
             }
           } until (!$Sheet4.Cless.Item($row,2))