PowerShell Parameter 'ValueFromPipeline' not working

萝らか妹 提交于 2019-12-11 18:45:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!