I'm attempting to create a logging function that accepts pipeline positionally-bound parameters, and positionally-bound arguments. However, I keep getting this error with the following code:
Function Test
{
[CmdletBinding(SupportsShouldProcess,DefaultParameterSetName='def')]
Param(
[Parameter(Position=0,ParameterSetName='def')]
[String]$Pos1,
[Parameter(ValueFromPipeline,Position=1,ParameterSetName='pip')]
[String]$InputObject,
[Parameter(Position=1,ParameterSetName='def')]
[Parameter(Position=0,ParameterSetName='pip')]
[String]$State
)
Process
{
Switch ($PScmdlet.ParameterSetName)
{
'def' {Write-Host "${State}: $Pos1"}
'pip' {Write-Host "${State}: $InputObject"}
}
}
}
PS C:\> 'this' | Test 'error'
test : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:10
+ 'test' | test 'error'
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (test:String) [Test], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Test
I can get the position-bound pipeline calling of the function to work with the following example:
Function Test
{
Param(
[Parameter(Position=1,ValueFromPipeline)]
[String]$Msg,
[Parameter(Position=0)]
[String]$State
)
Process
{
Write-Host "${State}: $Msg"
}
}
PS C:\> 'this' | Test 'error'
error: this
So my question: how can I create a function that will take positionally-bound arguments at command line (Test 'message' 'status') and from the pipeline ('message' | Test 'status') without the need to explicitly call the parameter name ('message' | Test -State 'status')?