XXInvidiaXX
XXInvidiaXX

Reputation: 323

Powershell | Error in Do Until Loop with Counter

Why cant I have a counter in a do until loop like this? I don't understand the error. Is there any correlation with the $table ?

I also tried renaming the variable $row, nothing changed.

            $row = [int]1
    do {    #Create a row
            $r = $Table.NewRow()
            #Enter data in the row
            $r.HostName   = $Sheet4.Cells.Item($row,2).Text
            $r.RecordType = $Sheet4.Cells.Item($row,3).Text
            $r.TimeStamp  = $Sheet4.Cells.Item($row,4).Text
            $r.TimeToLive = $Sheet4.Cells.Item($row,5).Text
            $r.RecordData = $Sheet4.Cells.Item($row,6).Text
            $r.Preference = $Sheet4.Cells.Item($row,7).Text  
            #Add the row to the table
            $Table.Rows.Add($r)
            $row++                
       } until (!$Sheet4.Cless.Item($row,2))  

Errorcode:

You cannot call a method on a null-valued expression.
At C:\Script\SCRIPT.ps1:392 char:17
+                 $row++
+                 ~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Table creation data:

    $TabName = "ExcelRecords"
    #Create Table object
    $Table = New-Object system.Data.DataTable “$ExcelRecords”
    #Define Columns
    $Col1 = New-Object system.Data.DataColumn ZoneName,([string])
    $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])
    $Col7 = New-Object system.Data.DataColumn Preference,([string])
    #Add the Columns
    $Table.columns.add($Col1)
    $Table.columns.add($Col2)
    $Table.columns.add($Col3)
    $Table.columns.add($Col4)
    $Table.columns.add($Col5)
    $Table.columns.add($Col6)
    $Table.columns.add($Col7)

Upvotes: 0

Views: 376

Answers (1)

Marco
Marco

Reputation: 23927

I am guessing you have a typo in your script:

change the line

} until (!$Sheet4.Cless.Item($row,2)) 

to:

} until (!$Sheet4.Cells.Item($row,2)) 

This would explain the Null reference exception. Because I can't remember that a worksheet has a property of Cless

Upvotes: 1

Related Questions