Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

You cannot retrieve a required parameter from "either pipeline or argument"

## Context:

  • A mandatory parameter of a function retrieved from pipeline is defined like this:
param(
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)] [string] $foo
)
  • A mandatory parameter of a function retrieved from argument is defined like this:
param(
    [Parameter(ValueFromPipeline=$false, Mandatory=$true)] [string] $foo
)

Problem:

You cannot define a mandatory parameter which is retrieved from pipeline OR argument.

  • If you set ValueFromPipeline for a mandatory parameter to $true, you cannot pass the parameter from argument.
  • If you set ValueFromPipeline for a mandatory parameter to $false, you cannot pass the parameter from pipeline.

If you set Mandatory to $false, you can pass the parameter from either pipeline or argument, but of course it is not a mandatory parameter.

Solution:

There are no workaround for this problem.

Added 2017-04-12

You can pass parameters from either pipeline or argument by:

param(
    [Parameter(ValueFromPipeline=$true, Mandatory=$true)] [string] $foo
)

And, you cannot define mandatory parameters that only retrieved from pipeline but not from argument.