当命令行开关接受管道输入时,ByPropertyName 和 ByValue 有什么区别? [英] When a commandlet accepts pipeline input, what is the difference between ByPropertyName and ByValue?

查看:52
本文介绍了当命令行开关接受管道输入时,ByPropertyName 和 ByValue 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些 PowerShell commandlet 接受管道输入 ByProperyName,一些接受 ByValue,另一些接受两者兼而有之.这是什么意思?它如何影响我们的 PowerShell 脚本?

Some PowerShell commandlets accept pipeline input ByProperyName, some do it ByValue, others do it for both. What does this mean? How does it impact our PowerShell scripts?

推荐答案

ValueFromPipeline 参数属性 会将参数值映射到从管道传入的任何对象类型.如果您使用 ValueFromPipelineByPropertyName 参数属性,然后将使用管道传递到命令+参数的对象中的特定属性.

The ValueFromPipeline parameter attribute will map the parameter value to whatever object type is passed in from the pipeline. If you use the ValueFromPipelineByPropertyName parameter attribute, then a specific property will be used from the objects that are piped into the command+parameter.

Get-Process | Stop-Process; # Stop-Process expects to receive a Process object

<#
 -InputObject <Process[]>
    Stops the processes represented by the specified process objects. Enter a variable that contains the objects, or
    type a command or expression that gets the objects.

    Required?                    true
    Position?                    1
    Default value
    Accept pipeline input?       true (ByValue)
    Accept wildcard characters?  false
#>

ValueFromPipelineByPropertyName 示例

# Get-Process looks for a ComputerName property on the incoming objects
[PSCustomObject]@{
    ComputerName = 'localhost';
    } | Get-Process;

<#
-ComputerName <String[]>
    Gets the processes running on the specified computers. The default is the local computer.

    Type the NetBIOS name, an IP address, or a fully qualified domain name of one or more computers. To specify the
    local computer, type the computer name, a dot (.), or "localhost".

    This parameter does not rely on Windows PowerShell remoting. You can use the ComputerName parameter of Get-Process
    even if your computer is not configured to run remote commands.

    Required?                    false
    Position?                    named
    Default value                Local computer
    Accept pipeline input?       True (ByPropertyName)
    Accept wildcard characters?  false
#>

这篇关于当命令行开关接受管道输入时,ByPropertyName 和 ByValue 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆