1

I'm trying to return an array from a function that query a sql table. All ok but only if the elements of array is more than one. If the array is composed by only one element the function doesn't return a type array but a type string. I'm a newbie to powershell and so confused... Here my function :

Function test2{
$query = @"
SELECT DfsTgtPath 
FROM DfsData_v2 WHERE TgtSrvName = 'compname' AND DfsTgtPath LIKE  
'%string%' ORDER BY DfsTgtPath"@
$connection = new-object system.data.sqlclient.sqlconnection("server=SQLSERV;database=DB;trusted_connect>i on=true;" ) 
$adapter = new-object system.data.sqlclient.sqldataadapter ($query,$connection)
$table = new-object system.data.datatable
$recs = $adapter.Fill($table)
$aDfsTgtPath = @($table | select -ExpandProperty DfsTgtPath)
return @($aDfsTgtPath)
}

$result = test2

I would expect the $result as an array containing one single string element but it's seems to be not an array but of type System.String.

3
  • 1
    return @($aDfsTgtPath) -> return ,$aDfsTgtPath Commented Nov 11, 2015 at 9:33
  • @PetSerAl Now it's ok. Thanks a lot !! Now I'll search to better understand why... Commented Nov 11, 2015 at 9:42
  • 2
    Possible duplicate of Pipe complete array-objects instead of array items one at a time? Commented Nov 11, 2015 at 9:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.