Powershell pass object[] into function

2019-01-09 15:56发布

问题:

Can someone please point me to a resource that can tell me how to pass in a Object[] as a parameter within a powershell function? Both of these functions are Cmdlets and they are being exported correctly, but I cannot see the $Return object in my second function.

Is something like this needed? https://msdn.microsoft.com/en-us/library/system.management.automation.parameterattribute.valuefrompipeline(v=vs.85).aspx

# Within powershell code

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

Where this is my function definiton

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

回答1:

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

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.

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



标签: powershell