Get-member examples -- different results on my PC

Welcome Forums General PowerShell Q&A Get-member examples -- different results on my PC

Viewing 3 reply threads
  • Author
    Posts
    • #127863
      Participant
      Topics: 75
      Replies: 120
      Points: 534
      Rank: Major Contributor

      The following code was copied right out of

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member?view=powershell-6

      
      $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

      
      $A.count
      
      

      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.

    • #127866
      Participant
      Topics: 0
      Replies: 38
      Points: 197
      Helping Hand
      Rank: 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.

    • #127867
      Participant
      Topics: 2
      Replies: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

      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. 🙂

       

    • #127876
      Participant
      Topics: 2
      Replies: 510
      Points: 1,301
      Helping Hand
      Rank: Community Hero

      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!)

       

      • #127884
        Participant
        Topics: 75
        Replies: 120
        Points: 534
        Rank: Major Contributor

        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.

      • #127890
        Participant
        Topics: 2
        Replies: 510
        Points: 1,301
        Helping Hand
        Rank: Community Hero

        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.

        What $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 $A variable. This results in $A containing 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.MemberDefinition objects into the array. So the original array that they then attempt to check the size of (using .Count) now contains all these MemberDefinition objects, instead of containing the original array. If you then call the $A variable, you'll see the Get-Member output that was missing.

      • #127899
        Participant
        Topics: 75
        Replies: 120
        Points: 534
        Rank: Major Contributor

        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!

Viewing 3 reply threads
  • The topic ‘Get-member examples -- different results on my PC’ is closed to new replies.