我使用的参数验证在高级功能(PowerShell的V4):如何将参数传递给PowerShell高级函数?
Function Test-It {
[cmdletbinding(DefaultParameterSetName='set1')]
Param (
[Parameter(ParameterSetName='set1',Position=0,Mandatory=$true,ValueFromPipeline=$true)]
[ValidatePattern("regex1")][string]$param1,
[Parameter(ParameterSetName='set1',Position=1,Mandatory=$true,ValueFromPipeline=$true)]
[ValidatePattern("regex2")][string]$param2
)
我想管参数1和参数的测试,它的功能从一个哈希表:
$ht.GetEnumerator() | % { $_.Name, $_.Value } | TestIt
问题是每个名称 - 值对作为一个参数而不是两个参数在位置1,位置2传递。我试过使用$ args [n],但参数语法不允许它。
如何将键值对作为两个不同的参数传递给高级函数?
为了回避这个问题,你可以使用有序的,而不是散列 –
如果我重构我的哈希表看起来像这样:''param1'='value1';'param2'='value2''我管这样:'' $ ht.GetEnumerator()| %{$ _} |测试它'也许命名参数将工作。我从参数定义中删除了'Position = n' ...仍在测试... – jott19
pkew我被推迟了,https://technet.microsoft.com/en-us/magazine/gg675931.aspx https:// www .bing.com/search?q = powershell + splatting –