Passing DataTable parameter to funcion
function getbuilddata { $dt = new-object system.data.datatable $column1 = new-object system.data.datacolumn 'id' $column2 = new-object system.data.datacolumn 'name' $dt.columns.add($column1) $dt.columns.add($column2) return , $dt } function addrow($dt, [string]$parentid, [int]$level, $item, $incurrentpath) { $row = $dt.newrow() $row.id = [guid]::newguid() $row.name = $item.title $dt.rows.add($row) return $row.id } function adddatarecursive($dt, $items, [string]$parentid, [int]$level) { foreach ($item in $items) { $id = addrow $dt, $parentid, $level, $item, $false if ($item.childnodes.count -gt 0) { $level = $level + 1 adddatarecursive $dt, $item.childnodes, $row.id, $level } } } # generate datatable [system.data.datatable] $builddata = getbuilddata adddatarecursive $builddata $rootnode.childnodes "" 1
then first call adddatarecursive $builddata $rootnode.childnodes "" 1
parameter $dt datatable inside function
adddatarecursive call addrow $dt variable system.array type , have: : method invocation failed because [system.object[]] doesn't contain method named 'newrow'.
then comment line
$id = addrow $dt, $parentid, $level, $item, $false
also recursive call adddatarecursive $dt, $item.childnodes, $row.id, $level gives error. because $dt is system.array type
whats wrong?
try following code below. copy is. should see result guid value. tested on powershell ise 4.
function getbuilddata { $dt = new-object system.data.datatable $column1 = new-object system.data.datacolumn 'id' $column2 = new-object system.data.datacolumn 'name' $dt.columns.add($column1) $dt.columns.add($column2) return ,$dt } function addrow { param( $dt, [string] $parentid, [int] $level, $item, $incurrentpath ) $row = $dt.newrow() $row.id = [guid]::newguid() $row.name = $item.title $dt.rows.add($row) return $row.id } # generate datatable $builddata = [system.data.datatable](getbuilddata) $id = addrow $builddata "" 1 "" "" $id
let me know if you're successful.
Windows Server > Windows PowerShell
Comments
Post a Comment