开发者

Piping into objects/properties/methods in PowerShell

开发者 https://www.devze.com 2023-04-10 12:31 出处:网络
In PowerShell, y开发者_如何学运维ou can pipe into Cmdlets and into scripted functions. But is it possible to pipe into objects, properties, or member functions?

In PowerShell, y开发者_如何学运维ou can pipe into Cmdlets and into scripted functions. But is it possible to pipe into objects, properties, or member functions?

For example, if I have a database connection object $dbCon, I want to be able to so something like this:

$dbCon.GetSomeRecords() | <code-to-manipulate-those-records | $dbCon.WriteBackRecords()

I know the same functionality can be achieved with Foreach-Object or with a Cmdlet that gets the object as an argument - the reason I want to pipe directly to the object or to it's members is to achieve elegance and to keep the OOP style(using an object's method instead of sending the object as an argument)

Is it possible?


EDIT:

It looks like people don't understand my question, so I need to clarify it:

PowerShell can pipe to normal functions. I can Write:

function ScriptedFunction
{
    $input|foreach{"Got "+$_}
}
1,2,3|ScriptedFunction

And get

Got 1

Got 2

Got 3

As the result. But when I try to use that technique with a scripted method:

$object=New-Object System.Object
$object|Add-Member -MemberType ScriptMethod -Name ScriptedMethod -Value {$input|foreach{"Got "+$_}}
1,2,3|$object.ScriptedMethod

I get an error message: Expressions are only allowed as the first element of a pipeline.(adding () does not help BTW). What I'm looking for is a way to make that command work the same way it works with global functions.


This is not exactly what you are asking for but this has almost the same syntax: use a NoteProperty instead of a ScriptedMethod and call it using operators . or &:

$object = New-Object System.Object
$object | Add-Member -MemberType NoteProperty -Name Script -Value {$input|foreach{"Got "+$_}}
1,2,3 | & $object.Script

Output

Got 1
Got 2
Got 3

BUT: There is a caveat, perhaps a show stopper: this is not a script method at all, there will be no $this defined by the core for it (you can define $this = $object before invocation yourself but this is rather ugly, to send $object as a parameter would be better).


If I'm following, you can attach new script methods to exiting objects with the Add-Member cmdlet (take a look at Example code #4 in help). You can also add them in a Type file (xml) and load the file with the Update-TypeData cmdlet so they become available automatically when you have certain type of objects in hand.

UPDATE

You cannot pipe to methods. Methods you add are avaiable on the objects:

function ScriptedFunction
{
    process
    {
        $object = $_
        $object = $object | Add-Member -MemberType ScriptMethod -Name Increment -Value {$this+1} -PassThru -Force
        $object
    }
}

$newObjects = 1,2,3 | ScriptedFunction 
# increment the first object, its value is one, when the 
# Incereent method is invoked you get 2
$newObjects[0].Increment() 

2
0

精彩评论

暂无评论...
验证码 换一张
取 消