【发布时间】:2013-09-16 05:02:38
【问题描述】:
对相关参数进行 PowerShell cmdlet 验证的最佳方法是什么?例如,在下面的示例 cmdlet 中,我需要运行 Low 大于 High 的验证,但这似乎不适用于验证属性。
[Cmdlet(VerbsCommon.Get, "FakeData")]
public class GetFakeData : PSCmdlet
{
[Parameter(Mandatory = true)]
[ValidateNotNullOrEmpty]
public int Low { get; set; }
[Parameter(Mandatory = true)]
[ValidateNotNullOrEmpty]
public int High { get; set; }
protected override void BeginProcessing()
{
if (Low >= High)
{
// Is there a better exception to throw here?
throw new CmdletInvocationException("Low must be less than High");
}
base.BeginProcessing();
}
protected override void OnProcessRecord()
{
// Do stuff...
}
}
有没有更好的方法来做到这一点?我不喜欢上述解决方案的主要一点是我不能像验证属性那样抛出ParameterBindingException,因为它是一个内部类。我可以抛出 ArgumentException 或 PSArgumentException 但这些确实是针对方法而不是 cmdlet。
【问题讨论】:
标签: c# validation powershell cmdlets cmdlet