C# 如何在PowerShell cmdlet中验证依赖参数

C# 如何在PowerShell cmdlet中验证依赖参数,c#,validation,powershell,cmdlets,cmdlet,C#,Validation,Powershell,Cmdlets,Cmdlet,对依赖参数执行PowerShell cmdlet验证的最佳方法是什么?例如,在下面的示例cmdlet中,我需要运行低值大于高值的验证,但对于验证属性,这似乎是不可能的 [Cmdlet(VerbsCommon.Get,“FakeData”)] 公共类GetFakeData:PSCmdlet { [参数(强制=真)] [完全或空验证] 公共整数低{get;set;} [参数(强制=真)] [完全或空验证] 公共整数高{get;set;} 受保护的覆盖void BeginProcessing() {

对依赖参数执行PowerShell cmdlet验证的最佳方法是什么?例如,在下面的示例cmdlet中,我需要运行低值大于高值的验证,但对于验证属性,这似乎是不可能的

[Cmdlet(VerbsCommon.Get,“FakeData”)]
公共类GetFakeData:PSCmdlet
{
[参数(强制=真)]
[完全或空验证]
公共整数低{get;set;}
[参数(强制=真)]
[完全或空验证]
公共整数高{get;set;}
受保护的覆盖void BeginProcessing()
{
如果(低>=高)
{
//这里有更好的例外吗?
抛出新的CmdletInvocationException(“低必须小于高”);
}
base.BeginProcessing();
}
受保护的覆盖无效OnProcessRecord()
{
//做些事情。。。
}
}

有没有更好的办法?我不喜欢上面的解决方案的主要一点是,我不能像验证属性那样抛出
ParameterBindingException
,因为它是一个内部类。我可以抛出
ArgumentException
pArgumentException
,但这些实际上是用于方法而不是cmdlet的。

您需要类似于cmdlet
get random
的东西。 因为您不能在cmdlet中使用
[validatescript()]
属性,因为该属性仅在运行时对powershell函数/脚本有效,所以您需要从microsoft.powershell.utility\get random中窃取该想法:

值检查在BeginProcessing()中完成,并使用自定义错误
throwmingreaterAnorEqualMax

protected override void BeginProcessing()
    {
      using (GetRandomCommand.tracer.TraceMethod())
      {
        if (this.SetSeed.HasValue)
          this.Generator = new Random(this.SetSeed.Value);
        if (this.EffectiveParameterSet == GetRandomCommand.MyParameterSet.RandomNumber)
        {
          if (this.IsInt(this.Maximum) && this.IsInt(this.Minimum))
          {
            int minValue = this.ConvertToInt(this.Minimum, 0);
            int maxValue = this.ConvertToInt(this.Maximum, int.MaxValue);
            if (minValue >= maxValue)
              this.ThrowMinGreaterThanOrEqualMax((object) minValue, (object) maxValue);
            this.WriteObject((object) this.Generator.Next(minValue, maxValue));
          }
          else
          {
            double min = this.ConvertToDouble(this.Minimum, 0.0);
            double max = this.ConvertToDouble(this.Maximum, double.MaxValue);
            if (min >= max)
              this.ThrowMinGreaterThanOrEqualMax((object) min, (object) max);
            this.WriteObject((object) this.GetRandomDouble(min, max));
          }
        }
        else
        {
          if (this.EffectiveParameterSet != GetRandomCommand.MyParameterSet.RandomListItem)
            return;
          this.chosenListItems = new List<object>();
          this.numberOfProcessedListItems = 0;
          if (this.Count != 0)
            return;
          this.Count = 1;
        }
      }
    }

您可以使用反编译器()查看代码的其余部分,以了解有关cmdlet自定义错误的更多信息

这正是我想要的,谢谢。我找到了这个MSDN文档,介绍了如何正确创建错误记录:,MSDN上ThrowterminationError的备注部分加强了您答案中显示的用法:@ScottLerch很高兴提供帮助!
private void ThrowMinGreaterThanOrEqualMax(object min, object max)
    {
      if (min == null)
        throw GetRandomCommand.tracer.NewArgumentNullException("min");
      if (max == null)
        throw GetRandomCommand.tracer.NewArgumentNullException("max");
      string errorId = "MinGreaterThanOrEqualMax";
      this.ThrowTerminatingError(new ErrorRecord((Exception) new ArgumentException(this.GetErrorDetails(errorId, min, max).Message), errorId, ErrorCategory.InvalidArgument, (object) null));
    }