Hast table trouble

This topic contains 2 replies, has 2 voices, and was last updated by  Bard 3 years, 9 months ago.

  • Author
    Posts
  • #11078

    Bard
    Participant

    I can't figure out why I'm getting this error after I iterate through my For Each loop after a period of time.

    ERROR:
    Cannot convert value "2147483648" to type "System.Int32". Error: "Value was either too large or too small for an Int32."
    At line:120 char:9
    + $n = $n + $n
    + ~~~~~~~~~~~~
    + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException
    ...
    [int]$n = 1
    $rowObjects = @()
    ForEach ($row in $rowData) {
    If ($($row.GetType().Name.ToString()) -eq "DataRow") {
    $rd = @{RowCount=$n;
    Column1=$row.Column1;}
    $rowObject = New-Object -TypeName PSObject -Property $rd
    $rowObjects += $rowObject
    $n = $n + $n
    }
    }

    $rowObjects | ConvertTo-HTML -head $a -Body $b | Out-File d:\Test.htm

  • #11081

    Dave Wyatt
    Moderator

    You're doubling $n each time through the loop, instead of just incrementing it by 1 (and you also declared it as an [int], preventing PowerShell from automatically changing its type to something that can hold a larger number.)

    Try changing $n = $n + $n to either $n = $n + 1 , or simply $n++

  • #11102

    Bard
    Participant

    Gee... don't know how I missed that. I guess I was in too much of a hast. Thanks for the help!

You must be logged in to reply to this topic.