Powershell |使用计数器执行Do Until循环时出错

时间:2013-11-18 11:30:06

标签: loops powershell

为什么我不能在这样的循环中有一个计数器? 我不明白这个错误。 与$ table有任何关联吗?

我也尝试重命名变量$ row,没有改变。

            $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))  

错误码:

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

表格创建数据:

    $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)

1 个答案:

答案 0 :(得分:1)

我猜你的剧本中有拼写错误:

更改行

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

为:

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

这将解释Null引用异常。因为我不记得工作表的属性为Cless