Author Posts

October 28, 2013 at 2:36 pm

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

October 28, 2013 at 3:50 pm

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++

October 29, 2013 at 8:48 am

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