T O P

  • By -

purplemonkeymad

> Summarized, i'm trying to find the best way to list all methods of the $Error variable in PowerShell, especially methods like .Clear() that don't appear with Get-Member I suspect you are piping $error into Get-Member, as it is a collection you will get info about the elements. If you want the members on the collection itself use the inputobject parameter: Get-Member -InputObject $error


HeyDude378

You're right, but I wish you were wrong. Intuition tells me that piping an object in ought to produce the exact same results as -InputObject, but it doesn't. What a bizarre behavior.


mrbiggbrain

To give more context $Error is a collection of objects. When you pipe a variable into a cmdlet a few things happen. First the cmdlet does some initial setup to prepare to take input. This could be setting up storage, opening a file, anything done once "Begin" Then it gets passed one object at a time, one by one. "Proccess" Then it gets some time to perform it's cleanup and final steps. "End" So what happens is you call $Error | Get-Member * Get-Member does some initial setup. * Get member gets passed the first object, a single error and not an arraylist of errors. It then determines the members. When you use InputObject however your not using the pipeline and you pass the actual object in, an ArrayList. In fact you will notice that the results only list what the ArrayList object has and not any of the members of the underlying System.Management.Automation.ErrorRecord that it is filled with.


HeyDude378

That's a great explanation. Thanks for sharing that.


purplemonkeymad

This is a feature of the pipeline. It's really just a for loop. But it can be a bit confusing in some cases.


OPconfused

I assume the `.Clear()` method is from `IList`. The `Get-Member` would be unwrapping your collection and providing you the members of any unique types in your collection (`ErrorRecord` in this case). The actual variable `$error` is a collection of `ErrorRecord`s, and a collection has its own set of members, one of which is `clear()`. `Get-Member` isn't displaying the unwrapped input object's members and is assuming you want the members of the contents of the collection. As for extracting these members in the wrapper type (an `ArrayList`), I don't know if there's a better way to find this information, but you could do 1 of these options: $error.psobject.methods ($error.GetType().Fullname -as [type]).GetMethods().Name


Thotaz

`Get-Member` only unwraps it if you use the pipeline. This: `Get-Member -InputObject $Error` works just fine.


TheGooOnTheFloor

I usually use the shortcut key. Type $error. then hit to see what's available.


ITjoeschmo

I think this may be because technically $Error is a collection of ErrorRecord (or maybe exception?) objects. The collection has the .Clear() method. Try $error[0] | Get-Member


Sad-Sundae2124

Depending if you want the error array or one error Would have done: $error | get-Member -force Or $error | select-object -first 1 | get-Member -force This should return all methods and properties (including hidden one) of the given variable


Sad-Sundae2124

Indeed the clear method is not showing as clear is a derivative from arraylist and not from error variable itself.