2013-06-20 51 views
4

我具有类似于THIS ONE如何验证允许空数组的PowerShell函数参数?

我传递到功能3个阵列的问题,我验证对象类型这样

function _TEST { 
[CmdletBinding()] 
param (
    [parameter(mandatory=$true)] 
    [array]$Path, 
    [parameter(mandatory=$true)] 
    [array]$RW, 
    [parameter(mandatory=$true)] 
    [array]$RO 
) 
process { 
    # my code 
} 

它的工作原理,除非我传递给运行,不会元件阵列,在这种情况下它返回这个错误_TEST : Cannot bind argument to parameter 'Path' because it is an empty collection.

有没有办法解决链接问题中类似于[AllowEmptyString()]的问题,还是我必须编写自定义代码来测试输入变量吗?

回答

14

尝试这种情况:

param (
    [parameter(mandatory=$true)] 
    [AllowEmptyCollection()] 
    [array]$Path 
) 

链接:

Parameter Validation Attributes

+0

使用该代码它允许也是对象从阵列的不同: '_TEST @()' - >没有错误; '_TEST“字符串”' - >无错误 – Naigel