将 object[] 传递给 PowerShell 中的函数 [英] Pass object[] into a function in PowerShell

查看:55
本文介绍了将 object[] 传递给 PowerShell 中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a resource on how to pass in a Object[] as a parameter within a PowerShell function?

这两个函数都是 cmdlet 并且可以正确导出,但是我在第二个函数中看不到 $Return 对象.

Both of these functions are cmdlets and they are being exported correctly, but I cannot see the $Return object in my second function.

是否需要以下内容?

ParameterAttribute.ValueFromPipeline 属性(系统.管理.自动化)

# 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 {}
}

推荐答案

$Input 是一个自动变量的名称.使用不同的名称.

$Input is the name of an automatic variable. Use a different name.

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

I recommend $InputObject as that is in common usage so it has a well-understood meaning, but usually that means you are accepting pipeline input as well.

当然,如果有一个更能描述此参数的名称,您应该使用它.

Of course if there's a name that's more descriptive for this parameter, you should use that.

我已经在 PowerShell GitHub 项目上提交了这个问题,建议 Set-StrictMode 被修改以检查自动变量赋值.

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

这篇关于将 object[] 传递给 PowerShell 中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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