The Get-Member Gotcha


Get-Member is by far my most used cmdlet, for good reason, it’s the best one!

I see this come up in the forums once in a while, and I know it got me when I first started to use powershell…

Lets say, I want to take a look at a systems CPU so, lets pull it from WMI

 

 

$proc = get-wmiobject win32_processor

Now, because I’m the curious fella that I am, I want to see what I can do with this new object so lets inspect.

 

$proc | get-member

TypeName: System.Management.ManagementObject#root\cimv2\Win32_Processor

Name                        MemberType   Definition                                                                        
—-                        ———-   ———-                                                                        
Reset                       Method       System.Management.ManagementBaseObject Reset()                                    
SetPowerState               Method       System.Management.ManagementBaseObject SetPowerState(System.UInt16 PowerState, S…
AddressWidth                Property     System.UInt16 AddressWidth {get;set;}                                             
Architecture                Property     System.UInt16 Architecture {get;set;}
                                             
……

well hey now, the architecture of the CPU might be good to know, lets take a look

 

 

$proc.Architecture

… nothing…

Why is it empty?! The processor must have an architecture type set.

 

If you try to run a method in this case, you’ll get something like.

Method invocation failed because [System.Object[]] doesn’t contain a method named XYZ

 

Here is the gotcha, $proc is actually an array, I’ve got multiple CPU’s.

 

get-member –inputobject $proc

 

   TypeName: System.Object[]

Name           MemberType    Definition                                                                                    
—-           ———-    ———-                                                                                    
Count          AliasProperty Count = Length

……

 

ahhh… that makes more sense now… (I hope it does for you too)

 

 

$proc.count

2

$proc[0].Architecture

9

If we take a look at our docs (http://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx) we’ll see that 9 is an x64 architecture.

 

So the thing to keep in mind is that if you pass an object down the pipe to get-member it will show you the members of each unique object in an array. In this case, our array only had one object type in it, but, if you do that with the event log for example, you’ll see there are many different object types in that array…

 

Get-EventLog -Newest 3 -LogName application | gm

 

In my case, out of the 3 events I pulled, there are two different object types…

 

TypeName: System.Diagnostics.EventLogEntry#application/Microsoft-SharePoint Products-SharePoint Foundation/6398

TypeName: System.Diagnostics.EventLogEntry#application/AutoEnrollment/21474837

 

The thing to keep in mind here is be careful with how you use get-member.

If you are unsure if you have an array or not, check

 

get-member –inputobject $obj

#shorthand

gm –I $obj

If it returns a type like System.Object[], then you have an array. The important part in the type name is the [], which is actually what indicates you have an array. If you find that you’ve got an array inspect what’s in that array

 

$obj | gm

 

Hope that clears it up a bit!

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on May 25, 2011, in WMF (Powershell/WinRM) and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: