Operation is not valid due to the current state of the obje

by scottbass at 2012-09-07 22:48:20

# create table structure
$dt = New-Object System.Data.DataTable
[Void]$dt.Columns.Add(“Program Name”, [string])
[Void]$dt.Columns.Add(“Return Code”, [int])

# add data to the table
[Void]$dt.Rows.Add(“Extract”,0)
[Void]$dt.Rows.Add(“Transform”,4)
[Void]$dt.Rows.Add(“Load”,8)

# print table
$dt
$dt | Format-Table “Program Name” -AutoSize


Gives the error:

Operation is not valid due to the current state of the object.
At :line:0 char]

Any ideas?

Thanks,
Scott
by mjolinor at 2012-09-08 06:47:22
Running V3 on W7, I cannot reproduce that error.
by DonJ at 2012-09-08 07:42:39
I’m going to move this to Kirk’s forum… more of an advanced topic, and he’s more likely to have worked with this stuff.
by poshoholic at 2012-09-08 09:38:58
I get an issue with PowerShell 2 for this script, but not for PowerShell 3. With PowerShell 2, it seems to be tripping up because you are outputting two tables from one script (a bug that is run into quite regularly in PowerShell 2). Do you need to output your table twice? If not, remove the one you don’t want and then output one set of results and see if that fixes the issue.