gpt4 book ai didi

Powershell函数从管道接收多个参数

转载 作者:行者123 更新时间:2023-12-03 00:31:43 25 4
gpt4 key购买 nike

我正在编写如下函数:

Function Display-ItemLocation {
Param(
[ Parameter (
Mandatory = $True,
Valuefrompipeline = $True ) ]
[ String ]$stringItem,
[ Parameter (
Mandatory = $False,
Valuefrompipeline = $True ) ]
[ String ]$stringLocation = 'unknown'
)
Echo "The location of item $stringItem is $stringLocation."
}

Display-ItemLocation 'Illudium Q-36 Explosive Space Modulator' 'Mars'
Display-ItemLocation 'Plumbus'
它像书面一样工作正常。
The location of item Illudium Q-36 Explosive Space Modulator is Mars.
The location of item Plumbus is unknown.
我希望能够预加载包含多个数据对的数组,并通过管道将其发送到函数中。
$Data = @(
@('Bucket','Aisle 1'),
@('Spinach Pie','Freezer 4')
)
$Data | Display-ItemLocation
我找不到让它工作的神奇语法。该函数可以同时接受来自管道的一对值吗?

最佳答案

将您的管道绑定(bind)参数定义为按属性名称绑定(bind) - ValuefromPipelineByPropertyName - 然后管道(自定义)具有此类属性的对象 :

Function Display-ItemLocation {
Param(
[ Parameter (
Mandatory,
ValuefromPipelineByPropertyName ) ]
[ String ]$stringItem,
[ Parameter (
Mandatory = $False,
ValuefromPipelineByPropertyName ) ]
[ String ]$stringLocation = 'unknown'
)

process { # !! You need a `process` block to process *all* input objects.
Echo "The location of item $stringItem is $stringLocation."
}

}
顺便说一句: Display不是 approved verb在 PowerShell 中。
现在您可以按如下方式通过管道传递给该函数;请注意,属性名称必须与参数名称匹配:
$Data = [pscustomobject] @{ stringItem = 'Bucket'; stringLocation = 'Aisle 1' },
[pscustomobject] @{ stringItem = 'Spinach Pie'; stringLocation = 'Freezer 4' }

$Data | Display-ItemLocation
以上产生:
The location of item Bucket is Aisle 1.
The location of item Spinach Pie is Freezer 4.
  • 以上使用 [pscustomobject]实例 , 这很容易构建 ad hoc。
  • 请注意 哈希表 (例如,只是 @{ stringItem = 'Bucket'; stringLocation = 'Aisle 1' } )不工作 - 尽管 this GitHub issue 中正在讨论改变这一点.

  • 在 PSv5+ 中,您可以 或者定义一个自定义 class :
  • # Define the class.
    class Product {
    [string] $stringItem
    [string] $stringLocation
    Product([object[]] $itemAndLocation) {
    $this.stringItem = $itemAndLocation[0]
    $this.stringLocation = $itemAndLocation[1]
    }
    }

    # Same output as above.
    [Product[]] (
    ('Bucket', 'Aisle 1'),
    ('Spinach Pie', 'Freezer 4')
    ) | Display-ItemLocation

    关于Powershell函数从管道接收多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63363480/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com