PowerShell参数'ValueFromPipeline'不起作用 [英] PowerShell Parameter 'ValueFromPipeline' not working

查看:111
本文介绍了PowerShell参数'ValueFromPipeline'不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数来创建HTML文件,现在我正在对其进行一些改进.通过管道提供功能时,它可以很好地工作,但是并不能达到预期的效果.

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.

没关系:

Create-FileHTML -Path "L:\" -FileName "Title" "Text1", "Text2"
 L:\ Title Text1 Text2

那不行:

"Text1", "Text2" | Create-FileHTML -Path "L:\" -FileName "Title"
L:\ Title Text1 Text1
L:\ Title Text2 Text2

在使用管道时,如何向函数提供2个值,使其具有与我的第一个示例相同的结果?

How is it possible to feed 2 values to the function to have the same result as my first example when using the pipeline?

功能:

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"
     }
}

感谢您的帮助或提示.

推荐答案

您不能使用ValueFromPipeline发送多个参数,使用ValueFromPipelineByPropertyName而不是像这样发送参数:

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"

这篇关于PowerShell参数'ValueFromPipeline'不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆