简体   繁体   English

在PowerShell中管道化对象/属性/方法

[英]Piping into objects/properties/methods in PowerShell

In PowerShell, you can pipe into Cmdlets and into scripted functions. 在PowerShell中,您可以通过管道传递到Cmdlet和脚本函数。 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 ,我希望能够这样:

$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) 我知道可以使用Foreach-Object或将对象作为参数的Cmdlet来实现相同的功能-我想直接通过管道传递给对象或其成员的原因是为了获得优雅并保持OOP样式(使用对象的方法,而不是将对象作为参数发送)

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. PowerShell可以通过管道传递到正常功能。 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. 我收到一条错误消息: 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 . 这不是您所要的,但是它具有几乎相同的语法:使用NoteProperty而不是ScriptedMethod并使用operator进行调用. 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). 但是:有一个警告,也许是显示塞:这是不是在所有的脚本方法,也不会有$this由核心为它定义(你可以定义$this = $object自己调用之前但是这是比较难看,以将$object作为参数发送会更好)。

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). 如果我正在关注,则可以使用Add-Member cmdlet将新的脚本方法附加到退出的对象上(请参阅帮助中的示例代码4)。 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. 您还可以将它们添加到Type文件(xml)中,并使用Update-TypeData cmdlet加载文件,以便在您拥有某种类型的对象时,它们将自动变为可用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM