The following shows that the returned type are different depends on how many rows returned. Why it's designed this way? It's very easy to make assumption it always returns an array and write the code $a.Length
which will raise error when it returns only one row, unless the it's written as $a | % { ....}
$a = @(invoke-....)
, which is easy to forget.
$a=Invoke-Sqlcmd -ServerInstance server "select 1 a"
$b=Invoke-Sqlcmd -ServerInstance server "select 1 a union all select 2"
$a.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False DataRow System.Object
And the following statement returns an array of object (BTW, why not an array of DataRow?)
$b.GetType()
IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Object[] System.Array
However, gm
returns the same type for both variables. Why it's designed this way which can be very confused.
Question:
- What's the point that the array is removed when only one item is returned?
- Why
gm
get item type of an array? How togm
of an array? - Why
getType()
cannot return the data type of the item when it returns an array type?
?
PS SQLSERVER:\> $a|gm
TypeName: System.Data.DataRow
Name MemberType Definition
---- ---------- ----------
AcceptChanges Method void AcceptChanges()
......
ToString Method string ToString()
Item ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string co...
a Property int a {get;set;}
PS SQLSERVER:\> $b|gm
TypeName: System.Data.DataRow
Name MemberType Definition
---- ---------- ----------
AcceptChanges Method void AcceptChanges()
......
ToString Method string ToString()
Item ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string co...
a Property int a {get;set;}
Invoke-Sqlcmd
and also depends on your PowerShell version. 2. Get-Member is acting on elements passed in the pipe not the entire object which is why 3. returns what it does. – Matt