Monthly Archives: May 2011

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