当命令行开关接受管道输入时,ByPropertyName 和 ByValue 之间有什么区别?

2024-01-10

一些 PowerShell commandlet 接受管道输入 ByProperyName,一些接受 ByValue,另一些则同时接受两者。这是什么意思?它如何影响我们的 PowerShell 脚本?


The ValueFromPipeline参数属性 http://msdn.microsoft.com/en-us/library/system.management.automation.parameterattribute.valuefrompipeline%28v=vs.85%29.aspx会将参数值映射到从管道传入的任何对象类型。如果您使用ValueFromPipelineByPropertyName参数属性 http://msdn.microsoft.com/en-us/library/system.management.automation.parameterattribute.valuefrompipelinebypropertyname%28v=vs.85%29.aspx,那么将从通过管道传输到命令+参数的对象中使用特定属性。

ValueFromPipeline 示例

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
#>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当命令行开关接受管道输入时,ByPropertyName 和 ByValue 之间有什么区别? 的相关文章

随机推荐