December 3, 2018 at 8:24 pm #127863
The following code was copied right out of
$A = Get-Member - InputObject @(1) $A.Count 1 $A = Get-Member -InputObject 1,2,3 TypeName: System.Object Name MemberType Definition ---- ---------- ---------- Count AliasProperty Count = Length Address Method System.Object& Address(Int32 ) Clone Method System.Object Clone() ... $A.Count 1
When I run these same 4 commands in succession on my PC using either PS ISE (PS 5.1) or VS Code (also PS 5.1) I get the answer '34' in all cases of
I am totally lost at this weird result. I also do not get the output of the 3rd command where the TypeName, etc. object info are displayed.
Would be grateful for any advice, tips or suggestions.
December 3, 2018 at 8:38 pm #127866ParticipantTopics: 0Replies: 38Points: 197Rank: Participant
It must be a bug in the example. You get an array with the members of an array – which is 34. They also have a space between the dash and InputObject.
December 3, 2018 at 8:41 pm #127867
Hmm, I can see that needs to be fixed. Not sure what they were going for there, none of that is working at all. Maybe something more like the following,:
PS> $A = @(1) PS> $A.Count 1 PS> Get-Member -InputObject $A TypeName: System.Object Name MemberType Definition ---- ---------- ---------- Count AliasProperty Count = Length Address Method System.Object& Address(Int32 ) Clone Method System.Object Clone() ... PS> $A = @(1,2,3) PS> $A.Count 3 PS> Get-Member -InputObject $A TypeName: System.Object Name MemberType Definition ---- ---------- ---------- Count AliasProperty Count = Length Address Method System.Object& Address(Int32 ) Clone Method System.Object Clone() ...
I'll have to go figure out where that is in the Docs repo and get it fixed. 🙂
December 3, 2018 at 8:46 pm #127870
Thanks Joel. If you click that link (which I reproduce here) and scroll down to Example 6, you will get to the actual code. Yes, there is a space between the '-' and 'Inputobject' in the first example, but I corrected that for my own test and still got '34'.
Many thanks for your attention and kind consideration.
December 3, 2018 at 9:00 pm #127876
Thanks! Yeah, I went ahead and dug up the Github page for this document and submitted a change request. (And I note that that example has been basically in that form since v3.0's help!)
December 3, 2018 at 9:18 pm #127884
My sincerest thanks Joel, this has been very useful and educational for me.
Perhaps that syntax was valid in v3.0, but before I posted my dilemma on this example, I tried to "make it right" — more or less along the lines of what you did to correct it: put into variable first, then Get-Member it, etc.. but I was also trying to understand the logic of the examples in that document, and why, if they are "wrong", there were no error messages from Powershell. I take it that code such as
$A = Get-member -InputObject 1,2,3
is considered "not good practice"?
Thanks again for your attention and assistance.
December 3, 2018 at 9:48 pm #127890
Well, apart from the fact that the example had the misplaced hyphen, there was nothing syntactically wrong with it. That's valid syntax.
The difficulty is that they're trying to do two or three things in one line, and confusing the process. I guarantee that example was never actually tested in a console, or else the oddities would have been noticed.
$A = Get-Member -InputObject @(1, 2, 3)actually does is it stores the output of the Get-Member command (which you'd normally want to see in the console) into the
$Avariable. This results in
$Acontaining an array of
Get-Member's metadata objects. So it ends up with an array the same size as the number of properties, methods, and other members the original array had to start with.
You can illustrate this if you run that line and then do
$A | Get-Member, which will tell you that it's stored a bunch of
Microsoft.PowerShell.Commands.MemberDefinitionobjects into the array. So the original array that they then attempt to check the size of (using
.Count) now contains all these
MemberDefinitionobjects, instead of containing the original array. If you then call the
$Avariable, you'll see the
Get-Memberoutput that was missing.
December 3, 2018 at 10:35 pm #127899
Thank you very much Joel!
Now I think I understand why the number 34 came out. When I did this:
Get-member -InputObject 1,2,3
I created an object of TypeName: System.Object, which happens to have 34 members, beginning with: Count, Add, Address ...and ending with: Rank, SyncRoot.
My sincerest gratitude for your taking the time to explain in detail this finer point: the difference between two distinct and separate entities — the array object itself, and its class members. (I hope I have explained this correctly to myself)
Thanks again. Much appreciated!
- The topic ‘Get-member examples -- different results on my PC’ is closed to new replies.