How to have a Powershell Function return a table? -


i love using tables in powershell scripts. since lot of repeating code create them if need different tables, decided create function returns functional table me.

my try far looks this:

function maketable ($btab, $tablename, $columnarray) { $btab = new-object system.data.datatable("$tablename") foreach($col in $columnarray)   {     $mcol = new-object system.data.datacolumn $col;     $btab.columns.add($mcol)   } } $acol = @("bob","wob","trop") $atab = $null maketable $atab "test" $acol 

alternatively tried:

function maketable ($tablename, $columnarray) { $btab = new-object system.data.datatable("$tablename") foreach($col in $columnarray)   {     $mcol = new-object system.data.datacolumn $col;     $btab.columns.add($mcol)    } return $btab } $acol = @("bob","wob","trop") $atab = maketable "test" $acol 

i tested both versions same code:

$arow = $atab.newrow() $arow["bob"] = "t1" $arow["wob"] = "t2" $arow["trop"] = "t3" $atab.rows.add($arow) $atab  

both sadly didn't expected.

you cannot call method on null-valued expression. @ line:13 char:1 + $arow = $atab.newrow() + ~~~~~~~~~~~~~~~~~~~~~~ + categoryinfo          : invalidoperation: (:) [], runtimeexception + fullyqualifiederrorid : invokemethodonnull 

can me?

edit:

$global:atab = new-object system.data.datatable("") $global:btab = new-object system.data.datatable("") function maketable ($x, $tablename, $columnarray) {     if($x -eq 1)     {         $xtab = $global:atab     }     elseif($x -eq 2)     {         $xtab = $global:btab     }     $xtab.tablename = $tablename     foreach($col in $columnarray)     {         $mcol = new-object system.data.datacolumn $col;         $xtab.columns.add($mcol)     }    } $acol = @("bob","wob","trop") maketable 1 "test" $acol $arow = $global:atab.newrow() $arow["bob"] = "t1" $arow["wob"] = "t1" $arow["trop"] = "t1" $global:atab.rows.add($arow) $global:atab 

this doing want, not really. think there better way.

to make code work add comma after return according this solution.

"return , $btab" instead of "return $btab"

by default returning enumerable contents of datatable, datarows, null in case no datarows have been created yet.

the comma before datatable object ($btab) implies array $btab element. nothing supplied first element (to left of comma) out of pipe comes other element: datatable itself.

    function maketable ($tablename, $columnarray) { $btab = new-object system.data.datatable("$tablename") foreach($col in $columnarray)   {     $mcol = new-object system.data.datacolumn $col;     $btab.columns.add($mcol)    } return , $btab }  $acol = @("bob","wob","trop") $atab = maketable "test" $acol  $arow = $atab.newrow() $arow["bob"] = "t1" $arow["wob"] = "t2" $arow["trop"] = "t3" $atab.rows.add($arow) $atab  

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -