问题
I wrote a function to create HTML files and I'm now in the process to improve it a bit. When feeding the function through the pipeline it works fine but it's not resulting in the desired action.
This is ok:
Create-FileHTML -Path "L:\" -FileName "Title" "Text1", "Text2"
L:\ Title Text1 Text2
The is not ok:
"Text1", "Text2" | Create-FileHTML -Path "L:\" -FileName "Title"
L:\ Title Text1 Text1
L:\ Title Text2 Text2
How is it possible to feed 2 values to the function to have the same result as my first example when using the pipeline?
The function:
Function Create-FileHTML {
[CmdletBinding()]
Param (
[parameter(Mandatory=$true,Position=1)]
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[String[]] $Path,
[parameter(Mandatory=$true,Position=2)]
[String[]] $FileName,
[parameter(Mandatory=$true,ValueFromPipeline=$true,Position=3)]
[String[]] $Message1,
[parameter(Mandatory=$false,ValueFromPipeline=$true,Position=4)]
[String[]] $Message2
)
Process {
Write-Host "$Path $FileName $Message1 $Message2"
}
}
Thank you for your help or tips.
回答1:
You cannot use ValueFromPipeline to send more than one parameter, Use ValueFromPipelineByPropertyName rather like and send the parameters like:
$params = New-Object psobject -property @{Message1 = 'Text1'; Message2 = 'Text2'}
$params | Create-FileHTML -Path "L:\" -FileName "Title"
来源:https://stackoverflow.com/questions/24776576/powershell-parameter-valuefrompipeline-not-working