4

How come I can successfully pipe result of a cmdlet to Get-Member, but not through a variable?

PM> Get-ProjectFolder "Services" -Project "Foobar" | Get-Member


   TypeName: System.__ComObject#{8e2f1269-185e-43c7-8899-950ad2769ccf}

Name              MemberType Definition                                  
----              ---------- ----------                                  
AddFolder         Method     ProjectItem AddFolder (string, string)      
AddFromDirectory  Method     ProjectItem AddFromDirectory (string)       
AddFromFile       Method     ProjectItem AddFromFile (string)            
AddFromFileCopy   Method     ProjectItem AddFromFileCopy (string)        
AddFromTemplate   Method     ProjectItem AddFromTemplate (string, string)
Item              Method     ProjectItem Item (Variant)                  
ContainingProject Property   Project ContainingProject () {get}          
Count             Property   int Count () {get}                          
DTE               Property   DTE DTE () {get}                            
Kind              Property   string Kind () {get}                        
Parent            Property   IDispatch Parent () {get}                   

.

PM> $f = Get-ProjectFolder "Services" -Project "Foobar"
PM> $f | Get-Member
Get-Member : You must specify an object for the Get-Member cmdlet.
At line:1 char:6
+ $f | Get-Member
+      ~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

Edit: https://i.stack.imgur.com/mEMCu.png

enter image description here

Ilya Kozhevnikov
  • 10,242
  • 4
  • 40
  • 70

1 Answers1

7

How about Get-Member -InputObject $f instead of piping? There is a difference, as the help says, but it should consider only collections:

-InputObject

Specifies the object whose members are retrieved.

Using the InputObject parameter is not the same as piping an object to Get-Member.

vonPryz
  • 22,996
  • 7
  • 54
  • 65
  • Thanks, `InputObject` does work, but what I don't grasp is why the one-line version of pipe works, while saving result to a var and piping that does not. Am I correct in saying that there is logical difference between the two? – Ilya Kozhevnikov May 08 '14 at 08:54
  • @IlyaKozhevnikov There shouldn't be a difference, at least according to the documentation. What happens if you evaluate `$f`, does it provide same output than the `Get-ProjectFolder` command? – vonPryz May 08 '14 at 09:21
  • @IlyaKozhevnikov In addition, [COM objects are somewhat a mess](http://www.sorrell.mcleod.co.uk/Scotty/powershell/COMinterop.htm) with .Net. – vonPryz May 08 '14 at 09:27
  • Typoed, meant it as "there is **no** logical difference". Yes, the outputs are the same, have a look at the screenshot in the edit. – Ilya Kozhevnikov May 08 '14 at 10:49
  • Yeah, I think it's something to do with it being a COM object, some sort of race condition maybe, `$(Get-ProjectFolder Foo) | Get-Member` fails in the same way as the variable variant. – Ilya Kozhevnikov May 08 '14 at 11:21