0

新たな問題に遭遇しましたが、正確にどこから説明を開始すればよいかさえわかりません。最善を尽くしますので、不明な点があればお尋ねください。

DNS レコードに関する情報 (複数の行) を含む Excel ワークブックがあります。これは、powershell DNS 構文によく似ています。例えば:

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 

BUT: さらに詳しく説明すると: $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 サーバーから削除します。

すべてのテキストを読んだ場合は、ありがとうございます。すべての助けに感謝します。前もって感謝します!

4

1 に答える 1

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))
于 2013-11-04T17:21:59.353 に答える