I am trying to understand how to pipe |
an object and call the properties or methods on that.
Ex:
$a = Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\
$a.GetSomething() //calls the method
(Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\).GetSomething() //calls the method
Can I pipe the output of the Get-Item
and invoke properties/methods
on it?
Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\ | call GetSomething()
Object properties To get the properties of an object, use the Get-Member cmdlet. For example, to get the properties of a FileInfo object, use the Get-ChildItem cmdlet to get the FileInfo object that represents a file. Then, use a pipeline operator ( | ) to send the FileInfo object to Get-Member .
To display all the properties and methods available for the get-service cmdlet you need to pipeline Get-Member (alias gm). MemberType 'Property' is to display the specific property like machinename, servicename, etc.
The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array.
The short answer is no. You can't call a method like this using Pipeline. But you can surround your Get-Item
invoke in parentheses and invoke it:
(Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\).GetSomething()
If you don't want that, you could abuse the Select-Object
cmdlet:
Get-Item Registry::HKLM\SOFTWARE\WOW6432Node\Microsoft\Test\abc\ | select { $_.GetSomething() }
It's not possible without writing something to make it so. That something would be pretty confusing.
Like this.
filter Invoke-Method {
param(
[String]$Method,
[Object[]]$ArgumentList
)
$_.GetType().InvokeMember(
$Method.Trim(),
([System.Reflection.BindingFlags]'InvokeMethod'),
$null,
$_,
$ArgumentList
)
}
"qwerty" | Invoke-Method Replace 'q', 'z'
Properties are easier in that there's already a command to do that:
(...).GetSomething() | Select-Object Property1, Property2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With