header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

TypeName Inconsistency?
Last Post 23 Sep 2008 11:46 PM by Shay. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
23 Sep 2008 10:15 PM  

$a = 1,2,3

$a | gm

TypeName: System.Int32

$a.GetType().FullName

FullName: System.Object[]

 $a = [int[]] $a

$a | gm

System.Int32

$a.GetType().FullName

System.Int32[]

As you can see, what are the Types inconsistent?  Am I missing or just not understanding something?

Elan Shudnow
http://www.shudnow.net
smurawskiUser is Offline
New Member
New Member
Posts:46

--
23 Sep 2008 10:27 PM  
There is an explanation for the above behavior.

When you pipe $a to get-member ($a | get-member), get-member is returning the members for each item in the collection (one result for each type). So if my collection was $a = 1, "Hello", 3 , I would get both System.Int32 and System.String as the TypeNames.

If I would then try $a.GetType().FullName, I would get the System.Object[]. By default collections in PowerShell are collections of System.Object (the base object that all .NET objects inherit from). By being that type of collection, it can contain any type of .NET object.

When you cast the collection as [int[]], you are casting it to an array of System.Int32's, and all the members of that array will need to be System.Int32 or could be cast to System.Int32. Piping that same collection to get-member will have the same effect as above, since get-member is evaluating each member of the collection, not the collection as a whole.

Hope this helps... Good luck!
Steven Murawski
Co-Host - Mind of Root (www.mindofroot.com)
Host - PowerShell Basics (powershell-basics.com)
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
23 Sep 2008 10:32 PM  
Definitely makes sense. Thank you smurawski!
Elan Shudnow
http://www.shudnow.net
ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
23 Sep 2008 11:46 PM  

Adding to Steve, to get the collection type itself (instead of its members) use a singelton array notation:

,$a | gm

or -

(,$a).gettype().fullname

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer