Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

return data from function

208 views
Skip to first unread message

Jacob Sampson

unread,
Oct 7, 2008, 4:30:01 PM10/7/08
to
I am trying to return a dataset from a function buy i seem to be having
trouble.

function BkupHist
($computername){
.
.
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
Clear
return $DataSet
}
$test = BkupHist

if i type $dataset.Tables[0] within the function I have data but I get the
following error if I do the same to $test.Tables[0]

Cannot index into a null array.


Kiron

unread,
Oct 7, 2008, 6:44:50 PM10/7/08
to
Functions in PowerShell return all output produced within it, the Return keyword doesn't behave as it does in other languages. Some methods produce unexpected output that must be redirected, captured or cast to [void] in order to suppress it.

Try modifying these statements, an any others that produce output

$SqlAdapter.Fill($DataSet) > $null
[void]$SqlConnection.Close()
#Clear


--
Kiron

Jacob Sampson

unread,
Oct 8, 2008, 9:54:08 AM10/8/08
to
worked great!! The difficulty is how to knwo what statements actually return
data.

Kiron

unread,
Oct 8, 2008, 10:34:07 AM10/8/08
to
> The difficulty is how to know what statements actually return data.

You're right, but there aren't many of these methods, you'll be able to recognize them as you implement them. Easiest way to tell is to test the statement interactively; you can always read the Method's description on MSDN, look for the 'Return value' section:

http://msdn.microsoft.com/en-us/library/zxkb3c3d.aspx

--
Kiron
0 new messages