【问题标题】:Pass object[] into a function in PowerShell将 object[] 传递给 PowerShell 中的函数
【发布时间】:2019-03-29 05:37:48
【问题描述】:

是否有关于如何在 PowerShell 函数中将 Object[] 作为参数传递的资源?

这两个函数都是 cmdlet,它们被正确导出,但我在第二个函数中看不到 $Return 对象。

需要类似下面的东西吗?

ParameterAttribute.ValueFromPipeline Property (System.Management.Automation)

# Within PowerShell code

$Return = My-Function -Param "value" # $Return is of type Object[]
$ModifiedReturn = My-SecondFunction -Input $Return

这是我的函数定义:

function My-SecondFunction
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [Object[]]$Input
    )
    begin {}
    process
    {
        Write-Host "test: $Input" # Does not return anything
    }
    end {}
}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    $Input is the name of an automatic variable。使用不同的名称。

    我推荐$InputObject,因为它很常用,所以它的含义很好理解,但通常这意味着你也接受管道输入。

    当然,如果这个参数有一个更具描述性的名称,你应该使用它。

    I have submitted this issue on the PowerShell GitHub project suggesting that Set-StrictMode be modified to check for automatic variable assignment.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2016-03-30
      • 2022-10-07
      相关资源
      最近更新 更多